MCPcopy Index your code
hub / github.com/nodejs/corepack / findProjectSpec

Method findProjectSpec

sources/Engine.ts:247–314  ·  view source on GitHub ↗

* Locates the active project's package manager specification. * * If the specification exists but doesn't match the active package manager, * an error is thrown to prevent users from using the wrong package manager, * which would lead to inconsistent project layouts. * * If the pro

(initialCwd: string, locator: Locator | LazyLocator, {transparent = false, binaryVersion}: {transparent?: boolean, binaryVersion?: string | null} = {})

Source from the content-addressed store, hash-verified

245 * don't need to ask again in the future.
246 */
247 async findProjectSpec(initialCwd: string, locator: Locator | LazyLocator, {transparent = false, binaryVersion}: {transparent?: boolean, binaryVersion?: string | null} = {}): Promise<Descriptor> {
248 // A locator is a valid descriptor (but not the other way around)
249 const fallbackDescriptor = {name: locator.name, range: `${locator.reference}`};
250
251 if (process.env.COREPACK_ENABLE_PROJECT_SPEC === `0`) {
252 if (typeof locator.reference === `function`)
253 fallbackDescriptor.range = await locator.reference();
254
255 return fallbackDescriptor;
256 }
257
258 if (process.env.COREPACK_ENABLE_STRICT === `0`)
259 transparent = true;
260
261 while (true) {
262 const result = await specUtils.loadSpec(initialCwd);
263
264 switch (result.type) {
265 case `NoProject`: {
266 if (typeof locator.reference === `function`)
267 fallbackDescriptor.range = await locator.reference();
268
269 debugUtils.log(`Falling back to ${fallbackDescriptor.name}@${fallbackDescriptor.range} as no project manifest were found`);
270 return fallbackDescriptor;
271 }
272
273 case `NoSpec`: {
274 if (typeof locator.reference === `function`)
275 fallbackDescriptor.range = await locator.reference();
276
277 if (process.env.COREPACK_ENABLE_AUTO_PIN === `1`) {
278 const resolved = await this.resolveDescriptor(fallbackDescriptor, {allowTags: true});
279 if (resolved === null)
280 throw new UsageError(`Failed to successfully resolve '${fallbackDescriptor.range}' to a valid ${fallbackDescriptor.name} release`);
281
282 const installSpec = await this.ensurePackageManager(resolved);
283
284 console.error(`! The local project doesn't define a 'packageManager' field. Corepack will now add one referencing ${installSpec.locator.name}@${installSpec.locator.reference}.`);
285 console.error(`! For more details about this field, consult the documentation at https://nodejs.org/api/packages.html#packagemanager`);
286 console.error();
287
288 await specUtils.setLocalPackageManager(path.dirname(result.target), installSpec);
289 }
290
291 debugUtils.log(`Falling back to ${fallbackDescriptor.name}@${fallbackDescriptor.range} in the absence of "packageManager" field in ${result.target}`);
292 return fallbackDescriptor;
293 }
294
295 case `Found`: {
296 const spec = result.getSpec({enforceExactVersion: !binaryVersion});
297 if (spec.name !== locator.name) {
298 if (transparent) {
299 if (typeof locator.reference === `function`)
300 fallbackDescriptor.range = await locator.reference();
301
302 debugUtils.log(`Falling back to ${fallbackDescriptor.name}@${fallbackDescriptor.range} in a ${spec.name}@${spec.range} project`);
303 return fallbackDescriptor;
304 } else {

Callers 1

Calls 3

resolveDescriptorMethod · 0.95
ensurePackageManagerMethod · 0.95
logMethod · 0.80

Tested by

no test coverage detected