@zero-scripts/core
Version:
Framework for creating presets, configurations and extensions
98 lines • 5.19 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const mri_1 = __importDefault(require("mri"));
const api_1 = require("../api");
const readZeroScriptsOptions_1 = require("../utils/readZeroScriptsOptions");
const WorkSpace_1 = require("../WorkSpace");
const getCLIMeta_1 = require("./getCLIMeta");
const getConfigurationMeta_1 = require("./getConfigurationMeta");
const types_1 = require("./types");
async function run(argv) {
const cliMeta = getCLIMeta_1.getCLIMeta(argv);
const configuration = readZeroScriptsOptions_1.readZeroScriptsOptions();
const configMeta = getConfigurationMeta_1.getConfigurationMeta(configuration);
if (configMeta.workspaceType === types_1.WorkspaceConfigurationType.WRONG) {
throw new Error(`Error: Unsupported [configuration.workspace] type "${typeof configuration.workspace}".`);
}
if (configMeta.workflowType === types_1.WorkflowConfigurationType.WRONG) {
throw new Error(`Error: Unsupported [configuration.workflow] type "${typeof configuration.workflow}".`);
}
if (configMeta.isWorkflowDefined &&
cliMeta.isWorkflowNamePassed &&
(configMeta.workflowType === types_1.WorkflowConfigurationType.ARRAY ||
configMeta.workflowType == types_1.WorkflowConfigurationType.OBJECT)) {
console.log(`Warning: Option "--workflow ${cliMeta.optionWorkflowName}" is ignored, because [configuration.workflow] is Array.`);
}
const workFlow = configMeta.isWorkflowDefined &&
configMeta.workflowType === types_1.WorkflowConfigurationType.MAPPED_OBJECTS
? configuration.workflow[cliMeta.optionWorkflowName || 'default']
: configuration.workflow;
if (!configMeta.isWorkspaceDefined) {
throw new Error('Error: Option "workspace" is not set. It\'s should be array or object and contains package names of plugins.');
}
const workSpaceName = workFlow && 'workspace' in workFlow
? workFlow.workspace
: cliMeta.optionWorkspaceName;
if (cliMeta.isWorkspaceNamePassed && cliMeta.isWorkflowNamePassed) {
throw new Error(`Error: Can't use "--workflow ${cliMeta.optionWorkflowName}" and "--workspace ${cliMeta.optionWorkspaceName}" options together.`);
}
if (configMeta.workspaceType === types_1.WorkspaceConfigurationType.ARRAY &&
cliMeta.isWorkspaceNamePassed) {
console.log(`Warning: Passed option "--workspace ${workSpaceName}" is ignored, because [configuration.workspace] is already array.`);
}
const pluginPackageNames = configMeta.workspaceType === types_1.WorkspaceConfigurationType.ARRAY
? configuration.workspace
: configuration
.workspace[workSpaceName || 'default'];
if (!pluginPackageNames && !workSpaceName) {
throw new Error('Error: You should provide --workspace option with name of workspace, which should be loaded.');
}
const workSpaceInstance = new WorkSpace_1.WorkSpace('default');
const plugins = pluginPackageNames.map(pluginPackageName => {
const PluginClass = require(pluginPackageName).default;
const plugin = new PluginClass();
workSpaceInstance.plugins.push(plugin);
return plugin;
});
const pluginAPI = new api_1.PluginAPI(workSpaceInstance);
plugins.forEach(plugin => {
plugin.apply(pluginAPI);
});
workSpaceInstance.hooks.beforeRun.call(new api_1.WorkspaceBeforeRunAPI(workSpaceInstance));
const taskQueue = workFlow
? Array.isArray(workFlow)
? workFlow
: workFlow.flow
: cliMeta.args;
for (const taskString of taskQueue) {
const taskArgv = taskString.split(' ');
const _a = mri_1.default(taskArgv), { _: args } = _a, options = __rest(_a, ["_"]);
const [taskName, ...restArgs] = args;
const task = workSpaceInstance.tasks.get(taskName);
// TODO: check this before running queue
if (!task) {
throw new Error(`Error: Task "${taskName}" is not exists in your workspace "${workSpaceName}".`);
}
const useCommonArgsAndOptions = Object.keys(options).length === 0 && restArgs.length === 0;
const finalArgs = useCommonArgsAndOptions ? cliMeta.args : restArgs;
const finalOptions = useCommonArgsAndOptions ? cliMeta.options : options;
await task.run(finalArgs, finalOptions);
}
}
exports.run = run;
//# sourceMappingURL=run.js.map