UNPKG

@omni-door/cli

Version:

A tool set for set up the standard JS project

266 lines (265 loc) 16.2 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var commander_1 = __importDefault(require("commander")); var leven_1 = __importDefault(require("leven")); var chalk_1 = __importDefault(require("chalk")); var utils_1 = require("@omni-door/utils"); var commandDicts = { init: 'init', dev: 'dev', start: 'start', new: 'new', build: 'build', release: 'release' }; (function () { return __awaiter(this, void 0, void 0, function () { function getConfig(silent) { var _a; try { var ppkj = utils_1.requireCwd('./package.json', true); configFilePath = ((_a = ppkj === null || ppkj === void 0 ? void 0 : ppkj.omni) === null || _a === void 0 ? void 0 : _a.filePath) || configFilePath; config = utils_1.requireCwd(configFilePath, silent); } catch (e) { utils_1.logWarn(e); } } function checkConfig() { if (!config) { utils_1.logWarn("Please initialize project first or checking the \"" + configFilePath + "\" configuration file"); utils_1.logWarn("\u8BF7\u5148\u521D\u59CB\u5316\u9879\u76EE\u6216\u68C0\u67E5 \"" + configFilePath + "\" \u914D\u7F6E\u6587\u4EF6\u662F\u5426\u5B58\u5728\u95EE\u9898"); process.exit(0); } } function changeCWD(workPath) { try { process.chdir(workPath); var cwd = process.cwd(); utils_1.logInfo("The work path change to \"" + cwd + "\""); utils_1.logInfo("\u5DE5\u4F5C\u8DEF\u5F84\u53D8\u66F4\u4E3A \"" + cwd + "\""); } catch (err) { utils_1.logWarn("Please checking the \"" + workPath + "\" had existed"); utils_1.logWarn("\u5DE5\u4F5C\u8DEF\u5F84\u53D8\u66F4\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5 \"" + workPath + "\" \u662F\u5426\u5B58\u5728"); process.exit(0); } } var e_1, _a, initial, dev, start, newTpl, build, release, pkj, config, configFilePath; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, utils_1.nodeVersionCheck('10.13.0')]; case 1: _b.sent(); return [3 /*break*/, 3]; case 2: e_1 = _b.sent(); utils_1.logWarn(e_1); return [3 /*break*/, 3]; case 3: _a = require('./commands'), initial = _a.initial, dev = _a.dev, start = _a.start, newTpl = _a.newTpl, build = _a.build, release = _a.release; pkj = require('../../package.json'); config = null; configFilePath = './omni.config.js'; commander_1.default .version(pkj.version, '-v, --version') .name('omni') .usage('[command] [options]'); commander_1.default .command(commandDicts.init + " [strategy]") .option('-rb, --react_basic [name]', 'create a basic React SPA project') .option('-rs, --react_standard [name]', 'create a standard React SPA project') .option('-re, --react_entire [name]', 'create a most versatile React SPA project') .option('-rp, --react_pc [name]', 'create a React SPA project based on antd') .option('-vb, --vue_basic [name]', 'create a basic Vue SPA project') .option('-vs, --vue_standard [name]', 'create a standard Vue SPA project') .option('-ve, --vue_entire [name]', 'create a most versatile Vue SPA project') .option('-rS, --react_ssr [name]', 'create a React component library') .option('-rc, --react_components [name]', 'create a React component library') .option('-vc, --vue_components [name]', 'create a Vue component library') .option('-t, --toolkit [name]', 'create a toolkit project') .option('-n, --no-install', 'init project without install dependencies') .option('-P, --path <path>', 'the workpath for init the project') .description('initialize your project, [strategy] could be stable(default) or latest', { strategy: 'stable or latest', }) .usage('[strategy] [options]') .action(function (strategy, options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; utils_1.updateNotifier(pkj); var workPath = options.path; if (workPath) changeCWD(workPath); var CLITAG = (_c = (_b = (_a = pkj === null || pkj === void 0 ? void 0 : pkj.version) === null || _a === void 0 ? void 0 : _a.match) === null || _b === void 0 ? void 0 : _b.call(_a, /[a-zA-Z]+/g)) === null || _c === void 0 ? void 0 : _c[0]; var TPLTAG = (_f = (_e = (_d = pkj === null || pkj === void 0 ? void 0 : pkj.version) === null || _d === void 0 ? void 0 : _d.match) === null || _e === void 0 ? void 0 : _e.call(_d, /[0-9]+\.[0-9]+/g)) === null || _f === void 0 ? void 0 : _f[0]; var CLICURRENTVERSION = (_j = (_h = (_g = pkj === null || pkj === void 0 ? void 0 : pkj.version) === null || _g === void 0 ? void 0 : _g.match) === null || _h === void 0 ? void 0 : _h.call(_g, /[0-9]+\.[0-9]+\.[0-9]+/g)) === null || _j === void 0 ? void 0 : _j[0]; initial(strategy, options, { tplPkjTag: TPLTAG ? "~" + TPLTAG : 'latest', tplPkjParams: ["tag=" + (CLITAG || (CLICURRENTVERSION ? "~" + CLICURRENTVERSION : 'latest'))] }); }); commander_1.default .command(commandDicts.dev) .option('-p, --port <port>', 'start the dev-server according to the specified port') .option('-H, --hostname <host>', 'start the dev-server according to the specified hostname') .option('-P, --path <path>', 'the workpath for start the dev-server') .description('omni dev [-p <port>] [-H <host>] [-P <path>]', { port: 'The dev-server listen port.', host: 'The dev-server running hostname.', path: 'The cli workpath for running dev-server.' }) .action(function (options) { var workPath = options.path; if (workPath) changeCWD(workPath); getConfig(!!workPath); checkConfig(); utils_1.npmVersionCheck(pkj.name, pkj.version); dev(config, options); }); commander_1.default .command(commandDicts.start) .option('-p, --port <port>', 'start the prod-server according to the specified port') .option('-H, --hostname <host>', 'start the prod-server according to the specified hostname') .option('-P, --path <path>', 'the workpath for start the prod-server') .description('omni start [-p <port>] [-H <host>] [-P <path>]', { port: 'The prod-server listen port.', host: 'The prod-server running hostname.', path: 'The cli workpath for running prod-server.' }) .action(function (options) { var workPath = options.path; if (workPath) changeCWD(workPath); getConfig(!!workPath); checkConfig(); start(config, options); }); commander_1.default .command(commandDicts.new + " [name]") .option('-f, --function', 'create a React-Function-Component') .option('-c, --class', 'create a React-Class-Component') .option('-r, --render', 'create a Vue-Render-Function') .option('-s, --single', 'create a Vue-Single-File-Component') .option('-P, --path <path>', 'the workpath for create component') .description('omni new [name] [-f | -c] [-P <path>]', { name: 'The name of component.', }) .usage('[name] [options]') .action(function (componentName, options) { var _a, _b, _c; var workPath = options.path; if (workPath) changeCWD(workPath); getConfig(!!workPath); checkConfig(); utils_1.updateNotifier(pkj); var TPLTAG = (_c = (_b = (_a = pkj === null || pkj === void 0 ? void 0 : pkj.version) === null || _a === void 0 ? void 0 : _a.match) === null || _b === void 0 ? void 0 : _b.call(_a, /[0-9]\.[0-9]/g)) === null || _c === void 0 ? void 0 : _c[0]; newTpl(config, componentName, __assign(__assign({}, options), { tplPkjTag: TPLTAG })); }); commander_1.default .command(commandDicts.build) .option('-c, --config <path>', 'specify the path of config file') .option('-n, --no-verify', 'bypass all pre-check before building') .option('-P, --path <path>', 'the workpath for build project') .description('build your project according to the [omni.config.js]\'s build field') .action(function (buildTactic) { var workPath = buildTactic.path; if (workPath) changeCWD(workPath); getConfig(!!workPath); checkConfig(); utils_1.npmVersionCheck(pkj.name, pkj.version); build(config, buildTactic); }); commander_1.default .command(commandDicts.release) .option('-a, --automatic', 'auto-increase the version of iteration') .option('-i, --ignore', 'ignoring the version of iteration') .option('-m, --manual <version>', 'manual specify the version of iteration') .option('-t, --tag <tag>', 'the tag will add to npm-package') .option('-n, --no-verify', 'bypass all pre-check before release') .option('-P, --path <path>', 'the workpath for release project') .description('publish your project according to the [omni.config.js]\'s release field') .action(function (iterTactic) { var workPath = iterTactic.path; if (workPath) changeCWD(workPath); getConfig(!!workPath); checkConfig(); utils_1.updateNotifier(pkj); release(config, iterTactic); }); commander_1.default.arguments('<command>') .action(function (unknownCommand) { var availableCommands = commander_1.default.commands.map(function (cmd) { return cmd._name; }); var suggestion; availableCommands.forEach(function (cmd) { var isBestMatch = leven_1.default(cmd, unknownCommand) < leven_1.default(suggestion || '', unknownCommand); if (leven_1.default(cmd, unknownCommand) < 3 && isBestMatch) { suggestion = cmd; } }); utils_1.logErr("Unknown command " + chalk_1.default.bold("omni " + unknownCommand)); if (suggestion) { utils_1.logWarn("Try to " + chalk_1.default.underline(chalk_1.default.green("omni " + suggestion))); } }); commander_1.default.parse(process.argv); if (!commander_1.default.args.length) { commander_1.default.help(); } return [2 /*return*/]; } }); }); })();