@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
177 lines • 7.51 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.HeftTaskPluginDefinition = exports.HeftLifecyclePluginDefinition = exports.HeftPluginDefinitionBase = void 0;
const path = __importStar(require("path"));
const node_core_library_1 = require("@rushstack/node-core-library");
class HeftPluginDefinitionBase {
constructor(options) {
this._heftPluginDefinitionJson = options.heftPluginDefinitionJson;
this._pluginPackageName = options.packageName;
this._resolvedEntryPoint = path.resolve(options.packageRoot, this._heftPluginDefinitionJson.entryPoint);
// Ensure that the plugin parameters are unique
const seenParameters = new Set();
for (const parameter of this.pluginParameters) {
if (seenParameters.has(parameter.longName)) {
throw new Error(`Parameter ${JSON.stringify(parameter.longName)} is defined multiple times by the providing ` +
`plugin ${JSON.stringify(this.pluginName)} in package ` +
`${JSON.stringify(this.pluginPackageName)}.`);
}
seenParameters.add(parameter.longName);
}
// Unfortunately loading the schema is a synchronous process.
if (options.heftPluginDefinitionJson.optionsSchema) {
const resolvedSchemaPath = path.resolve(options.packageRoot, options.heftPluginDefinitionJson.optionsSchema);
this._optionsSchema = node_core_library_1.JsonSchema.fromFile(resolvedSchemaPath);
}
}
/**
* The package name containing the target plugin.
*/
get pluginPackageName() {
return this._pluginPackageName;
}
/**
* The name of the target plugin.
*/
get pluginName() {
return this._heftPluginDefinitionJson.pluginName;
}
/**
* The resolved entry point to the plugin.
*/
get entryPoint() {
return this._resolvedEntryPoint;
}
/**
* The scope for all parameters defined by this plugin.
*/
get pluginParameterScope() {
// Default to the plugin name for the parameter scope. Plugin names should be unique within any run
// of Heft. Additionally, plugin names have the same naming restrictions as parameter scopes so can
// be used without modification.
return this._heftPluginDefinitionJson.parameterScope || this.pluginName;
}
/**
* The parameters that are defined for this plugin.
*/
get pluginParameters() {
return this._heftPluginDefinitionJson.parameters || [];
}
/**
* Load the plugin associated with the definition.
*/
async loadPluginAsync(logger) {
// Do not memoize the plugin here, since we want a new instance of the plugin each time it is loaded
// from the definition
let heftPlugin;
const entryPointPath = this.entryPoint;
try {
const loadedPluginModule = await Promise.resolve(`${entryPointPath}`).then(s => __importStar(require(s)));
const heftPluginConstructor = loadedPluginModule.default || loadedPluginModule;
heftPlugin = new heftPluginConstructor();
}
catch (e) {
const error = e;
if (error.message === 'heftPluginConstructor is not a constructor') {
// Common error scenario, give a more helpful error message
throw new Error(`Could not load plugin from "${entryPointPath}": The target module does not ` +
'export a plugin class with a parameterless constructor.');
}
else {
throw new node_core_library_1.InternalError(`Could not load plugin from "${entryPointPath}": ${error}`);
}
}
if (!heftPlugin) {
throw new node_core_library_1.InternalError(`Plugin ${JSON.stringify(this.pluginName)} loaded from "${entryPointPath}" is null or undefined.`);
}
logger.terminal.writeVerboseLine(`Loaded plugin from "${entryPointPath}"`);
if (typeof heftPlugin.apply !== 'function') {
throw new node_core_library_1.InternalError(`The plugin ${JSON.stringify(this.pluginName)} loaded from "${entryPointPath}" ` +
'doesn\'t define an "apply" function.');
}
return heftPlugin;
}
/**
* Validate the provided plugin options against the plugin's options schema, if one is provided.
*/
validateOptions(options) {
if (this._optionsSchema) {
try {
this._optionsSchema.validateObject(options || {}, '');
}
catch (error) {
throw new Error(`Provided options for plugin ${JSON.stringify(this.pluginName)} did not match the provided ` +
`plugin schema.\n${error}`);
}
}
}
}
exports.HeftPluginDefinitionBase = HeftPluginDefinitionBase;
class HeftLifecyclePluginDefinition extends HeftPluginDefinitionBase {
/**
* Load a lifecycle plugin definition given the provided plugin definition options.
*/
static loadFromObject(options) {
return new HeftLifecyclePluginDefinition(options);
}
/**
* {@inheritDoc HeftPluginDefinitionBase.loadPluginAsync}
* @override
*/
loadPluginAsync(logger) {
return super.loadPluginAsync(logger);
}
}
exports.HeftLifecyclePluginDefinition = HeftLifecyclePluginDefinition;
class HeftTaskPluginDefinition extends HeftPluginDefinitionBase {
/**
* Load a task plugin definition given the provided plugin definition options.
*/
static loadFromObject(options) {
return new HeftTaskPluginDefinition(options);
}
/**
* {@inheritDoc HeftPluginDefinitionBase.loadPluginAsync}
* @override
*/
loadPluginAsync(logger) {
return super.loadPluginAsync(logger);
}
}
exports.HeftTaskPluginDefinition = HeftTaskPluginDefinition;
//# sourceMappingURL=HeftPluginDefinition.js.map