@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
137 lines • 6.87 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RigPackageResolver = void 0;
const path = __importStar(require("path"));
const node_core_library_1 = require("@rushstack/node-core-library");
/**
* Rig resolves requested tools from the project's Heft rig.
*/
class RigPackageResolver {
constructor(options) {
this._packageJsonLookup = new node_core_library_1.PackageJsonLookup();
this._resolverCache = new Map();
this._buildFolder = options.buildFolder;
this._projectPackageJson = options.projectPackageJson;
this._rigConfig = options.rigConfig;
}
/**
* Rig resolve the path to a specific package.
*
* The following rules will apply when rig resolving a package:
* - If the local project has a devDependency (not regular or peer dependency) on the tool,
* that has highest precedence.
* - OTHERWISE if there is a rig.json file, then look at the rig's package.json. Does it have a
* regular dependency (not dev or peer dependency) on the tool? If yes, then
* resolve the tool from the rig package folder.
* - OTHERWISE try to resolve it from the current project.
*/
async resolvePackageAsync(packageName, terminal) {
const buildFolder = this._buildFolder;
const projectFolder = this._packageJsonLookup.tryGetPackageFolderFor(buildFolder);
if (!projectFolder) {
throw new Error(`Unable to find a package.json file for "${buildFolder}".`);
}
const cacheKey = `${projectFolder};${packageName}`;
let resolutionPromise = this._resolverCache.get(cacheKey);
if (!resolutionPromise) {
resolutionPromise = this._resolvePackageInnerAsync(packageName, terminal);
this._resolverCache.set(cacheKey, resolutionPromise);
}
return await resolutionPromise;
}
async _resolvePackageInnerAsync(toolPackageName, terminal) {
// See if the project has a devDependency on the package
if (this._projectPackageJson.devDependencies &&
this._projectPackageJson.devDependencies[toolPackageName]) {
try {
const resolvedPackageFolder = node_core_library_1.Import.resolvePackage({
packageName: toolPackageName,
baseFolderPath: this._buildFolder
});
terminal.writeVerboseLine(`Resolved ${JSON.stringify(toolPackageName)} as a direct devDependency of the project.`);
return resolvedPackageFolder;
}
catch (e) {
throw new Error(`${JSON.stringify(toolPackageName)} is listed as a direct devDependency of the project, but ` +
'could not be resolved. Have dependencies been installed?');
}
}
// See if the project rig has a regular dependency on the package
const rigConfiguration = this._rigConfig;
if (rigConfiguration.rigFound) {
const rigFolder = rigConfiguration.getResolvedProfileFolder();
const rigPackageJsonPath = this._packageJsonLookup.tryGetPackageJsonFilePathFor(rigFolder);
if (!rigPackageJsonPath) {
throw new Error('Unable to resolve the package.json file for the ' +
`${JSON.stringify(rigConfiguration.rigPackageName)} rig package.`);
}
const rigPackageJson = this._packageJsonLookup.loadNodePackageJson(rigPackageJsonPath);
if (rigPackageJson.dependencies && rigPackageJson.dependencies[toolPackageName]) {
try {
const resolvedPackageFolder = node_core_library_1.Import.resolvePackage({
packageName: toolPackageName,
baseFolderPath: path.dirname(rigPackageJsonPath)
});
terminal.writeVerboseLine(`Resolved ${JSON.stringify(toolPackageName)} as a dependency of the ` +
`${JSON.stringify(rigConfiguration.rigPackageName)} rig package.`);
return resolvedPackageFolder;
}
catch (e) {
throw new Error(`${JSON.stringify(toolPackageName)} is listed as a dependency of the ` +
`${JSON.stringify(rigConfiguration.rigPackageName)} rig package, but could not be resolved. ` +
'Have dependencies been installed?');
}
}
}
// Last attempt, try to resolve it from the current project using node resolution
try {
const resolvedPackageFolder = node_core_library_1.Import.resolvePackage({
packageName: toolPackageName,
baseFolderPath: this._buildFolder
});
terminal.writeVerboseLine(`Resolved ${JSON.stringify(toolPackageName)} from "${resolvedPackageFolder}".`);
return resolvedPackageFolder;
}
catch (e) {
throw new Error(`Unable to resolve ${JSON.stringify(toolPackageName)}. For more information on riggable ` +
'dependency resolution, see https://rushstack.io/pages/heft/rig_packages/#3-riggable-dependencies');
}
}
}
exports.RigPackageResolver = RigPackageResolver;
//# sourceMappingURL=RigPackageResolver.js.map