@rushstack/heft-config-file
Version:
Configuration file loader for @rushstack/heft
80 lines • 4.12 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 nodeJsPath from 'node:path';
import { FileSystem, PackageJsonLookup } from '@rushstack/node-core-library';
import { ConfigurationFileBase } from './ConfigurationFileBase';
/**
* @beta
*/
export class ProjectConfigurationFile extends ConfigurationFileBase {
constructor(options) {
super(options);
this.projectRelativeFilePath = options.projectRelativeFilePath;
}
/**
* Find and return a configuration file for the specified project, automatically resolving
* `extends` properties and handling rigged configuration files. Will throw an error if a configuration
* file cannot be found in the rig or project config folder.
*/
loadConfigurationFileForProject(terminal, projectPath, rigConfig) {
const projectConfigurationFilePath = this._getConfigurationFilePathForProject(projectPath);
return this._loadConfigurationFileInnerWithCache(terminal, projectConfigurationFilePath, PackageJsonLookup.instance.tryGetPackageFolderFor(projectPath), this._getRigConfigFallback(terminal, rigConfig));
}
/**
* Find and return a configuration file for the specified project, automatically resolving
* `extends` properties and handling rigged configuration files. Will throw an error if a configuration
* file cannot be found in the rig or project config folder.
*/
async loadConfigurationFileForProjectAsync(terminal, projectPath, rigConfig) {
const projectConfigurationFilePath = this._getConfigurationFilePathForProject(projectPath);
return await this._loadConfigurationFileInnerWithCacheAsync(terminal, projectConfigurationFilePath, PackageJsonLookup.instance.tryGetPackageFolderFor(projectPath), this._getRigConfigFallback(terminal, rigConfig));
}
/**
* This function is identical to {@link ProjectConfigurationFile.loadConfigurationFileForProject}, except
* that it returns `undefined` instead of throwing an error if the configuration file cannot be found.
*/
tryLoadConfigurationFileForProject(terminal, projectPath, rigConfig) {
try {
return this.loadConfigurationFileForProject(terminal, projectPath, rigConfig);
}
catch (e) {
if (FileSystem.isNotExistError(e)) {
return undefined;
}
throw e;
}
}
/**
* This function is identical to {@link ProjectConfigurationFile.loadConfigurationFileForProjectAsync}, except
* that it returns `undefined` instead of throwing an error if the configuration file cannot be found.
*/
async tryLoadConfigurationFileForProjectAsync(terminal, projectPath, rigConfig) {
try {
return await this.loadConfigurationFileForProjectAsync(terminal, projectPath, rigConfig);
}
catch (e) {
if (FileSystem.isNotExistError(e)) {
return undefined;
}
throw e;
}
}
_getConfigurationFilePathForProject(projectPath) {
return nodeJsPath.resolve(projectPath, this.projectRelativeFilePath);
}
_getRigConfigFallback(terminal, rigConfig) {
return rigConfig
? (resolvedConfigurationFilePathForLogging) => {
if (rigConfig.rigFound) {
const rigProfileFolder = rigConfig.getResolvedProfileFolder();
terminal.writeDebugLine(`Configuration file "${resolvedConfigurationFilePathForLogging}" does not exist. Attempting to load via rig ("${ConfigurationFileBase._formatPathForLogging(rigProfileFolder)}").`);
return nodeJsPath.resolve(rigProfileFolder, this.projectRelativeFilePath);
}
else {
terminal.writeDebugLine(`No rig found for "${ConfigurationFileBase._formatPathForLogging(rigConfig.projectFolderPath)}"`);
}
}
: undefined;
}
}
//# sourceMappingURL=ProjectConfigurationFile.js.map