@@ -14,7 +14,9 @@ import { ProcessOutput, createConsoleLogger } from '@angular-devkit/core/node';
|
14 | 14 | import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
|
15 | 15 | import { NodeWorkflow } from '@angular-devkit/schematics/tools';
|
16 | 16 | import * as ansiColors from 'ansi-colors';
|
| 17 | +import { existsSync } from 'fs'; |
17 | 18 | import * as inquirer from 'inquirer';
|
| 19 | +import * as path from 'path'; |
18 | 20 | import yargsParser, { camelCase, decamelize } from 'yargs-parser';
|
19 | 21 |
|
20 | 22 | /**
|
@@ -108,6 +110,45 @@ function _createPromptProvider(): schema.PromptProvider {
|
108 | 110 | };
|
109 | 111 | }
|
110 | 112 |
|
| 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 | + |
111 | 152 | // eslint-disable-next-line max-lines-per-function
|
112 | 153 | export async function main({
|
113 | 154 | args,
|
@@ -155,6 +196,7 @@ export async function main({
|
155 | 196 | dryRun,
|
156 | 197 | resolvePaths: [process.cwd(), __dirname],
|
157 | 198 | schemaValidation: true,
|
| 199 | +packageManager: getPackageManagerName(), |
158 | 200 | });
|
159 | 201 |
|
160 | 202 | /** If the user wants to list schematics, we simply show all the schematic names. */
|
|
0 commit comments