|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import type { ChildProcess } from 'node:child_process'; |
| 5 | + |
| 6 | +import { Executable, FileSystem, JsonFile, type IPackageJson } from '@rushstack/node-core-library'; |
| 7 | +import type { ITerminal } from '@rushstack/terminal'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @public |
| 11 | + */ |
| 12 | +// eslint-disable-next-line @typescript-eslint/typedef |
| 13 | +export const VALID_PACKAGE_MANAGERS = ['npm', 'pnpm', 'yarn'] as const; |
| 14 | + |
| 15 | +/** |
| 16 | + * The package managers supported by the SPFx CLI. |
| 17 | + * |
| 18 | + * @public |
| 19 | + */ |
| 20 | +export type PackageManager = (typeof VALID_PACKAGE_MANAGERS)[number]; |
| 21 | + |
| 22 | +const VALID_PACKAGE_MANAGERS_SET: ReadonlySet<PackageManager> = new Set<PackageManager>( |
| 23 | + VALID_PACKAGE_MANAGERS |
| 24 | +); |
| 25 | + |
| 26 | +interface IPackageJsonWithEngines extends IPackageJson { |
| 27 | + engines?: Record<string, string>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Reads the project's `package.json` and returns the package manager recorded in its `"engines"` |
| 32 | + * field, or `undefined` if none is present or the file does not exist. |
| 33 | + * |
| 34 | + * @throws If multiple known package managers are found in the `"engines"` field. |
| 35 | + * |
| 36 | + * @public |
| 37 | + */ |
| 38 | +export async function tryReadPackageManagerFromPackageJsonEnginesAsync( |
| 39 | + targetDir: string |
| 40 | +): Promise<PackageManager | undefined> { |
| 41 | + const filePath: string = `${targetDir}/package.json`; |
| 42 | + let engines: IPackageJsonWithEngines['engines'] | undefined; |
| 43 | + try { |
| 44 | + const packageJson: IPackageJsonWithEngines = await JsonFile.loadAsync(filePath); |
| 45 | + ({ engines } = packageJson); |
| 46 | + } catch (error) { |
| 47 | + if (!FileSystem.isNotExistError(error)) { |
| 48 | + throw error; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (engines) { |
| 53 | + const foundPackageManagers: PackageManager[] = []; |
| 54 | + for (const engine of Object.keys(engines)) { |
| 55 | + const maybePackageManager: PackageManager = engine as PackageManager; |
| 56 | + if (VALID_PACKAGE_MANAGERS_SET.has(maybePackageManager)) { |
| 57 | + foundPackageManagers.push(maybePackageManager); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (foundPackageManagers.length > 1) { |
| 62 | + throw new Error( |
| 63 | + `Found multiple package managers in the "engines" field of package.json: ` + |
| 64 | + `${foundPackageManagers.join(', ')}. Only one package manager should be specified.` |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return foundPackageManagers[0]; |
| 69 | + } |
| 70 | + |
| 71 | + return undefined; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Detects the installed version of the given package manager, then writes a `>=MAJOR` constraint |
| 76 | + * into the `"engines"` field of the project's `package.json`. |
| 77 | + * |
| 78 | + * Emits a warning and skips silently on version-detection or file-read failures so that a missing |
| 79 | + * or undetectable package manager never fails the overall scaffold. |
| 80 | + * |
| 81 | + * @public |
| 82 | + */ |
| 83 | +export async function writePackageManagerToPackageJsonEnginesAsync( |
| 84 | + packageManager: PackageManager, |
| 85 | + targetDir: string, |
| 86 | + terminal: ITerminal |
| 87 | +): Promise<void> { |
| 88 | + const versionChild: ChildProcess = Executable.spawn(packageManager, ['--version'], { |
| 89 | + currentWorkingDirectory: targetDir, |
| 90 | + stdio: 'pipe' |
| 91 | + }); |
| 92 | + |
| 93 | + const { stdout } = await Executable.waitForExitAsync(versionChild, { |
| 94 | + throwOnNonZeroExitCode: false, |
| 95 | + throwOnSignal: false, |
| 96 | + encoding: 'utf-8' |
| 97 | + }); |
| 98 | + |
| 99 | + const majorVersion: number = parseInt(stdout.trim(), 10); |
| 100 | + if (Number.isNaN(majorVersion)) { |
| 101 | + terminal.writeWarningLine( |
| 102 | + `Could not detect ${packageManager} version; skipping engines field update in package.json.` |
| 103 | + ); |
| 104 | + } else { |
| 105 | + const filePath: string = `${targetDir}/package.json`; |
| 106 | + let packageJson: IPackageJsonWithEngines | undefined; |
| 107 | + try { |
| 108 | + packageJson = await JsonFile.loadAsync(filePath); |
| 109 | + } catch (error) { |
| 110 | + if (FileSystem.isNotExistError(error)) { |
| 111 | + terminal.writeWarningLine( |
| 112 | + `Could not find package.json in ${targetDir}; skipping engines field update.` |
| 113 | + ); |
| 114 | + } else { |
| 115 | + throw error; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (packageJson) { |
| 120 | + packageJson.engines ??= {}; |
| 121 | + packageJson.engines[packageManager] = `>=${majorVersion}`; |
| 122 | + await JsonFile.saveAsync(packageJson, filePath, { updateExistingFile: true }); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments