Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pkg-json generation during tag #7013

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scopes/component/isolator/isolator.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import path from 'path';
import equals from 'ramda/src/equals';
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
import RemovePath from '@teambit/legacy/dist/consumer/component/sources/remove-path';
import { PackageJsonTransformer } from '@teambit/legacy/dist/consumer/component/package-json-transformer';
import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
import { ArtifactVinyl } from '@teambit/legacy/dist/consumer/component/sources/artifact';
import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
Expand Down Expand Up @@ -645,7 +645,7 @@ export class IsolatorMain {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
packageJson.addOrUpdateProperty('version', semver.inc(legacyComp.version!, 'prerelease') || '0.0.1-0');
}
await PackageJsonTransformer.applyTransformers(legacyComp, packageJson);
await PackageJsonTransformer.applyTransformers(component, packageJson);
const valuesToMerge = legacyComp.overrides.componentOverridesPackageJsonData;
packageJson.mergePackageJsonObject(valuesToMerge);
dataToPersist.addFile(packageJson.toVinylFile());
Expand Down
2 changes: 1 addition & 1 deletion scopes/harmony/bit/load-bit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import BitMap from '@teambit/legacy/dist/consumer/bit-map';
import ComponentLoader from '@teambit/legacy/dist/consumer/component/component-loader';
import ComponentConfig from '@teambit/legacy/dist/consumer/config/component-config';
import ComponentOverrides from '@teambit/legacy/dist/consumer/config/component-overrides';
import { PackageJsonTransformer } from '@teambit/legacy/dist/consumer/component/package-json-transformer';
import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
import { satisfies } from 'semver';
import { getHarmonyVersion } from '@teambit/legacy/dist/bootstrap';
import { ExtensionDataList } from '@teambit/legacy/dist/consumer/config';
Expand Down
2 changes: 1 addition & 1 deletion scopes/harmony/testing/load-aspect/load-aspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NodeAspect } from '@teambit/node';
import ComponentLoader from '@teambit/legacy/dist/consumer/component/component-loader';
import ComponentConfig from '@teambit/legacy/dist/consumer/config/component-config';
import ComponentOverrides from '@teambit/legacy/dist/consumer/config/component-overrides';
import { PackageJsonTransformer } from '@teambit/legacy/dist/consumer/component/package-json-transformer';
import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
import { ExtensionDataList } from '@teambit/legacy/dist/consumer/config';
import WorkspaceConfig from '@teambit/legacy/dist/consumer/config/workspace-config';
import { DependencyResolver } from '@teambit/legacy/dist/consumer/component/dependencies/dependency-resolver';
Expand Down
13 changes: 3 additions & 10 deletions scopes/pkg/pkg/pkg.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { IsolatorAspect, IsolatorMain } from '@teambit/isolator';
import { LoggerAspect, LoggerMain, Logger } from '@teambit/logger';
import { ScopeAspect, ScopeMain } from '@teambit/scope';
import { Workspace, WorkspaceAspect } from '@teambit/workspace';
import { PackageJsonTransformer } from '@teambit/legacy/dist/consumer/component/package-json-transformer';
import LegacyComponent from '@teambit/legacy/dist/consumer/component';
import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
import { BuilderMain, BuilderAspect } from '@teambit/builder';
import { CloneConfig } from '@teambit/new-component-helper';
import { BitError } from '@teambit/bit-error';
Expand Down Expand Up @@ -472,16 +471,10 @@ export class PkgMain implements CloneConfig {
}

async transformPackageJson(
legacyComponent: LegacyComponent,
component: Component,
packageJsonObject: Record<string, any>
): Promise<Record<string, any>> {
// const newId = await this.workspace.resolveComponentId(component.id);
// const newComponent = await this.workspace.get(newId);
const host = this.componentAspect.getHost();
const id = await host.resolveComponentId(legacyComponent.id);
const newComponent = await host.get(id);
if (!newComponent) throw new Error(`cannot transform package.json of component: ${legacyComponent.id.toString()}`);
const newProps = this.getPackageJsonModifications(newComponent);
const newProps = this.getPackageJsonModifications(component);
return Object.assign(packageJsonObject, newProps);
}
}
Expand Down
2 changes: 1 addition & 1 deletion scopes/typescript/ts-server/process-based-tsserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class ProcessBasedTsServer {
if (this.options.logToConsole) {
this.logger.console(`${msg} ${JSON.stringify(obj, undefined, 4)}`);
} else {
this.logger.debug(msg, obj);
this.logger.trace(msg, obj);
}
}

