@rushstack/heft-config-file
Version:
Configuration file loader for @rushstack/heft
135 lines • 6.68 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectConfigurationFile = void 0;
const nodeJsPath = __importStar(require("path"));
const node_core_library_1 = require("@rushstack/node-core-library");
const ConfigurationFileBase_1 = require("./ConfigurationFileBase");
/**
* @beta
*/
class ProjectConfigurationFile extends ConfigurationFileBase_1.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, new Set(), 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, new Set(), 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 (node_core_library_1.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 (node_core_library_1.FileSystem.isNotExistError(e)) {
return undefined;
}
throw e;
}
}
_tryLoadConfigurationFileInRig(terminal, rigConfig, visitedConfigurationFilePaths) {
if (rigConfig.rigFound) {
const rigProfileFolder = rigConfig.getResolvedProfileFolder();
try {
return this._loadConfigurationFileInnerWithCache(terminal, nodeJsPath.resolve(rigProfileFolder, this.projectRelativeFilePath), visitedConfigurationFilePaths, undefined);
}
catch (e) {
// Ignore cases where a configuration file doesn't exist in a rig
if (!node_core_library_1.FileSystem.isNotExistError(e)) {
throw e;
}
else {
terminal.writeDebugLine(`Configuration file "${this.projectRelativeFilePath}" not found in rig ("${ConfigurationFileBase_1.ConfigurationFileBase._formatPathForLogging(rigProfileFolder)}")`);
}
}
}
else {
terminal.writeDebugLine(`No rig found for "${ConfigurationFileBase_1.ConfigurationFileBase._formatPathForLogging(rigConfig.projectFolderPath)}"`);
}
return undefined;
}
async _tryLoadConfigurationFileInRigAsync(terminal, rigConfig, visitedConfigurationFilePaths) {
if (rigConfig.rigFound) {
const rigProfileFolder = await rigConfig.getResolvedProfileFolderAsync();
try {
return await this._loadConfigurationFileInnerWithCacheAsync(terminal, nodeJsPath.resolve(rigProfileFolder, this.projectRelativeFilePath), visitedConfigurationFilePaths, undefined);
}
catch (e) {
// Ignore cases where a configuration file doesn't exist in a rig
if (!node_core_library_1.FileSystem.isNotExistError(e)) {
throw e;
}
else {
terminal.writeDebugLine(`Configuration file "${this.projectRelativeFilePath}" not found in rig ("${ConfigurationFileBase_1.ConfigurationFileBase._formatPathForLogging(rigProfileFolder)}")`);
}
}
}
else {
terminal.writeDebugLine(`No rig found for "${ConfigurationFileBase_1.ConfigurationFileBase._formatPathForLogging(rigConfig.projectFolderPath)}"`);
}
return undefined;
}
_getConfigurationFilePathForProject(projectPath) {
return nodeJsPath.resolve(projectPath, this.projectRelativeFilePath);
}
}
exports.ProjectConfigurationFile = ProjectConfigurationFile;
//# sourceMappingURL=ProjectConfigurationFile.js.map