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 a new flag "--dependencies-head" #8920

Merged
merged 1 commit into from
May 28, 2024
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
27 changes: 21 additions & 6 deletions scopes/scope/importer/import-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type ImportOptions = {
writeConfigFiles: boolean; // default: true
objectsOnly?: boolean;
importDependenciesDirectly?: boolean; // default: false, normally it imports them as packages, not as imported
importHeadDependenciesDirectly?: boolean; // default: false, similar to importDependenciesDirectly, but it checks out to their head
importDependents?: boolean;
dependentsVia?: string;
dependentsAll?: boolean;
Expand Down Expand Up @@ -444,8 +445,10 @@ if you just want to get a quick look into this snap, create a new workspace and
: await this.getBitIdsForNonLanes();
const shouldImportDependents =
this.options.importDependents || this.options.dependentsVia || this.options.dependentsAll;
if (this.options.importDependenciesDirectly || shouldImportDependents) {
if (this.options.importDependenciesDirectly) {
const shouldImportDependencies =
this.options.importDependenciesDirectly || this.options.importHeadDependenciesDirectly;
if (shouldImportDependencies || shouldImportDependents) {
if (shouldImportDependencies) {
const dependenciesIds = await this.getFlattenedDepsUnique(bitIds);
bitIds.push(...dependenciesIds);
}
Expand All @@ -467,17 +470,29 @@ if you just want to get a quick look into this snap, create a new workspace and
return ComponentIdList.uniqFromArray(flattenedDeps);
};
const flattened = getFlattened();
const withLatest = this.removeMultipleVersionsKeepLatest(flattened);
return withLatest;
return this.options.importHeadDependenciesDirectly
? this.uniqWithoutVersions(flattened)
: this.removeMultipleVersionsKeepLatest(flattened);
}

private uniqWithoutVersions(flattened: ComponentIdList) {
const latest = flattened.toVersionLatest();
return ComponentIdList.uniqFromArray(latest);
}

private removeMultipleVersionsKeepLatest(flattened: ComponentIdList): ComponentID[] {
const grouped = flattened.toGroupByIdWithoutVersion();
const latestVersions = Object.keys(grouped).map((key) => {
const ids = grouped[key];
if (ids.length === 1) return ids[0];
const latest = getLatestVersionNumber(ids, ids[0].changeVersion(LATEST_VERSION));
return latest;
try {
const latest = getLatestVersionNumber(ids, ids[0].changeVersion(LATEST_VERSION));
return latest;
} catch (err: any) {
throw new Error(`a dependency "${key}" was found with multiple versions, unable to find which one of them is newer.
error: ${err.message}
consider running with "--dependencies-head" flag instead, which checks out to the head of the dependencies`);
}
});

return latestVersions;
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 @@ -27,6 +27,7 @@ type ImportFlags = {
filterEnvs?: string;
saveInLane?: boolean;
dependencies?: boolean;
dependenciesHead?: boolean;
dependents?: boolean;
dependentsDryRun?: boolean;
dependentsVia?: string;
Expand Down Expand Up @@ -75,6 +76,7 @@ export class ImportCmd implements Command {
'dependencies',
'import all dependencies (bit components only) of imported components and write them to the workspace',
],
['', 'dependencies-head', 'same as --dependencies, except it imports the dependencies with their head version'],
[
'',
'dependents',
Expand Down Expand Up @@ -211,6 +213,7 @@ export class ImportCmd implements Command {
filterEnvs,
saveInLane = false,
dependencies = false,
dependenciesHead = false,
dependents = false,
dependentsDryRun = false,
silent,
Expand All @@ -234,6 +237,9 @@ export class ImportCmd implements Command {
if (!ids.length && dependencies) {
throw new BitError('you have to specify ids to use "--dependencies" flag');
}
if (!ids.length && dependenciesHead) {
throw new BitError('you have to specify ids to use "--dependencies-head" flag');
}
if (!ids.length && dependents) {
throw new BitError('you have to specify ids to use "--dependents" flag');
}
Expand Down Expand Up @@ -268,6 +274,7 @@ export class ImportCmd implements Command {
writeConfigFiles: !skipWriteConfigFiles,
saveInLane,
importDependenciesDirectly: dependencies,
importHeadDependenciesDirectly: dependenciesHead,
importDependents: dependents,
dependentsVia,
dependentsAll,
Expand Down