Expand Down
2 changes: 2 additions & 0 deletions scopes/workspace/modules/node-modules-linker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export {
linkToNodeModulesWithCodemod,
linkToNodeModulesByComponents,
} from './node-modules-linker';

export { PackageJsonTransformer } from './package-json-transformer';
64 changes: 27 additions & 37 deletions scopes/workspace/modules/node-modules-linker/node-modules-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ import R from 'ramda';
import { BitId } from '@teambit/legacy-bit-id';
import { IS_WINDOWS, PACKAGE_JSON, SOURCE_DIR_SYMLINK_TO_NM } from '@teambit/legacy/dist/constants';
import BitMap from '@teambit/legacy/dist/consumer/bit-map/bit-map';
import ComponentMap from '@teambit/legacy/dist/consumer/bit-map/component-map';
import ComponentsList from '@teambit/legacy/dist/consumer/component/components-list';
import ConsumerComponent from '@teambit/legacy/dist/consumer/component/consumer-component';
import PackageJsonFile from '@teambit/legacy/dist/consumer/component/package-json-file';
import { PackageJsonTransformer } from '@teambit/legacy/dist/consumer/component/package-json-transformer';
import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
import RemovePath from '@teambit/legacy/dist/consumer/component/sources/remove-path';
import Consumer from '@teambit/legacy/dist/consumer/consumer';
import logger from '@teambit/legacy/dist/logger/logger';
import { first } from '@teambit/legacy/dist/utils';
import getNodeModulesPathOfComponent from '@teambit/legacy/dist/utils/bit/component-node-modules-path';
import { PathOsBasedRelative } from '@teambit/legacy/dist/utils/path';
import { BitIds } from '@teambit/legacy/dist/bit-id';
import { changeCodeFromRelativeToModulePaths } from '@teambit/legacy/dist/consumer/component-ops/codemod-components';
import Symlink from '@teambit/legacy/dist/links/symlink';
import { Workspace } from '@teambit/workspace';
import { snapToSemver } from '@teambit/component-package-version';
import { Component } from '@teambit/component';
import { PackageJsonTransformer } from './package-json-transformer';

