@dojo/cli
Version:
86 lines • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const globby = require("globby");
const path_1 = require("path");
const configurationHelper_1 = require("./configurationHelper");
const chalk_1 = require("chalk");
function isEjected(groupName, command) {
const config = configurationHelper_1.default.sandbox(groupName, command).get();
return config && config['ejected'];
}
exports.isEjected = isEjected;
/**
* Enumerate all the installed commands and return their absolute paths
* N.B. we return globby's promise (its not a native node Promise, but a 'pinky-promise' wrapper) - LOL
* @param config
* @returns {Promise<string []>} the paths of all installed commands
*/
function enumerateInstalledCommands(config, group) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { searchPrefixes } = config;
const builtins = ['version', 'init', 'eject', 'validate'];
const globPaths = searchPrefixes.reduce((globPaths, key) => {
const isBuiltin = builtins.some((c) => group === c);
key = group && !isBuiltin ? `${key}-${group}` : key;
return globPaths.concat(config.searchPaths.map((depPath) => path_1.resolve(depPath, `${key}-*`)));
}, []);
return globby(globPaths, { ignore: '**/*.{map,d.ts}' });
});
}
exports.enumerateInstalledCommands = enumerateInstalledCommands;
/**
* Enumerate all the builtIn commands and return their absolute paths
* @param config
* @returns {Promise<string []>} the paths of all builtIn commands
*/
function enumerateBuiltInCommands(config) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const builtInCommandParentDirGlob = path_1.join(config.builtInCommandLocation, '/*.js');
return globby(builtInCommandParentDirGlob, { ignore: '**/*.{map,d.ts}' });
});
}
exports.enumerateBuiltInCommands = enumerateBuiltInCommands;
/**
* Function to load commands given a search path and a load function. The load
* function is injected for the purposes of abstraction and testing.
* The commands are stored in a CommandsMap which uses a composite key of
* group-name to store the Command. Currently the first of each group is
* stored as the default command for that group.
*
* @param paths array of absolute paths to commands
* @param load The load function, this takes a path and loads it using the searchPrefix
* that it was pre-configured to look for.
* @returns Promise This function is async and returns a promise once all
* of the commands have been loaded.
*/
function loadCommands(paths, load) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const specialCommandsMap = new Map();
paths.forEach((path) => {
try {
const commandWrapper = load(path);
const { group, name } = commandWrapper;
if (!isEjected(group, name)) {
if (!specialCommandsMap.has(group)) {
commandWrapper.default = true;
specialCommandsMap.set(group, new Map());
}
const commandsMap = specialCommandsMap.get(group);
if (!specialCommandsMap.get(group).has(name)) {
commandsMap.set(name, commandWrapper);
}
}
}
catch (error) {
error.message = `${chalk_1.default.red(`Failed to load module ${path}`)}\n\nNested error:\n ${error.message}`;
reject(error);
}
});
resolve(specialCommandsMap);
});
});
}
exports.loadCommands = loadCommands;
//# sourceMappingURL=loadCommands.js.map