diff --git a/scopes/scope/importer/import-components.ts b/scopes/scope/importer/import-components.ts index 49cc7e7db0a7..8335a433ceaa 100644 --- a/scopes/scope/importer/import-components.ts +++ b/scopes/scope/importer/import-components.ts @@ -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; @@ -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); } @@ -467,8 +470,14 @@ 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[] { @@ -476,8 +485,14 @@ if you just want to get a quick look into this snap, create a new workspace and 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; diff --git a/scopes/scope/importer/import.cmd.ts b/scopes/scope/importer/import.cmd.ts index 8386979d822f..8d01740354d9 100644 --- a/scopes/scope/importer/import.cmd.ts +++ b/scopes/scope/importer/import.cmd.ts @@ -27,6 +27,7 @@ type ImportFlags = { filterEnvs?: string; saveInLane?: boolean; dependencies?: boolean; + dependenciesHead?: boolean; dependents?: boolean; dependentsDryRun?: boolean; dependentsVia?: string; @@ -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', @@ -211,6 +213,7 @@ export class ImportCmd implements Command { filterEnvs, saveInLane = false, dependencies = false, + dependenciesHead = false, dependents = false, dependentsDryRun = false, silent, @@ -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'); } @@ -268,6 +274,7 @@ export class ImportCmd implements Command { writeConfigFiles: !skipWriteConfigFiles, saveInLane, importDependenciesDirectly: dependencies, + importHeadDependenciesDirectly: dependenciesHead, importDependents: dependents, dependentsVia, dependentsAll,