type LinkDetail = { from: string; to: string };
export type NodeModulesLinksResult = {
Expand All @@ -36,12 +33,10 @@ export type NodeModulesLinksResult = {
* for example, require('@bit/remote-scope.bar.foo)
*/
export default class NodeModuleLinker {
components: ConsumerComponent[];
consumer: Consumer;
bitMap: BitMap; // preparation for the capsule, which is going to have only BitMap with no Consumer
dataToPersist: DataToPersist;
constructor(components: ConsumerComponent[], consumer: Consumer) {
this.components = ComponentsList.getUniqueComponents(components);
constructor(private components: Component[], consumer: Consumer) {
this.consumer = consumer;
this.bitMap = consumer.bitMap;
this.dataToPersist = new DataToPersist();
Expand All @@ -60,16 +55,14 @@ export default class NodeModuleLinker {
await pMapSeries(this.components, (component) => {
const componentId = component.id.toString();
logger.debug(`linking component to node_modules: ${componentId}`);
const componentMap: ComponentMap = this.bitMap.getComponent(component.id);
component.componentMap = componentMap;
return this._populateComponentsLinks(component);
});

return this.dataToPersist;
}
getLinksResults(): NodeModulesLinksResult[] {
const linksResults: NodeModulesLinksResult[] = [];
const getExistingLinkResult = (id) => linksResults.find((linkResult) => linkResult.id.isEqual(id));
const getExistingLinkResult = (id: BitId) => linksResults.find((linkResult) => linkResult.id.isEqual(id));
const addLinkResult = (id: BitId | null | undefined, from: string, to: string) => {
if (!id) return;
const existingLinkResult = getExistingLinkResult(id);
Expand All @@ -83,9 +76,9 @@ export default class NodeModuleLinker {
addLinkResult(symlink.componentId, symlink.src, symlink.dest);
});
this.components.forEach((component) => {
const existingLinkResult = getExistingLinkResult(component.id);
const existingLinkResult = getExistingLinkResult(component.id._legacy);
if (!existingLinkResult) {
linksResults.push({ id: component.id, bound: [] });
linksResults.push({ id: component.id._legacy, bound: [] });
}
});
return linksResults;
Expand All @@ -104,14 +97,15 @@ export default class NodeModuleLinker {
* node expects the module inside node_modules to have either package.json with valid "main"
* property or an index.js file. this main property can't be relative.
*/
async _populateComponentsLinks(component: ConsumerComponent): Promise<void> {
const componentId = component.id;
async _populateComponentsLinks(component: Component): Promise<void> {
const legacyComponent = component.state._consumer as ConsumerComponent;
const componentId = component.id._legacy;
const linkPath: PathOsBasedRelative = getNodeModulesPathOfComponent({
bindingPrefix: component.bindingPrefix,
bindingPrefix: legacyComponent.bindingPrefix,
id: componentId,
allowNonScope: true,
defaultScope: this._getDefaultScope(component),
extensions: component.extensions,
defaultScope: this._getDefaultScope(legacyComponent),
extensions: legacyComponent.extensions,
});

this.symlinkComponentDir(component, linkPath);
Expand All @@ -122,22 +116,22 @@ export default class NodeModuleLinker {
/**
* symlink the entire source directory into "src" in node-modules.
*/
private symlinkComponentDir(component: ConsumerComponent, linkPath: PathOsBasedRelative) {
const componentMap = component.componentMap as ComponentMap;
private symlinkComponentDir(component: Component, linkPath: PathOsBasedRelative) {
const componentMap = this.bitMap.getComponent(component.id._legacy);

const filesToBind = componentMap.getAllFilesPaths();
filesToBind.forEach((file) => {
const fileWithRootDir = path.join(componentMap.rootDir as string, file);
const dest = path.join(linkPath, file);
this.dataToPersist.addSymlink(Symlink.makeInstance(fileWithRootDir, dest, component.id, true));
this.dataToPersist.addSymlink(Symlink.makeInstance(fileWithRootDir, dest, component.id._legacy, true));
});

if (IS_WINDOWS) {
this.dataToPersist.addSymlink(
Symlink.makeInstance(
componentMap.rootDir as string,
path.join(linkPath, SOURCE_DIR_SYMLINK_TO_NM),
component.id
component.id._legacy
)
);
}
Expand Down Expand Up @@ -217,24 +211,24 @@ export default class NodeModuleLinker {
* Since an authored component doesn't have rootDir, it's impossible to symlink to the component directory.
* It makes it easier for Author to use absolute syntax between their own components.
*/
private async createPackageJson(component: ConsumerComponent) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
const hasPackageJsonAsComponentFile = component.files.some((file) => file.relative === PACKAGE_JSON);
private async createPackageJson(component: Component) {
const legacyComp = component.state._consumer as ConsumerComponent;
const hasPackageJsonAsComponentFile = legacyComp.files.some((file) => file.relative === PACKAGE_JSON);
if (hasPackageJsonAsComponentFile) return; // don't generate package.json on top of the user package.json
const dest = path.join(
getNodeModulesPathOfComponent({
...component,
id: component.id,
...legacyComp,
id: legacyComp.id,
allowNonScope: true,
})
);
const packageJson = PackageJsonFile.createFromComponent(dest, component, true, true);
const packageJson = PackageJsonFile.createFromComponent(dest, legacyComp, true, true);
await this._applyTransformers(component, packageJson);
if (IS_WINDOWS) {
// in the workspace, override the "types" and add the "src" prefix.
// otherwise, the navigation and auto-complete won't work on the IDE.
// this is for Windows only. For Linux, we use symlinks for the files.
packageJson.addOrUpdateProperty('types', `${SOURCE_DIR_SYMLINK_TO_NM}/${component.mainFile}`);
packageJson.addOrUpdateProperty('types', `${SOURCE_DIR_SYMLINK_TO_NM}/${legacyComp.mainFile}`);
}
if (packageJson.packageJsonObject.version === 'latest') {
packageJson.packageJsonObject.version = '0.0.1-new';
Expand All @@ -253,7 +247,7 @@ export default class NodeModuleLinker {
/**
* these are changes made by aspects
*/
async _applyTransformers(component: ConsumerComponent, packageJson: PackageJsonFile) {
async _applyTransformers(component: Component, packageJson: PackageJsonFile) {
return PackageJsonTransformer.applyTransformers(component, packageJson);
}
}
Expand All @@ -276,24 +270,20 @@ export async function linkToNodeModulesByIds(
bitIds: BitId[],
loadFromScope = false
): Promise<NodeModulesLinksResult[]> {
const componentsIds = BitIds.fromArray(bitIds);
const componentsIds = await workspace.resolveMultipleComponentIds(bitIds);
if (!componentsIds.length) return [];
const getComponents = async () => {
if (loadFromScope) {
return Promise.all(componentsIds.map((id) => workspace.consumer.loadComponentFromModel(id)));
return workspace.scope.getMany(componentsIds);
}
const { components } = await workspace.consumer.loadComponents(componentsIds);
return components;
return workspace.getMany(componentsIds);
};
const components = await getComponents();
const nodeModuleLinker = new NodeModuleLinker(components, workspace.consumer);
return nodeModuleLinker.link();
}

export async function linkToNodeModulesByComponents(components: Component[], workspace: Workspace) {
const nodeModuleLinker = new NodeModuleLinker(
components.map((c) => c.state._consumer),
workspace.consumer
);
const nodeModuleLinker = new NodeModuleLinker(components, workspace.consumer);
return nodeModuleLinker.link();
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import mapSeries from 'p-map-series';
import { replacePlaceHolderForPackageValue } from '../../utils/bit/component-placeholders';

import ConsumerComponent from './consumer-component';
import PackageJsonFile from './package-json-file';
import { parseScope } from '../../utils/bit/parse-scope';
import PackageJsonFile from '@teambit/legacy/dist/consumer/component/package-json-file';
import { parseScope } from '@teambit/legacy/dist/utils/bit/parse-scope';
import { replacePlaceHolderForPackageValue } from '@teambit/legacy/dist/utils/bit/component-placeholders';
import { Component } from '@teambit/component';

type PackageJsonTransformers = Function[];

export class PackageJsonTransformer {
static packageJsonTransformersRegistry: PackageJsonTransformers = [];
static registerPackageJsonTransformer(
func: (component: ConsumerComponent, packageJsonObject: Record<string, any>) => Promise<Record<string, any>>
func: (component: Component, packageJsonObject: Record<string, any>) => Promise<Record<string, any>>
) {
this.packageJsonTransformersRegistry.push(func);
}

/**
* these are changes made by aspects
*/
static async applyTransformers(component: ConsumerComponent, packageJson: PackageJsonFile) {
static async applyTransformers(component: Component, packageJson: PackageJsonFile) {
let newPackageJsonObject = packageJson.packageJsonObject;

await mapSeries(PackageJsonTransformer.packageJsonTransformersRegistry, async (transformer) => {
newPackageJsonObject = await transformer(component, newPackageJsonObject);
});

const scopeId = component.scope || component.defaultScope;
const scopeId = component.id.scope;
const { scope, owner } = parseScope(scopeId);
const name = component.id.name;
const name = component.id.fullName;

const contextForReplace = {
mainFile: component.mainFile,
mainFile: component.state._consumer.mainFile,
name,
scope,
scopeId,
Expand Down