mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 11:03:53 +08:00
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
This commit is contained in:
parent
fca2d0f5bc
commit
ecf43090d1
@ -14,7 +14,9 @@ import { ProcessOutput, createConsoleLogger } from '@angular-devkit/core/node';
|
||||
import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
|
||||
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
|
||||
import * as ansiColors from 'ansi-colors';
|
||||
import { existsSync } from 'fs';
|
||||
import * as inquirer from 'inquirer';
|
||||
import * as path from 'path';
|
||||
import yargsParser, { camelCase, decamelize } from 'yargs-parser';
|
||||
|
||||
/**
|
||||
@ -108,6 +110,45 @@ function _createPromptProvider(): schema.PromptProvider {
|
||||
};
|
||||
}
|
||||
|
||||
function findUp(names: string | string[], from: string) {
|
||||
if (!Array.isArray(names)) {
|
||||
names = [names];
|
||||
}
|
||||
const root = path.parse(from).root;
|
||||
|
||||
let currentDir = from;
|
||||
while (currentDir && currentDir !== root) {
|
||||
for (const name of names) {
|
||||
const p = path.join(currentDir, name);
|
||||
if (existsSync(p)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
currentDir = path.dirname(currentDir);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* return package manager' name by lock file
|
||||
*/
|
||||
function getPackageManagerName() {
|
||||
// order by check priority
|
||||
const LOCKS: Record<string, string> = {
|
||||
'package-lock.json': 'npm',
|
||||
'yarn.lock': 'yarn',
|
||||
'pnpm-lock.yaml': 'pnpm',
|
||||
};
|
||||
const lockPath = findUp(Object.keys(LOCKS), process.cwd());
|
||||
if (lockPath) {
|
||||
return LOCKS[path.basename(lockPath)];
|
||||
}
|
||||
|
||||
return 'npm';
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
export async function main({
|
||||
args,
|
||||
@ -155,6 +196,7 @@ export async function main({
|
||||
dryRun,
|
||||
resolvePaths: [process.cwd(), __dirname],
|
||||
schemaValidation: true,
|
||||
packageManager: getPackageManagerName(),
|
||||
});
|
||||
|
||||
/** If the user wants to list schematics, we simply show all the schematic names. */
|
||||
|
Loading…
x
Reference in New Issue
Block a user