@vnxjs/service
Version:
Vnmf Service
106 lines • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.printHelpLog = exports.resolvePresetsOrPlugins = exports.mergePlugins = exports.convertPluginsToObject = exports.getPluginPath = exports.isNpmPkg = void 0;
const helper_1 = require("@vnxjs/helper");
const lodash_1 = require("lodash");
const path = require("path");
const resolve = require("resolve");
const isNpmPkg = name => !(/^(\.|\/)/.test(name));
exports.isNpmPkg = isNpmPkg;
function getPluginPath(pluginPath) {
if ((0, exports.isNpmPkg)(pluginPath) || path.isAbsolute(pluginPath))
return pluginPath;
throw new Error('plugin and preset Configure must be absolute path or package name');
}
exports.getPluginPath = getPluginPath;
function convertPluginsToObject(items) {
return () => {
const obj = {};
if (Array.isArray(items)) {
items.forEach(item => {
if (typeof item === 'string') {
const name = getPluginPath(item);
obj[name] = null;
}
else if (Array.isArray(item)) {
const name = getPluginPath(item[0]);
obj[name] = item[1];
}
});
}
return obj;
};
}
exports.convertPluginsToObject = convertPluginsToObject;
function mergePlugins(dist, src) {
return () => {
const srcObj = convertPluginsToObject(src)();
const distObj = convertPluginsToObject(dist)();
return (0, lodash_1.merge)(distObj, srcObj);
};
}
exports.mergePlugins = mergePlugins;
// getModuleDefaultExport
function resolvePresetsOrPlugins(root, args, type) {
return Object.keys(args).map(item => {
var _a;
let fPath;
try {
fPath = resolve.sync(item, {
basedir: root,
extensions: ['.js', '.ts']
});
}
catch (err) {
if ((_a = args[item]) === null || _a === void 0 ? void 0 : _a.backup) {
// If there is no project,You can use it. CLI Plugins in
fPath = args[item].backup;
}
else {
console.log(helper_1.chalk.red(`No dependencies found "${item}",Please install it in the project first.`));
process.exit(1);
}
}
return {
id: fPath,
path: fPath,
type,
opts: args[item] || {},
apply() {
try {
return (0, helper_1.getModuleDefaultExport)(require(fPath));
}
catch (error) {
console.error(error);
throw new Error(`Plugin Reliance "${item}" Loading Failed,Please check the plugin configuration`);
}
}
};
});
}
exports.resolvePresetsOrPlugins = resolvePresetsOrPlugins;
function supplementBlank(length) {
return Array(length).map(() => '').join(' ');
}
function printHelpLog(command, optionsList, synopsisList) {
console.log(`Usage: vnmf ${command} [options]`);
console.log();
console.log('Options:');
const keys = Array.from(optionsList.keys());
const maxLength = keys.reduce((v1, v2) => {
return v1.length > v2.length ? v1 : v2;
}).length + 3;
optionsList.forEach((v, k) => {
const supplementBlankLength = maxLength - k.length;
console.log(` ${k}${supplementBlank(supplementBlankLength)}${v}`);
});
if (synopsisList && synopsisList.size) {
console.log();
console.log('Synopsis:');
synopsisList.forEach(item => {
console.log(` $ ${item}`);
});
}
}
exports.printHelpLog = printHelpLog;
//# sourceMappingURL=index.js.map