@feflow/cli
Version:
A front-end flow tool.
416 lines • 20.6 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 __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 __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var path_1 = __importDefault(require("path"));
var glob_1 = __importDefault(require("glob"));
var report_1 = __importDefault(require("@feflow/report"));
var command_line_usage_1 = __importDefault(require("command-line-usage"));
var commander_1 = __importDefault(require("./commander"));
var hook_1 = __importDefault(require("./hook"));
var logger_1 = __importDefault(require("./logger"));
var command_picker_1 = __importStar(require("./command-picker"));
var resident_1 = require("./resident");
var load_plugins_1 = __importDefault(require("./plugin/load-plugins"));
var load_universal_plugin_1 = __importDefault(require("./plugin/load-universal-plugin"));
var load_devkits_1 = __importDefault(require("./devkit/load-devkits"));
var command_options_1 = __importDefault(require("./devkit/command-options"));
var binp_1 = __importDefault(require("./universal-pkg/binp"));
var pkg_1 = require("./universal-pkg/dep/pkg");
var constant_1 = require("../shared/constant");
var yaml_1 = require("../shared/yaml");
var fef_error_1 = require("../shared/fef-error");
var git_1 = require("../shared/git");
var fs_1 = require("../shared/fs");
var npm_1 = require("../shared/npm");
var type_predicates_1 = require("../shared/type-predicates");
var package_json_1 = __importDefault(require("../../package.json"));
var Feflow = /** @class */ (function () {
function Feflow(args) {
var _this = this;
this.root = constant_1.FEFLOW_HOME;
this.configPath = path_1.default.join(constant_1.FEFLOW_HOME, '.feflowrc.yml');
this.bin = path_1.default.join(constant_1.FEFLOW_HOME, constant_1.FEFLOW_BIN);
this.lib = path_1.default.join(constant_1.FEFLOW_HOME, constant_1.FEFLOW_LIB);
this.rootPkg = path_1.default.join(constant_1.FEFLOW_HOME, 'package.json');
this.loggerPath = path_1.default.join(constant_1.FEFLOW_HOME, constant_1.LOG_FILE);
this.universalPkgPath = path_1.default.join(constant_1.FEFLOW_HOME, constant_1.UNIVERSAL_PKG_JSON);
this.universalModules = path_1.default.join(constant_1.FEFLOW_HOME, constant_1.UNIVERSAL_MODULES);
this.args = args;
this.version = package_json_1.default.version;
var config = (0, yaml_1.parseYaml)(this.configPath);
(0, type_predicates_1.isValidConfig)(config) && (this.config = config) && (0, git_1.setServerUrl)(config.serverUrl);
this.hook = new hook_1.default();
this.commander = new commander_1.default(function (cmdName) {
_this.hook.emit(constant_1.HOOK_TYPE_ON_COMMAND_REGISTERED, cmdName);
});
this.logger = (0, logger_1.default)({
name: 'feflow-cli',
debug: Boolean(args.debug),
silent: Boolean(args.silent),
});
this.reporter = new report_1.default(this);
this.universalPkg = new pkg_1.UniversalPkg(this.universalPkgPath);
this.commandPick = null;
this.fefError = new fef_error_1.FefError(this);
}
Feflow.prototype.init = function (cmdName) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var disableCheck, error_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.reporter.init(cmdName);
return [4 /*yield*/, Promise.all([this.initClient(), this.initPackageManager(), this.initBinPath()])];
case 1:
_b.sent();
disableCheck = Boolean(this.args['disable-check']) || Boolean((_a = this.config) === null || _a === void 0 ? void 0 : _a.disableCheck);
if (!!disableCheck) return [3 /*break*/, 5];
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, (0, resident_1.checkUpdate)(this)];
case 3:
_b.sent();
return [3 /*break*/, 5];
case 4:
error_1 = _b.sent();
this.logger.error('check update error: ', error_1);
return [3 /*break*/, 5];
case 5:
this.commandPick = new command_picker_1.default(this, cmdName);
if (!this.commandPick.isAvailable()) return [3 /*break*/, 8];
// should hit the cache in most cases
this.logger.debug('find cmd in cache');
return [4 /*yield*/, this.commandPick.pickCommand()];
case 6:
_b.sent();
return [4 /*yield*/, this.loadCommands(command_picker_1.LOAD_DEVKIT)];
case 7:
_b.sent();
return [3 /*break*/, 11];
case 8:
// if not, load plugin/devkit/native in need
this.logger.debug('not find cmd in cache');
return [4 /*yield*/, this.loadCommands(command_picker_1.LOAD_ALL)];
case 9:
_b.sent();
// make sure the command has at least one function, otherwise replace to help command
return [4 /*yield*/, this.commandPick.checkCommand()];
case 10:
// make sure the command has at least one function, otherwise replace to help command
_b.sent();
_b.label = 11;
case 11: return [2 /*return*/];
}
});
});
};
Feflow.prototype.initClient = function () {
return __awaiter(this, void 0, void 0, function () {
var rootPkg, pkgInfo, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
rootPkg = this.rootPkg;
pkgInfo = null;
_a.label = 1;
case 1:
_a.trys.push([1, 4, , 5]);
return [4 /*yield*/, (0, fs_1.statAsync)(rootPkg)];
case 2:
_a.sent();
return [4 /*yield*/, (0, fs_1.readFileAsync)(rootPkg)];
case 3:
pkgInfo = _a.sent();
return [3 /*break*/, 5];
case 4:
e_1 = _a.sent();
return [3 /*break*/, 5];
case 5:
if (!!pkgInfo) return [3 /*break*/, 7];
return [4 /*yield*/, (0, fs_1.writeFileAsync)(rootPkg, JSON.stringify({
name: 'feflow-home',
version: '0.0.0',
private: true,
}, null, 2))];
case 6:
_a.sent();
_a.label = 7;
case 7: return [2 /*return*/];
}
});
});
};
Feflow.prototype.initBinPath = function () {
return __awaiter(this, void 0, void 0, function () {
var bin, stats, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
bin = this.bin;
_a.label = 1;
case 1:
_a.trys.push([1, 5, , 7]);
return [4 /*yield*/, (0, fs_1.statAsync)(bin)];
case 2:
stats = _a.sent();
if (!!stats.isDirectory()) return [3 /*break*/, 4];
return [4 /*yield*/, (0, fs_1.unlinkAsync)(bin)];
case 3:
_a.sent();
_a.label = 4;
case 4: return [3 /*break*/, 7];
case 5:
e_2 = _a.sent();
return [4 /*yield*/, (0, fs_1.mkdirAsync)(bin)];
case 6:
_a.sent();
return [3 /*break*/, 7];
case 7:
new binp_1.default().register(bin);
return [2 /*return*/];
}
});
});
};
Feflow.prototype.initPackageManager = function () {
var _this = this;
var _a = this, root = _a.root, logger = _a.logger;
return new Promise(function (resolve, reject) {
var _a;
if (!((_a = _this.config) === null || _a === void 0 ? void 0 : _a.packageManager)) {
var packageManagers = ['npm', 'tnpm', 'yarn', 'cnpm'];
var defaultPackageManager = packageManagers.find(function (packageManager) { return (0, npm_1.isInstalledPM)(packageManager); });
if (!defaultPackageManager) {
// 无包管理器直接结束
logger.error('You must installed a package manager');
return reject();
}
var configPath = path_1.default.join(root, '.feflowrc.yml');
var config = Object.assign({}, (0, yaml_1.parseYaml)(configPath), {
packageManager: defaultPackageManager,
});
(0, yaml_1.safeDump)(config, configPath);
_this.config = config;
}
else {
logger.debug('Use packageManager is: ', _this.config.packageManager);
}
resolve();
});
};
Feflow.prototype.loadNative = function () {
var _this = this;
var nativePath = path_1.default.join(__dirname, './native/*.js');
glob_1.default.sync(nativePath).forEach(function (file) {
(function () { return __awaiter(_this, void 0, void 0, function () {
var nativeCommandEntry;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(file)); })];
case 1:
nativeCommandEntry = _a.sent();
nativeCommandEntry.default(this);
return [2 /*return*/];
}
});
}); })();
});
};
Feflow.prototype.loadCommands = function (orderType) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.logger.debug('load order: ', orderType);
if (!((orderType & command_picker_1.LOAD_ALL) === command_picker_1.LOAD_ALL)) return [3 /*break*/, 2];
this.loadNative();
(0, load_universal_plugin_1.default)(this);
return [4 /*yield*/, (0, load_plugins_1.default)(this)];
case 1:
_a.sent();
(0, load_devkits_1.default)(this);
return [2 /*return*/];
case 2:
if (!((orderType & command_picker_1.LOAD_PLUGIN) === command_picker_1.LOAD_PLUGIN)) return [3 /*break*/, 4];
return [4 /*yield*/, (0, load_plugins_1.default)(this)];
case 3:
_a.sent();
_a.label = 4;
case 4:
if ((orderType & command_picker_1.LOAD_UNIVERSAL_PLUGIN) === command_picker_1.LOAD_UNIVERSAL_PLUGIN) {
(0, load_universal_plugin_1.default)(this);
}
if ((orderType & command_picker_1.LOAD_DEVKIT) === command_picker_1.LOAD_DEVKIT) {
(0, load_devkits_1.default)(this);
}
return [2 /*return*/];
}
});
});
};
Feflow.prototype.loadInternalPlugins = function () {
return __awaiter(this, void 0, void 0, function () {
var devToolPlugin, devToolPluginEntry, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
devToolPlugin = '@feflow/feflow-plugin-devtool';
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
this.logger.debug('Plugin loaded: %s', devToolPlugin);
return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(devToolPlugin)); })];
case 2:
devToolPluginEntry = _a.sent();
return [2 /*return*/, devToolPluginEntry.default(this)];
case 3:
err_1 = _a.sent();
this.fefError.printError({
error: err_1,
msg: 'internal plugin load failed: %s',
});
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
};
Feflow.prototype.invoke = function (cmdName, ctx) {
return __awaiter(this, void 0, void 0, function () {
var cmd;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.args.help && cmdName && cmdName !== 'help')) return [3 /*break*/, 2];
return [4 /*yield*/, this.showCommandOptionDescription(cmdName, ctx)];
case 1:
_a.sent();
_a.label = 2;
case 2:
cmd = this.commander.get(cmdName);
if (!cmd) return [3 /*break*/, 4];
this.logger.name = cmd.pluginName;
return [4 /*yield*/, cmd.runFn.call(this, ctx)];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
this.logger.debug("Command ' ".concat(cmdName, " ' has not been registered yet!"));
_a.label = 5;
case 5: return [2 /*return*/];
}
});
});
};
Feflow.prototype.showCommandOptionDescription = function (cmdName, ctx) {
return __awaiter(this, void 0, void 0, function () {
var registeredCommand, commandLine, sections, usage;
return __generator(this, function (_a) {
registeredCommand = ctx.commander.get(cmdName);
commandLine = [];
if (registeredCommand === null || registeredCommand === void 0 ? void 0 : registeredCommand.options) {
commandLine = (0, command_options_1.default)(registeredCommand.options, registeredCommand.desc, cmdName);
}
// 有副作用,暂无好方法改造
if (cmdName === 'help' && registeredCommand) {
return [2 /*return*/, registeredCommand.runFn.call(this, ctx)];
}
if (commandLine.length === 0) {
return [2 /*return*/];
}
sections = [];
sections.push.apply(sections, __spreadArray([], __read(commandLine), false));
usage = (0, command_line_usage_1.default)(sections);
console.info(usage);
return [2 /*return*/];
});
});
};
return Feflow;
}());
exports.default = Feflow;
//# sourceMappingURL=index.js.map