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

feat(import): introduce "--track-only" to only write entries to .bitmap #7117

Merged
merged 3 commits into from
Mar 4, 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
22 changes: 22 additions & 0 deletions e2e/harmony/import-harmony.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,26 @@ describe('import functionality on Harmony', function () {
expect(show.devPackageDependencies).to.include({ '@types/cors': '^2.8.10' });
});
});
describe('with --track-only flag', () => {
before(() => {
helper = new Helper();
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(3);
helper.command.tagAllWithoutBuild();
helper.command.export();

helper.scopeHelper.reInitLocalScope();
helper.scopeHelper.addRemoteScope();
const emptyBitMap = helper.bitMap.read();
helper.command.importComponent('*');
helper.fs.writeFile(`${helper.scopes.remote}/comp1/file`, 'hello');
helper.bitMap.write(emptyBitMap);

helper.command.importComponent('*', '--track-only');
});
it('should only add the entries to the .bitmap without writing files', () => {
helper.bitMap.expectToHaveId('comp1');
expect(path.join(helper.scopes.localPath, `${helper.scopes.remote}/comp1/file`)).to.be.a.file();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ManyComponentsWriterParams {
skipDependencyInstallation?: boolean;
verbose?: boolean;
resetConfig?: boolean;
skipWritingToFs?: boolean;
}

export type ComponentWriterResults = { installationError?: Error; compilationError?: Error };
Expand Down Expand Up @@ -87,6 +88,7 @@ export class ComponentWriterMain {
}
}
private async persistComponentsData(opts: ManyComponentsWriterParams) {
if (opts.skipWritingToFs) return;
const dataToPersist = new DataToPersist();
opts.components.forEach((component) => dataToPersist.merge(component.dataToPersist));
const componentsConfig = this.consumer?.config?.componentsConfig;
Expand Down Expand Up @@ -211,6 +213,7 @@ to move all component files to a different directory, run bit remove and then bi
componentMap: ComponentMap | null | undefined,
opts: ManyComponentsWriterParams
) {
if (opts.skipWritingToFs) return;
// if not writeToPath specified, it goes to the default directory. When componentMap exists, the
// component is not new, and it's ok to override the existing directory.
if (!opts.writeToPath && componentMap) return;
Expand Down
6 changes: 5 additions & 1 deletion scopes/component/component-writer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ComponentWriterAspect } from './component-writer.aspect';

export type { ComponentWriterMain, ComponentWriterResults } from './component-writer.main.runtime';
export type {
ComponentWriterMain,
ComponentWriterResults,
ManyComponentsWriterParams,
} from './component-writer.main.runtime';
export default ComponentWriterAspect;
export { ComponentWriterAspect };
9 changes: 5 additions & 4 deletions scopes/scope/importer/import-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import VersionDependencies, {
} from '@teambit/legacy/dist/scope/version-dependencies';
import { GraphMain } from '@teambit/graph';
import { Workspace } from '@teambit/workspace';
import { ComponentWriterMain, ComponentWriterResults } from '@teambit/component-writer';
import { ComponentWriterMain, ComponentWriterResults, ManyComponentsWriterParams } from '@teambit/component-writer';

export type ImportOptions = {
ids: string[]; // array might be empty
Expand All @@ -58,6 +58,7 @@ export type ImportOptions = {
};
allHistory?: boolean;
fetchDeps?: boolean; // by default, if a component was tagged with > 0.0.900, it has the flattened-deps-graph in the object
trackOnly?: boolean;
};
type ComponentMergeStatus = {
component: Component;
Expand Down Expand Up @@ -483,7 +484,7 @@ bit import ${idsFromRemote.map((id) => id.toStringWithoutVersion()).join(' ')}`)
// the typical objectsOnly option is when a user cloned a project with components tagged to the source code, but
// doesn't have the model objects. in that case, calling getComponentStatusById() may return an error as it relies
// on the model objects when there are dependencies
if (this.options.override || this.options.objectsOnly || this.options.merge) return;
if (this.options.override || this.options.objectsOnly || this.options.merge || this.options.trackOnly) return;
const componentsStatuses = await this.consumer.getManyComponentsStatuses(ids);
const modifiedComponents = componentsStatuses
.filter(({ status }) => status.modified || status.newlyCreated)
Expand Down Expand Up @@ -656,14 +657,14 @@ bit import ${idsFromRemote.map((id) => id.toStringWithoutVersion()).join(' ')}`)

async _writeToFileSystem(components: Component[]): Promise<ComponentWriterResults> {
const componentsToWrite = await this.updateAllComponentsAccordingToMergeStrategy(components);
const manyComponentsWriterOpts = {
consumer: this.consumer,
const manyComponentsWriterOpts: ManyComponentsWriterParams = {
components: componentsToWrite,
writeToPath: this.options.writeToPath,
writeConfig: this.options.writeConfig,
skipDependencyInstallation: !this.options.installNpmPackages,
verbose: this.options.verbose,
throwForExistingDir: !this.options.override,
skipWritingToFs: this.options.trackOnly,
};
return this.componentWriter.writeMany(manyComponentsWriterOpts);
}
Expand Down
7 changes: 7 additions & 0 deletions scopes/scope/importer/import.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class ImportCmd implements Command {
'relevant for fetching all components objects. avoid optimizations, fetch all history versions, always',
],
['', 'fetch-deps', 'fetch dependencies objects'],
['', 'track-only', 'do not write any file, just create .bitmap entries of the imported components'],
] as CommandOptions;
loader = true;
migration = true;
Expand Down Expand Up @@ -95,6 +96,7 @@ ${WILDCARD_HELP('import')}`;
dependents = false,
allHistory = false,
fetchDeps = false,
trackOnly = false,
}: {
path?: string;
objects?: boolean;
Expand All @@ -111,6 +113,7 @@ ${WILDCARD_HELP('import')}`;
dependents?: boolean;
allHistory?: boolean;
fetchDeps?: boolean;
trackOnly?: boolean;
}
): Promise<any> {
if (objects && merge) {
Expand All @@ -125,6 +128,9 @@ ${WILDCARD_HELP('import')}`;
if (!ids.length && dependents) {
throw new GeneralError('you have to specify ids to use "--dependents" flag');
}
if (!ids.length && trackOnly) {
throw new GeneralError('you have to specify ids to use "--track-only" flag');
}
if (skipNpmInstall) {
// eslint-disable-next-line no-console
console.log(
Expand Down Expand Up @@ -156,6 +162,7 @@ ${WILDCARD_HELP('import')}`;
importDependents: dependents,
allHistory,
fetchDeps,
trackOnly,
};
const importResults = await this.importer.import(importOptions, this._packageManagerArgs);
const { importDetails, importedIds, importedDeps, installationError, compilationError } = importResults;
Expand Down