@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
192 lines • 8.53 kB
JavaScript
"use strict";
// 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.HeftConfiguration = void 0;
const path = __importStar(require("path"));
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const heft_config_file_1 = require("@rushstack/heft-config-file");
const rig_package_1 = require("@rushstack/rig-package");
const Constants_1 = require("../utilities/Constants");
const RigPackageResolver_1 = require("./RigPackageResolver");
/**
* @public
*/
class HeftConfiguration {
/**
* {@link HeftConfiguration.buildFolderPath} with all path separators converted to forward slashes.
*/
get slashNormalizedBuildFolderPath() {
if (!this._slashNormalizedBuildFolderPath) {
this._slashNormalizedBuildFolderPath = node_core_library_1.Path.convertToSlashes(this.buildFolderPath);
}
return this._slashNormalizedBuildFolderPath;
}
/**
* The path to the project's "config" folder.
*/
get projectConfigFolderPath() {
if (!this._projectConfigFolderPath) {
this._projectConfigFolderPath = path.join(this.buildFolderPath, Constants_1.Constants.projectConfigFolderName);
}
return this._projectConfigFolderPath;
}
/**
* The project's temporary folder.
*
* @remarks This folder exists at \<project root\>/temp. In general, this folder is used to store temporary
* output from tasks under task-specific subfolders, and is not intended to be directly written to.
* Instead, plugins should write to the directory provided by HeftTaskSession.taskTempFolderPath
*/
get tempFolderPath() {
if (!this._tempFolderPath) {
this._tempFolderPath = path.join(this.buildFolderPath, Constants_1.Constants.tempFolderName);
}
return this._tempFolderPath;
}
/**
* The rig.json configuration for this project, if present.
*/
get rigConfig() {
if (!this._rigConfig) {
throw new node_core_library_1.InternalError('The rigConfig cannot be accessed until HeftConfiguration.checkForRigAsync() has been called');
}
return this._rigConfig;
}
/**
* The rig package resolver, which can be used to rig-resolve a requested package.
*/
get rigPackageResolver() {
if (!this._rigPackageResolver) {
this._rigPackageResolver = new RigPackageResolver_1.RigPackageResolver({
buildFolder: this.buildFolderPath,
projectPackageJson: this.projectPackageJson,
rigConfig: this.rigConfig
});
}
return this._rigPackageResolver;
}
/**
* The Heft tool's package.json
*/
get heftPackageJson() {
return node_core_library_1.PackageJsonLookup.instance.tryLoadPackageJsonFor(__dirname);
}
/**
* The package.json of the project being built
*/
get projectPackageJson() {
return node_core_library_1.PackageJsonLookup.instance.tryLoadPackageJsonFor(this.buildFolderPath);
}
constructor({ terminalProvider, buildFolderPath, numberOfCores }) {
this._knownConfigurationFiles = new Map();
this.buildFolderPath = buildFolderPath;
this.terminalProvider = terminalProvider;
this.numberOfCores = numberOfCores;
this.globalTerminal = new terminal_1.Terminal(terminalProvider);
}
/**
* Performs the search for rig.json and initializes the `HeftConfiguration.rigConfig` object.
* @internal
*/
async _checkForRigAsync() {
if (!this._rigConfig) {
this._rigConfig = await rig_package_1.RigConfig.loadForProjectFolderAsync({
projectFolderPath: this.buildFolderPath
});
}
}
/**
* Attempts to load a riggable project configuration file using blocking, synchronous I/O.
* @param options - The options for the configuration file loader from `@rushstack/heft-config-file`. If invoking this function multiple times for the same file, reuse the same object.
* @param terminal - The terminal to log messages during configuration file loading.
* @returns The configuration file, or undefined if it could not be loaded.
*/
tryLoadProjectConfigurationFile(options, terminal) {
const loader = this._getConfigFileLoader(options);
return loader.tryLoadConfigurationFileForProject(terminal, this.buildFolderPath, this._rigConfig);
}
/**
* Attempts to load a riggable project configuration file using asynchronous I/O.
* @param options - The options for the configuration file loader from `@rushstack/heft-config-file`. If invoking this function multiple times for the same file, reuse the same object.
* @param terminal - The terminal to log messages during configuration file loading.
* @returns A promise that resolves to the configuration file, or undefined if it could not be loaded.
*/
async tryLoadProjectConfigurationFileAsync(options, terminal) {
const loader = this._getConfigFileLoader(options);
return loader.tryLoadConfigurationFileForProjectAsync(terminal, this.buildFolderPath, this._rigConfig);
}
/**
* @internal
*/
static initialize(options) {
const packageJsonPath = node_core_library_1.PackageJsonLookup.instance.tryGetPackageJsonFilePathFor(options.cwd);
let buildFolderPath;
if (packageJsonPath) {
buildFolderPath = path.dirname(packageJsonPath);
// On Windows it is possible for the drive letter in the CWD to be lowercase, but the normalized naming is uppercase
// Force it to always be uppercase for consistency.
buildFolderPath =
process.platform === 'win32'
? buildFolderPath.charAt(0).toUpperCase() + buildFolderPath.slice(1)
: buildFolderPath;
}
else {
throw new Error('No package.json file found. Are you in a project folder?');
}
const configuration = new HeftConfiguration({
...options,
buildFolderPath
});
return configuration;
}
_getConfigFileLoader(options) {
let entry = this._knownConfigurationFiles.get(options.projectRelativeFilePath);
if (!entry) {
entry = {
options: Object.freeze(options),
loader: new heft_config_file_1.ProjectConfigurationFile(options)
};
}
else if (options !== entry.options) {
throw new Error(`The project configuration file for ${options.projectRelativeFilePath} has already been loaded with different options. Please ensure that options object used to load the configuration file is the same referenced object in all calls.`);
}
return entry.loader;
}
}
exports.HeftConfiguration = HeftConfiguration;
//# sourceMappingURL=HeftConfiguration.js.map