@zohodesk/client_build_tool
Version:
A CLI tool to build web applications and client libraries
74 lines (55 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.route = route;
exports.runCommand = runCommand;
var _fs = require("fs");
var _path = require("path");
var _allCommandsConfigs = _interopRequireDefault(require("./allCommandsConfigs"));
var _logger = require("./logger");
var _readOptions = require("./schemas/readOptions");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function runCommand(commandName, options) {
// eslint-disable-next-line no-use-before-define
const commandConfig = findCommandConfig(commandName);
if (!commandConfig) {
return;
}
const {
dirname
} = commandConfig;
function runRespectiveFile(name, data) {
const commandExecutorPath = (0, _path.join)(dirname, `${name}.js`);
if ((0, _fs.existsSync)(commandExecutorPath)) {
// eslint-disable-next-line import/no-dynamic-require, global-require
const commandExecutor = require(commandExecutorPath);
return (commandExecutor[name] || commandExecutor.default)(data);
}
(0, _logger.verboseLogger)('file not found', commandExecutorPath);
return null;
}
const processedOption = runRespectiveFile('optionsProcessor', options) || options;
runRespectiveFile('deprecationHandler', processedOption); // runRespectiveFile("errorHandler", () => {
runRespectiveFile('preProcessor', processedOption);
runRespectiveFile('commandExecutor', processedOption);
runRespectiveFile('postProcessor', processedOption); // });
}
function route(commandName) {
try {
const options = (0, _readOptions.getOptions)();
runCommand(commandName, options);
} catch (error) {
(0, _logger.errorLogger)(error);
}
}
function findCommandConfig(option) {
// eslint-disable-next-line no-restricted-syntax
for (const commandConfig of _allCommandsConfigs.default) {
if (commandConfig.name === option || commandConfig.alias.includes(option)) {
return commandConfig;
}
}
(0, _logger.warnLogger)('Un known command', option, 'please check the document');
return null;
}