@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
155 lines • 6.83 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { PackageJsonLookup, InternalError, Path } from '@rushstack/node-core-library';
import { Terminal } from '@rushstack/terminal';
import { ProjectConfigurationFile } from '@rushstack/heft-config-file';
import { RigConfig } from '@rushstack/rig-package';
import { Constants } from '../utilities/Constants';
import { RigPackageResolver } from './RigPackageResolver';
/**
* @public
*/
export class HeftConfiguration {
/**
* {@link HeftConfiguration.buildFolderPath} with all path separators converted to forward slashes.
*/
get slashNormalizedBuildFolderPath() {
if (!this._slashNormalizedBuildFolderPath) {
this._slashNormalizedBuildFolderPath = 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.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.tempFolderName);
}
return this._tempFolderPath;
}
/**
* The rig.json configuration for this project, if present.
*/
get rigConfig() {
if (!this._rigConfig) {
throw new 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({
buildFolder: this.buildFolderPath,
projectPackageJson: this.projectPackageJson,
rigConfig: this.rigConfig
});
}
return this._rigPackageResolver;
}
/**
* The Heft tool's package.json
*/
get heftPackageJson() {
return PackageJsonLookup.instance.tryLoadPackageJsonFor(__dirname);
}
/**
* The package.json of the project being built
*/
get projectPackageJson() {
return 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(terminalProvider);
}
/**
* Performs the search for rig.json and initializes the `HeftConfiguration.rigConfig` object.
* @internal
*/
async _checkForRigAsync() {
if (!this._rigConfig) {
this._rigConfig = await 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 = 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 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;
}
}
//# sourceMappingURL=HeftConfiguration.js.map