Skip to content

Commit ecf4309

Browse files
authored
feat(@angular-devkit/schematics-cli): auto detect package manager (#24305)
* feat(@angular-devkit/schematics-cli): auto detect package manager * refactor(@angular-devkit/schematics-cli): code formatting * refactor(@angular-devkit/schematics-cli): linting
1 parent fca2d0f commit ecf4309

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

packages/angular_devkit/schematics_cli/bin/schematics.ts

+42
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { ProcessOutput, createConsoleLogger } from '@angular-devkit/core/node';
1414
import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
1515
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
1616
import * as ansiColors from 'ansi-colors';
17+
import { existsSync } from 'fs';
1718
import * as inquirer from 'inquirer';
19+
import * as path from 'path';
1820
import yargsParser, { camelCase, decamelize } from 'yargs-parser';
1921

2022
/**
@@ -108,6 +110,45 @@ function _createPromptProvider(): schema.PromptProvider {
108110
};
109111
}
110112

113+
function findUp(names: string | string[], from: string) {
114+
if (!Array.isArray(names)) {
115+
names = [names];
116+
}
117+
const root = path.parse(from).root;
118+
119+
let currentDir = from;
120+
while (currentDir && currentDir !== root) {
121+
for (const name of names) {
122+
const p = path.join(currentDir, name);
123+
if (existsSync(p)) {
124+
return p;
125+
}
126+
}
127+
128+
currentDir = path.dirname(currentDir);
129+
}
130+
131+
return null;
132+
}
133+
134+
/**
135+
* return package manager' name by lock file
136+
*/
137+
function getPackageManagerName() {
138+
// order by check priority
139+
const LOCKS: Record<string, string> = {
140+
'package-lock.json': 'npm',
141+
'yarn.lock': 'yarn',
142+
'pnpm-lock.yaml': 'pnpm',
143+
};
144+
const lockPath = findUp(Object.keys(LOCKS), process.cwd());
145+
if (lockPath) {
146+
return LOCKS[path.basename(lockPath)];
147+
}
148+
149+
return 'npm';
150+
}
151+
111152
// eslint-disable-next-line max-lines-per-function
112153
export async function main({
113154
args,
@@ -155,6 +196,7 @@ export async function main({
155196
dryRun,
156197
resolvePaths: [process.cwd(), __dirname],
157198
schemaValidation: true,
199+
packageManager: getPackageManagerName(),
158200
});
159201

160202
/** If the user wants to list schematics, we simply show all the schematic names. */

0 commit comments

Comments
 (0)