zcatalyst-cli
Version:
Command Line Tool for CATALYST
153 lines (152 loc) • 6.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCommand = void 0;
const ansi_colors_1 = require("ansi-colors");
const index_js_1 = __importDefault(require("./error/index.js"));
const runtime_store_1 = __importDefault(require("./runtime-store"));
const config = __importStar(require("./util_modules/config"));
const constants_1 = require("./util_modules/constants");
const env_1 = require("./util_modules/env");
const js_1 = require("./util_modules/js");
const logger_1 = require("./util_modules/logger");
const project_1 = require("./util_modules/project");
const shell_1 = require("./util_modules/shell");
function executeCommand(exeCommand, { moduleSource, feature }) {
return __awaiter(this, void 0, void 0, function* () {
const cwd = moduleSource || runtime_store_1.default.get('project.root');
const exeDir = (0, project_1.resolveProjectPath)(cwd);
(0, logger_1.info)();
(0, logger_1.info)(`Executing ${(0, ansi_colors_1.cyan)(feature)} script in ${(0, ansi_colors_1.underline)(exeDir)}`);
(0, logger_1.debug)('Executing command > ' + exeCommand);
(0, logger_1.info)(`> ${exeCommand}`);
const exitListeners = process.listeners('exit');
process.removeAllListeners('exit');
try {
yield new Promise((_res, _rej) => {
var _a, _b;
const scriptProcess = (0, shell_1.spawn)(exeCommand, [], {
cwd: exeDir,
shell: true,
stdio: ['ignore', 'pipe', 'pipe']
}).RAW();
(_a = scriptProcess.stdout) === null || _a === void 0 ? void 0 : _a.pipe(env_1.isVsCode ? logger_1.LogStreamFactory.getStream() : process.stdout, {
end: false
});
(_b = scriptProcess.stderr) === null || _b === void 0 ? void 0 : _b.pipe(env_1.isVsCode ? logger_1.LogStreamFactory.getStream() : process.stdout, {
end: false
});
scriptProcess.on('exit', (code) => {
if (code !== 0) {
return _rej(code);
}
_res(code);
});
scriptProcess.on('error', (_error) => _rej(_error));
});
}
catch (er) {
if (er) {
throw new index_js_1.default('The script exited with code greater than 0 or an error', {
exit: 0,
original: new Error(JSON.stringify(er)),
errorId: 'EXEC-SCRIPT-4',
arg: [(0, ansi_colors_1.bold)(feature), ansi_colors_1.italic.red(JSON.stringify(er, null, ' '))]
});
}
}
if ((0, env_1.isPrimaryShell)()) {
js_1.JS.forEach(exitListeners || [], (listener) => {
process.addListener('exit', listener);
});
}
});
}
exports.executeCommand = executeCommand;
exports.default = (commandName) => {
const featureByScript = commandName.split(':');
if (featureByScript.length !== 2) {
throw new index_js_1.default('Not a proper script command', {
exit: 1,
errorId: 'EXEC-SCRIPT-1',
arg: [ansi_colors_1.bold.red(commandName), (0, ansi_colors_1.bold)('feature:script')]
});
}
const configModule = config[(featureByScript[0] + 'Config')];
if (configModule === undefined) {
throw new index_js_1.default('Not a valid catayst feature', {
exit: 1,
errorId: 'EXEC-SCRIPT-2',
arg: [
(0, ansi_colors_1.bold)(commandName.replace(featureByScript[0], (0, ansi_colors_1.red)(featureByScript[0]))),
(0, ansi_colors_1.bold)(['* client', '* function'].join('\n'))
]
});
}
const exeTempCommand = configModule.script(featureByScript[1], null);
if (exeTempCommand === null) {
const availableScripts = configModule.script(undefined, false);
throw new index_js_1.default(featureByScript[1] + ' script is not available.', {
exit: 1,
errorId: 'EXEC-SCRIPT-3',
arg: [
ansi_colors_1.bold.red(featureByScript[1]),
(0, ansi_colors_1.bold)(constants_1.FILENAME.config),
(0, ansi_colors_1.bold)(availableScripts
? Object.keys(availableScripts)
.map((script) => '* ' + script)
.join('\n')
: '* None')
]
});
}
const featureSource = configModule.source();
const exeCommand = js_1.JS.chain(exeTempCommand)
.split(' ')
.map((arg) => {
if (arg.includes('${source}')) {
return arg.replace('${source}', featureSource);
}
return arg;
})
.join(' ')
.value();
return executeCommand(exeCommand, {
moduleSource: configModule.source(),
feature: featureByScript[1]
});
};