@rushstack/heft-config-file
Version:
Configuration file loader for @rushstack/heft
117 lines • 5.82 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.ProjectConfigurationFile = void 0;
const nodeJsPath = __importStar(require("node: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, node_core_library_1.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, node_core_library_1.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 (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;
}
}
_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_1.ConfigurationFileBase._formatPathForLogging(rigProfileFolder)}").`);
return nodeJsPath.resolve(rigProfileFolder, this.projectRelativeFilePath);
}
else {
terminal.writeDebugLine(`No rig found for "${ConfigurationFileBase_1.ConfigurationFileBase._formatPathForLogging(rigConfig.projectFolderPath)}"`);
}
}
: undefined;
}
}
exports.ProjectConfigurationFile = ProjectConfigurationFile;
//# sourceMappingURL=ProjectConfigurationFile.js.map