piral-cli
Version:
The standard CLI for creating and building a Piral instance or a Pilet.
153 lines • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withCommand = withCommand;
exports.withoutCommand = withoutCommand;
exports.withFlags = withFlags;
exports.wrapCommand = wrapCommand;
exports.beforeCommand = beforeCommand;
exports.afterCommand = afterCommand;
exports.withPiralRule = withPiralRule;
exports.withPiletRule = withPiletRule;
exports.withPatcher = withPatcher;
exports.withBundler = withBundler;
const common_1 = require("./common");
const commands_1 = require("./commands");
const bundler_1 = require("./bundler");
const rules_1 = require("./rules");
function findAll(commandName, cb) {
for (let i = commands_1.commands.all.length; i--;) {
const command = commands_1.commands.all[i];
if (command.name === commandName) {
cb(command, i);
}
}
}
function withCommand(command) {
commands_1.commands.all.push(command);
return this;
}
function withoutCommand(commandName) {
findAll(commandName, (_, i) => commands_1.commands.all.splice(i, 1));
return this;
}
function maybeWithFlags(commandName, setter) {
if (typeof setter === 'function') {
withFlags(commandName, setter);
}
}
function withFlags(commandName, setter) {
if (typeof commandName !== 'string') {
(0, common_1.log)('apiCommandNameInvalid_0200', 'flags');
}
else if (typeof setter !== 'function') {
(0, common_1.log)('apiArgumentInvalid_0201', 'setter', 'flags');
}
else {
findAll(commandName, (command) => {
const current = command.flags || ((x) => x);
command.flags = (argv) => current(setter(argv));
});
}
return this;
}
function wrapCommand(commandName, wrapper) {
if (typeof commandName !== 'string') {
(0, common_1.log)('apiCommandNameInvalid_0200', 'command');
}
else if (typeof wrapper !== 'function') {
(0, common_1.log)('apiArgumentInvalid_0201', 'wrapper', 'command');
}
else {
findAll(commandName, (command) => {
const current = command.run;
command.run = (args) => wrapper(args, current);
});
}
return this;
}
function beforeCommand(commandName, before) {
if (typeof commandName !== 'string') {
(0, common_1.log)('apiCommandNameInvalid_0200', 'before command');
}
else if (typeof before !== 'function') {
(0, common_1.log)('apiArgumentInvalid_0201', 'before', 'before command');
}
else {
wrapCommand(commandName, async (args, current) => {
await before(args);
await current(args);
});
}
return this;
}
function afterCommand(commandName, after) {
if (typeof commandName !== 'string') {
(0, common_1.log)('apiCommandNameInvalid_0200', 'after command');
}
else if (typeof after !== 'function') {
(0, common_1.log)('apiArgumentInvalid_0201', 'after', 'after command');
}
else {
wrapCommand(commandName, async (args, current) => {
await current(args);
await after(args);
});
}
return this;
}
function withPiralRule(name, run) {
if (typeof name !== 'string') {
(0, common_1.log)('apiValidateNameInvalid_0202', 'Piral');
}
else if (typeof run !== 'function') {
(0, common_1.log)('apiValidateRunInvalid_0203', 'Piral');
}
else {
(0, rules_1.addPiralRule)({ name, run });
}
return this;
}
function withPiletRule(name, run) {
if (typeof name !== 'string') {
(0, common_1.log)('apiValidateNameInvalid_0202', 'pilet');
}
else if (typeof run !== 'function') {
(0, common_1.log)('apiValidateRunInvalid_0203', 'pilet');
}
else {
(0, rules_1.addPiletRule)({ name, run });
}
return this;
}
function withPatcher(packageName, patch) {
if (typeof packageName !== 'string') {
(0, common_1.log)('apiPatchInvalid_0204', 'packageName');
}
else if (typeof patch !== 'function') {
(0, common_1.log)('apiPatchInvalid_0204', 'patch');
}
else {
(0, common_1.installPatch)(packageName, patch);
}
return this;
}
function withBundler(name, actions) {
if (typeof name !== 'string') {
(0, common_1.log)('apiBundlerInvalid_0206', 'bundlerName');
}
else if (typeof actions !== 'object') {
(0, common_1.log)('apiBundlerInvalid_0206', 'bundler');
}
else {
(0, bundler_1.setBundler)({
name,
actions,
});
maybeWithFlags('debug-piral', actions.debugPiral.flags);
maybeWithFlags('build-piral', actions.buildPiral.flags);
maybeWithFlags('debug-pilet', actions.debugPilet.flags);
maybeWithFlags('build-pilet', actions.buildPilet.flags);
}
return this;
}
//# sourceMappingURL=api.js.map