UNPKG

@zero-scripts/core

Version:

Framework for creating presets, configurations and extensions

45 lines 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseScript = void 0; function isLongParam(arg) { return arg.indexOf('--') === 0; } function isShortParam(arg) { return arg.indexOf('-') === 0 && !isLongParam(arg); } function isParam(arg) { return isShortParam(arg) || isLongParam(arg); } function hasScript(arg, namesOfScripts) { return namesOfScripts.includes(arg); } function parseScript(argv, definedScripts) { let name, options = {}, args = [], met = false, skipNextArg = false; const isScript = (arg) => hasScript(arg, definedScripts); for (const [i, arg] of argv.entries()) { if (skipNextArg) { skipNextArg = false; continue; } if (typeof arg === 'string' && isParam(arg)) { const paramKey = isShortParam(arg) ? arg.slice(1) : arg.slice(2); const nextArg = argv[i + 1]; const nextArgIsParamVal = nextArg && !isScript(nextArg) && !isParam(nextArg); skipNextArg = Boolean(nextArgIsParamVal); options[paramKey] = nextArgIsParamVal ? nextArg : true; } else if (!met && isScript(arg)) { met = true; name = arg; } else { args.push(arg); } } if (!met || !name || name.trim() === '') { return undefined; } return { name, args, options }; } exports.parseScript = parseScript; //# sourceMappingURL=parseScript.js.map