|
1 | | -import {UsageError} from 'clipanion'; |
2 | | -import fs from 'fs'; |
3 | | -import path from 'path'; |
4 | | -import semverValid from 'semver/functions/valid'; |
5 | | - |
6 | | -import {PreparedPackageManagerInfo} from './Engine'; |
7 | | -import * as debugUtils from './debugUtils'; |
8 | | -import {NodeError} from './nodeUtils'; |
9 | | -import * as nodeUtils from './nodeUtils'; |
10 | | -import {Descriptor, isSupportedPackageManager} from './types'; |
| 1 | +import {UsageError} from 'clipanion'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import semverSatisfies from 'semver/functions/satisfies'; |
| 5 | +import semverValid from 'semver/functions/valid'; |
| 6 | +import {parseEnv} from 'util'; |
| 7 | + |
| 8 | +import type {PreparedPackageManagerInfo} from './Engine'; |
| 9 | +import * as debugUtils from './debugUtils'; |
| 10 | +import type {NodeError} from './nodeUtils'; |
| 11 | +import * as nodeUtils from './nodeUtils'; |
| 12 | +import {isSupportedPackageManager} from './types'; |
| 13 | +import type {LocalEnvFile, Descriptor} from './types'; |
11 | 14 |
|
12 | 15 | const nodeModulesRegExp = /[\\/]node_modules[\\/](@[^\\/]*[\\/])?([^@\\/][^\\/]*)$/; |
13 | 16 |
|
@@ -52,6 +55,43 @@ export function parseSpec(raw: unknown, source: string, {enforceExactVersion = t |
52 | 55 | }; |
53 | 56 | } |
54 | 57 |
|
| 58 | +type CorepackPackageJSON = { |
| 59 | + packageManager?: string; |
| 60 | + devEngines?: { packageManager?: DevEngineDependency }; |
| 61 | +}; |
| 62 | + |
| 63 | +interface DevEngineDependency { |
| 64 | + name: string; |
| 65 | + version: string; |
| 66 | +} |
| 67 | +function parsePackageJSON(packageJSONContent: CorepackPackageJSON, localEnv?: LocalEnvFile) { |
| 68 | + if (packageJSONContent.devEngines?.packageManager) { |
| 69 | + const {packageManager} = packageJSONContent.devEngines; |
| 70 | + |
| 71 | + if (Array.isArray(packageManager)) |
| 72 | + throw new UsageError(`Providing several package managers is currently not supported`); |
| 73 | + |
| 74 | + let {version} = packageManager; |
| 75 | + if (!version) |
| 76 | + throw new UsageError(`Providing no version nor ranger for package manager is currently not supported`); |
| 77 | + |
| 78 | + const localEnvKey = `COREPACK_DEV_ENGINES_${packageManager.name.toUpperCase()}`; |
| 79 | + const localEnvVersion = localEnv?.[localEnvKey]; |
| 80 | + if (localEnvVersion) { |
| 81 | + if (!semverSatisfies(localEnvVersion, version)) |
| 82 | + throw new UsageError(`Local env key ${localEnvKey} defines a value of ${localEnvVersion} which does not match the version defined in package.json devEngines.packageManager of ${version}`); |
| 83 | + |
| 84 | + debugUtils.log(`Using ${localEnvVersion} defined in .corepack.env`); |
| 85 | + version = localEnvVersion; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + return `${packageManager.name}@${version}`; |
| 90 | + } |
| 91 | + |
| 92 | + return packageJSONContent.packageManager; |
| 93 | +} |
| 94 | + |
55 | 95 | export async function setLocalPackageManager(cwd: string, info: PreparedPackageManagerInfo) { |
56 | 96 | const lookup = await loadSpec(cwd); |
57 | 97 |
|
@@ -84,6 +124,7 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> { |
84 | 124 | let selection: { |
85 | 125 | data: any; |
86 | 126 | manifestPath: string; |
| 127 | + localEnv?: LocalEnvFile; |
87 | 128 | } | null = null; |
88 | 129 |
|
89 | 130 | while (nextCwd !== currCwd && (!selection || !selection.data.packageManager)) { |
@@ -111,13 +152,24 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> { |
111 | 152 | if (typeof data !== `object` || data === null) |
112 | 153 | throw new UsageError(`Invalid package.json in ${path.relative(initialCwd, manifestPath)}`); |
113 | 154 |
|
114 | | - selection = {data, manifestPath}; |
| 155 | + let localEnv: LocalEnvFile | undefined; |
| 156 | + const envFilePath = path.join(currCwd, `.corepack.env`); |
| 157 | + debugUtils.log(`Checking ${envFilePath}`); |
| 158 | + try { |
| 159 | + localEnv = parseEnv(await fs.promises.readFile(envFilePath, `utf8`)) as LocalEnvFile; |
| 160 | + } catch (err) { |
| 161 | + if ((err as NodeError)?.code !== `ENOENT`) { |
| 162 | + throw err; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + selection = {data, manifestPath, localEnv}; |
115 | 167 | } |
116 | 168 |
|
117 | 169 | if (selection === null) |
118 | 170 | return {type: `NoProject`, target: path.join(initialCwd, `package.json`)}; |
119 | 171 |
|
120 | | - const rawPmSpec = selection.data.packageManager; |
| 172 | + const rawPmSpec = parsePackageJSON(selection.data, selection.localEnv); |
121 | 173 | if (typeof rawPmSpec === `undefined`) |
122 | 174 | return {type: `NoSpec`, target: selection.manifestPath}; |
123 | 175 |
|
|
0 commit comments