@feflow/cli
Version:
A front-end flow tool.
948 lines • 47.5 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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateUniversalPlugin = exports.PkgInfo = exports.getPkgInfo = exports.installPlugin = exports.getRepoInfo = void 0;
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var axios_1 = __importDefault(require("axios"));
var git_1 = require("../universal-pkg/repository/git");
var linker_1 = __importDefault(require("../universal-pkg/linker"));
var version_1 = __importDefault(require("../universal-pkg/dep/version"));
var apply_plugins_1 = __importStar(require("../plugin/apply-plugins"));
var command_picker_1 = require("../command-picker");
var package_json_1 = __importDefault(require("../../shared/package-json"));
var git_2 = require("../../shared/git");
var constant_1 = require("../../shared/constant");
var npm_1 = require("../../shared/npm");
var url_1 = require("../../shared/url");
var fs_2 = require("../../shared/fs");
exports.default = (function (ctx) {
ctx.commander.register('install', 'Install a devkit or plugin', function () { return __awaiter(void 0, void 0, void 0, function () {
var dependencies, installPluginStr, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
dependencies = ctx.args._;
installPluginStr = dependencies[0];
if (!installPluginStr) {
ctx.logger.error('parameter error');
return [2 /*return*/];
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, installPlugin(ctx, installPluginStr, true)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
ctx.logger.error('install error: ', e_1);
process.exit(2);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
ctx.commander.register('uninstall', 'Uninstall a devkit or plugin', function () { return __awaiter(void 0, void 0, void 0, function () {
var dependencies, serverUrl, universalPkg, installPluginStr, pkgInfo, pickerConfig;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
dependencies = ctx.args._;
ctx.logger.info('Uninstalling packages. This might take a couple of minutes.');
serverUrl = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.serverUrl;
if (!serverUrl) {
return [2 /*return*/, uninstallNpmPlugin(ctx, dependencies)];
}
universalPkg = ctx.universalPkg;
installPluginStr = dependencies[0];
return [4 /*yield*/, getPkgInfo(ctx, installPluginStr)];
case 1:
pkgInfo = _b.sent();
if (pkgInfo && universalPkg.isInstalled(pkgInfo.repoName)) {
return [2 /*return*/, uninstallUniversalPlugin(ctx, pkgInfo.repoName)];
}
return [4 /*yield*/, uninstallNpmPlugin(ctx, dependencies)];
case 2:
_b.sent();
pickerConfig = new command_picker_1.CacheController(ctx);
pickerConfig.removeCache(dependencies[0]);
return [2 /*return*/];
}
});
}); });
});
function getRepoInfo(ctx, packageName) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var serverUrl, url;
return __generator(this, function (_b) {
serverUrl = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.serverUrl;
if (!serverUrl) {
return [2 /*return*/, Promise.reject('cannot find \'serverUrl\' from config file')];
}
url = (0, url_1.getURL)(serverUrl, "apply/getlist?name=".concat(packageName));
if (!url) {
return [2 /*return*/, Promise.reject("the serverUrl is invalid: ".concat(serverUrl))];
}
return [2 /*return*/, axios_1.default
.get(url, {
proxy: false,
})
.then(function (res) {
var _a;
var data = res.data || {};
return (((_a = data.data) === null || _a === void 0 ? void 0 : _a.length) > 0 && data.data[0]) || [];
})
.catch(function (e) {
ctx.logger.debug('Get repo info error', e);
})];
});
});
}
exports.getRepoInfo = getRepoInfo;
// git@github.com:tencent/feflow.git
// or http[s]://github.com/tencent/feflow.git or http[s]://user:pwd@github.com/tencent/feflow.git
// to
// encodeURIComponent("github.com/tencent/feflow.git")
function getGitRepoName(repoUrl) {
var ret = /^((http:\/\/|https:\/\/)(.*?@)?|git@)/.exec(repoUrl);
var repurl = '';
if (Array.isArray(ret) && ret.length > 0) {
repurl = repoUrl.substring(ret[0].length);
}
return encodeURIComponent(constant_1.FEFLOW_PLUGIN_GIT_PREFIX + repurl.split(':').join('/'));
}
function getDirRepoName(dir) {
return encodeURIComponent(constant_1.FEFLOW_PLUGIN_LOCAL_PREFIX + dir.trim());
}
function deleteDir(dirPath) {
var files = [];
try {
var dirStats = fs_1.default.statSync(dirPath);
if (!dirStats.isDirectory()) {
return;
}
}
catch (e) {
return;
}
files = fs_1.default.readdirSync(dirPath);
files.forEach(function (file) {
var curPath = "".concat(dirPath, "/").concat(file);
var stat = fs_1.default.statSync(curPath);
if (stat.isDirectory()) {
deleteDir(curPath);
}
else {
fs_1.default.unlinkSync(curPath);
}
});
var stat = fs_1.default.lstatSync(dirPath);
if (stat.isDirectory()) {
fs_1.default.rmdirSync(dirPath);
}
else {
fs_1.default.rmdirSync(fs_1.default.realpathSync(dirPath));
fs_1.default.unlinkSync(dirPath);
}
}
function isGitRepo(url) {
return (new RegExp('^git@[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+(/[a-zA-Z0-9._-]+)+.git(@v(0|[1-9]\\d*).(0|[1-9]\\d*).(0|[1-9]\\d*))?$').test(url)
|| new RegExp('^http(s)?://([a-zA-Z0-9._-]*?(:[a-zA-Z0-9._-]*)?@)?[a-zA-Z0-9._-]+'
+ '(/[a-zA-Z0-9._-]+)+.git(@v(0|[1-9]\\d*).(0|[1-9]\\d*).(0|[1-9]\\d*))?$').test(url));
}
function installNpmPlugin(ctx) {
var _a;
var dependencies = [];
for (var _i = 1; _i < arguments.length; _i++) {
dependencies[_i - 1] = arguments[_i];
}
return __awaiter(this, void 0, void 0, function () {
var packageManager, registryUrl, versionList, needInstall, getCurversion, hasInstallDep_1, err_1;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
packageManager = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.packageManager;
if (!packageManager) {
ctx.logger.error('cannot find \'packageManager\' in config file.');
return [2 /*return*/];
}
return [4 /*yield*/, (0, npm_1.getRegistryUrl)(packageManager)];
case 1:
registryUrl = _b.sent();
needInstall = [];
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, Promise.all(dependencies.map(function (dependency) { return __awaiter(_this, void 0, void 0, function () {
var e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, (0, package_json_1.default)(dependency, registryUrl)];
case 1: return [2 /*return*/, _a.sent()];
case 2:
e_2 = _a.sent();
ctx.logger.error("".concat(dependency, " not found on ").concat(packageManager, ", please check if it exists"));
ctx.logger.debug(e_2);
process.exit(2);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); }))];
case 3:
versionList = _b.sent();
getCurversion = function () {
var json = {};
var installedPlugin = {};
try {
var data = fs_1.default.readFileSync(ctx.rootPkg, 'utf-8');
json = JSON.parse(data);
}
catch (e) {
ctx.logger.error('getCurversion error: ', e);
}
if (!json.dependencies) {
return {};
}
var deps = json.dependencies || json.devDependencies || {};
Object.keys(deps).forEach(function (name) {
if (!/^feflow-plugin-|^@[^/]+\/feflow-plugin-|^generator-|^@[^/]+\/generator-/.test(name)) {
return false;
}
installedPlugin[name] = deps[name];
});
return installedPlugin;
};
hasInstallDep_1 = getCurversion();
needInstall = dependencies.filter(function (dep, idx) {
var depList = (dep || '').split('@');
var depName = !depList[0] ? "@".concat(depList[1]) : depList[0];
if (hasInstallDep_1[depName] !== versionList[idx]) {
return dep;
}
ctx.logger.info("[".concat(dep, "] has installed the latest version: ").concat(hasInstallDep_1[depName]));
return '';
});
return [3 /*break*/, 5];
case 4:
err_1 = _b.sent();
ctx.logger.error('get pkg info error: ', err_1);
return [3 /*break*/, 5];
case 5:
if (!needInstall.length) {
return [2 /*return*/, Promise.resolve()];
}
ctx.logger.info('Installing packages. This might take a couple of minutes.');
return [2 /*return*/, (0, npm_1.install)(packageManager, ctx.root, packageManager === 'yarn' ? 'add' : 'install', needInstall, false).then(function () {
ctx.logger.info('install success');
})];
}
});
});
}
function updateNpmPluginInfo(ctx, pluginName, options) {
var root = ctx.root;
var configPath = path_1.default.join(root, constant_1.NPM_PLUGIN_INFO_JSON);
// eslint-disable-next-line @typescript-eslint/no-require-imports
var npmPluginInfoJson = fs_1.default.existsSync(configPath) ? require(configPath) : {};
if (options === false) {
delete npmPluginInfoJson[pluginName];
}
else {
if (options.globalCmd) {
var pluginInfo = npmPluginInfoJson[pluginName] || {};
var globalCmd = pluginInfo.globalCmd || [];
pluginInfo.globalCmd = globalCmd
? Array.from(new Set(__spreadArray(__spreadArray([], __read(globalCmd), false), __read(options.globalCmd), false)))
: options.globalCmd || [];
npmPluginInfoJson[pluginName] = pluginInfo;
// eslint-disable-next-line no-param-reassign
delete options.globalCmd;
}
npmPluginInfoJson[pluginName] = Object.assign({}, npmPluginInfoJson[pluginName] || {}, options || {});
}
fs_1.default.writeFileSync(configPath, JSON.stringify(npmPluginInfoJson, null, 4));
}
function installJsPlugin(ctx, installPlugin) {
return __awaiter(this, void 0, void 0, function () {
var bin, lib, logger, isGlobal;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
bin = ctx.bin, lib = ctx.lib, logger = ctx.logger;
isGlobal = ctx === null || ctx === void 0 ? void 0 : ctx.args.g;
// install js npm plugin
return [4 /*yield*/, installNpmPlugin(ctx, installPlugin)];
case 1:
// install js npm plugin
_a.sent();
// if install with option -g, register as global command
if (isGlobal && /^feflow-plugin-|^@[^/]+\/feflow-plugin-/.test(installPlugin)) {
ctx.hook.on(constant_1.HOOK_TYPE_ON_COMMAND_REGISTERED, function (cmdName) {
if (cmdName) {
logger.debug("linking cmd [".concat(cmdName, "] registered by plugin ").concat(installPlugin, " to global"));
// create symbol link to plugin, support global plugin cmd
var linker = new linker_1.default();
linker.register(bin, lib, cmdName);
updateNpmPluginInfo(ctx, installPlugin, { globalCmd: [cmdName] });
logger.info("can just type > \"".concat(cmdName, " options\" in terminal, equal to \"fef ").concat(cmdName, " options\""));
}
});
return [2 /*return*/, (0, apply_plugins_1.default)([installPlugin])(ctx)];
}
return [2 /*return*/];
}
});
});
}
function startInstall(ctx, pkgInfo, repoPath, updateFlag, isGlobal) {
return __awaiter(this, void 0, void 0, function () {
var logger, universalPkg, universalModules, bin, lib, e_3, lastRepoName, lastVersion, oldRepoPath, oldPlugin, linker, oldVersion, oldDependencies, plugin, pluginBin, pluginLib, _a, _b, depPlugin, curPkgInfo, commandName, pluginPath, curPlugin, useCommandName_1, commands, e_4, e_5_1, oldDependencies_1, oldDependencies_1_1, _c, oldPkg, oldPkgVersion, cmdName, useCommandName, commands;
var e_5, _d, e_6, _e;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
logger = ctx.logger, universalPkg = ctx.universalPkg, universalModules = ctx.universalModules, bin = ctx.bin, lib = ctx.lib;
// start install
logger.debug('install version:', pkgInfo.checkoutTag);
if (!(pkgInfo.fromType !== PkgInfo.dir)) return [3 /*break*/, 5];
if (!!fs_1.default.existsSync(repoPath)) return [3 /*break*/, 4];
logger.info("start download from ".concat(pkgInfo.repoFrom));
_f.label = 1;
case 1:
_f.trys.push([1, 3, , 4]);
return [4 /*yield*/, (0, git_2.download)(pkgInfo.repoFrom, pkgInfo.checkoutTag, repoPath)];
case 2:
_f.sent();
return [3 /*break*/, 4];
case 3:
e_3 = _f.sent();
logger.warn("download warn with code ".concat(e_3));
return [3 /*break*/, 4];
case 4: return [3 /*break*/, 7];
case 5:
deleteDir(repoPath);
logger.info("start copy from ".concat(pkgInfo.repoFrom));
return [4 /*yield*/, (0, fs_2.copyDir)(pkgInfo.repoFrom, repoPath)];
case 6:
_f.sent();
_f.label = 7;
case 7:
lastRepoName = '';
lastVersion = universalPkg.getInstalled().get(pkgInfo.repoName);
if (lastVersion) {
oldRepoPath = getRepoPath(universalModules, pkgInfo.repoName, lastVersion);
lastRepoName = toSimpleCommand(pkgInfo.repoName);
oldPlugin = (0, apply_plugins_1.resolvePlugin)(ctx, oldRepoPath);
if (oldPlugin.name) {
lastRepoName = oldPlugin.name;
}
}
if (!(pkgInfo.fromType !== PkgInfo.dir)) return [3 /*break*/, 9];
logger.info("switch to version: ".concat(pkgInfo.checkoutTag));
return [4 /*yield*/, (0, git_1.checkoutVersion)(repoPath, pkgInfo.checkoutTag, pkgInfo.lastCheckoutTag)];
case 8:
_f.sent();
_f.label = 9;
case 9:
linker = new linker_1.default();
oldVersion = universalPkg.getInstalled().get(pkgInfo.repoName);
if (isGlobal && oldVersion) {
oldDependencies = universalPkg.getDependencies(pkgInfo.repoName, oldVersion);
if (oldDependencies) {
oldDependencies = new Map(oldDependencies);
}
}
plugin = (0, apply_plugins_1.resolvePlugin)(ctx, repoPath);
// check the validity of the plugin before installing it
return [4 /*yield*/, plugin.check()];
case 10:
// check the validity of the plugin before installing it
_f.sent();
logger.debug('check plugin success');
pluginBin = path_1.default.join(repoPath, ".".concat(constant_1.FEFLOW_BIN));
pluginLib = path_1.default.join(repoPath, ".".concat(constant_1.FEFLOW_LIB));
_f.label = 11;
case 11:
_f.trys.push([11, 19, 20, 21]);
_a = __values(plugin.dep.plugin), _b = _a.next();
_f.label = 12;
case 12:
if (!!_b.done) return [3 /*break*/, 18];
depPlugin = _b.value;
_f.label = 13;
case 13:
_f.trys.push([13, 16, , 17]);
return [4 /*yield*/, getPkgInfo(ctx, depPlugin)];
case 14:
curPkgInfo = _f.sent();
if (!curPkgInfo) {
throw new Error("the dependent plugin ".concat(depPlugin, " does not belong to the universal packge"));
}
return [4 /*yield*/, installPlugin(ctx, depPlugin, false)];
case 15:
_f.sent();
commandName = toSimpleCommand(curPkgInfo.repoName);
if ((oldDependencies === null || oldDependencies === void 0 ? void 0 : oldDependencies.get(curPkgInfo.repoName)) === curPkgInfo.installVersion) {
oldDependencies.delete(curPkgInfo.repoName);
}
universalPkg.depend(pkgInfo.repoName, pkgInfo.installVersion, curPkgInfo.repoName, curPkgInfo.installVersion);
pluginPath = path_1.default.join(universalModules, "".concat(curPkgInfo.repoName, "@").concat(curPkgInfo.installVersion));
curPlugin = (0, apply_plugins_1.resolvePlugin)(ctx, pluginPath);
useCommandName_1 = commandName;
// custom command name
if (curPlugin.name) {
useCommandName_1 = curPlugin.name;
}
if (curPlugin.langRuntime) {
commands = curPlugin.command.getCommands();
linker.registerCustom(pluginBin, pluginLib, commands, useCommandName_1);
}
else {
// call {pkg}@{version} and disable-check
linker.register(pluginBin, pluginLib, "".concat(commandName, "@").concat(curPkgInfo.installVersion, " ").concat(constant_1.DISABLE_ARG, " ").concat(constant_1.SILENT_ARG), useCommandName_1);
}
logger.info("install [".concat(curPkgInfo.showName(), "] ") + "was successful and it is called using [".concat(useCommandName_1, "]"));
return [3 /*break*/, 17];
case 16:
e_4 = _f.sent();
logger.error("failed to install plugin dependency ".concat(depPlugin));
throw e_4;
case 17:
_b = _a.next();
return [3 /*break*/, 12];
case 18: return [3 /*break*/, 21];
case 19:
e_5_1 = _f.sent();
e_5 = { error: e_5_1 };
return [3 /*break*/, 21];
case 20:
try {
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
}
finally { if (e_5) throw e_5.error; }
return [7 /*endfinally*/];
case 21:
if (oldVersion && oldDependencies) {
try {
for (oldDependencies_1 = __values(oldDependencies), oldDependencies_1_1 = oldDependencies_1.next(); !oldDependencies_1_1.done; oldDependencies_1_1 = oldDependencies_1.next()) {
_c = __read(oldDependencies_1_1.value, 2), oldPkg = _c[0], oldPkgVersion = _c[1];
universalPkg.removeDepended(oldPkg, oldPkgVersion, pkgInfo.repoName, oldVersion);
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (oldDependencies_1_1 && !oldDependencies_1_1.done && (_e = oldDependencies_1.return)) _e.call(oldDependencies_1);
}
finally { if (e_6) throw e_6.error; }
}
}
plugin.preInstall.run();
cmdName = toSimpleCommand(pkgInfo.repoName);
useCommandName = cmdName;
// custom command name
if (plugin.name) {
useCommandName = plugin.name;
}
if (plugin.langRuntime) {
commands = plugin.command.getCommands();
linker.registerCustom(bin, lib, commands, useCommandName);
}
else {
linker.register(bin, lib, cmdName, useCommandName);
}
if (lastRepoName && lastRepoName !== useCommandName) {
linker.remove(bin, lib, lastRepoName);
}
// install when global or not exists
if (isGlobal || !universalPkg.isInstalled(pkgInfo.repoName)) {
universalPkg.install(pkgInfo.repoName, pkgInfo.installVersion);
}
// the package management information is retained only when the installation is fully successful
if (isGlobal) {
removeInvalidPkg(ctx);
}
universalPkg.saveChange();
plugin.test.runLess();
if (updateFlag) {
plugin.postUpgrade.runLess();
logger.info("update [".concat(pkgInfo.showName(), "] ") + "was successful and it is called using [".concat(useCommandName, "]"));
}
else {
plugin.postInstall.runLess();
logger.info("install [".concat(pkgInfo.showName(), "] ") + "was successful and it is called using [".concat(useCommandName, "]"));
}
return [2 /*return*/];
}
});
});
}
function getRepoPath(universalModules, repoName, installVersion) {
return path_1.default.join(universalModules, "".concat(repoName, "@").concat(installVersion));
}
function installPlugin(ctx, installPluginStr, isGlobal) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var logger, universalPkg, universalModules, serverUrl, finalInstallPluginStr, pkgInfo, updateFlag, repoPath, pluginFile, currentVersion;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
logger = ctx.logger, universalPkg = ctx.universalPkg, universalModules = ctx.universalModules;
serverUrl = (_a = ctx.config) === null || _a === void 0 ? void 0 : _a.serverUrl;
finalInstallPluginStr = installPluginStr.trim();
if (!serverUrl) {
logger.warn('please configure the serverUrl');
return [2 /*return*/, installJsPlugin(ctx, finalInstallPluginStr)];
}
return [4 /*yield*/, getPkgInfo(ctx, finalInstallPluginStr)];
case 1:
pkgInfo = _b.sent();
if (!pkgInfo) {
return [2 /*return*/, installJsPlugin(ctx, finalInstallPluginStr)];
}
if (!pkgInfo.repoName) {
throw new Error("plugin [".concat(pkgInfo.repoFrom, "] does not exist"));
}
// if the specified version is already installed, skip it
if (universalPkg.isInstalled(pkgInfo.repoName, pkgInfo.checkoutTag, !isGlobal)) {
global && logger.info('the current version is installed');
return [2 /*return*/];
}
updateFlag = false;
repoPath = getRepoPath(universalModules, pkgInfo.repoName, pkgInfo.installVersion);
if (!(pkgInfo.installVersion === constant_1.LATEST_VERSION)) return [3 /*break*/, 3];
pluginFile = path_1.default.join(repoPath, constant_1.UNIVERSAL_PLUGIN_CONFIG);
if (!(universalPkg.isInstalled(pkgInfo.repoName, constant_1.LATEST_VERSION) && fs_1.default.existsSync(pluginFile))) return [3 /*break*/, 3];
return [4 /*yield*/, (0, git_1.getCurrentTag)(repoPath)];
case 2:
currentVersion = _b.sent();
if (currentVersion && pkgInfo.checkoutTag === currentVersion) {
if (global) {
logger.info("[".concat(pkgInfo.repoName, "] the plugin version currently installed is the latest version: ").concat(currentVersion));
}
return [2 /*return*/];
}
updateFlag = true;
if (currentVersion) {
pkgInfo.lastCheckoutTag = currentVersion;
}
_b.label = 3;
case 3:
if (updateFlag) {
logger.info("[".concat(pkgInfo.showName(), "] update the plugin to version ").concat(pkgInfo.checkoutTag));
(0, apply_plugins_1.resolvePlugin)(ctx, repoPath).preUpgrade.runLess();
}
else {
logger.info("[".concat(pkgInfo.showName(), "] installing plugin"));
}
return [4 /*yield*/, startInstall(ctx, pkgInfo, repoPath, updateFlag, isGlobal)];
case 4:
_b.sent();
return [2 /*return*/];
}
});
});
}
exports.installPlugin = installPlugin;
function toSimpleCommand(command) {
return command.replace(constant_1.FEFLOW_PLUGIN_PREFIX, '');
}
function isDir(installPluginDir) {
try {
return fs_1.default.statSync(installPluginDir).isDirectory();
}
catch (e) {
return false;
}
}
// when you install a universal package, return PkgInfo, otherwise return undefined
function getPkgInfo(ctx, installPlugin) {
return __awaiter(this, void 0, void 0, function () {
var installVersion, checkoutTag, repoFrom, repoName, fromType, splits, ver, confirmTag, plugin, pluginName, pluginVersion, repoInfo;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!isGitRepo(installPlugin)) return [3 /*break*/, 2];
fromType = PkgInfo.git;
if (installPlugin.indexOf('.git@') !== -1) {
splits = installPlugin.split('@');
ver = splits.pop();
repoFrom = splits.join('@');
installVersion = ver || constant_1.LATEST_VERSION;
}
else {
repoFrom = installPlugin;
installVersion = constant_1.LATEST_VERSION;
}
confirmTag = installVersion === constant_1.LATEST_VERSION ? undefined : installVersion;
return [4 /*yield*/, (0, git_1.getTag)(repoFrom, confirmTag)];
case 1:
checkoutTag = _c.sent();
repoName = getGitRepoName(repoFrom);
return [3 /*break*/, 7];
case 2:
if (!isDir(installPlugin)) return [3 /*break*/, 3];
fromType = PkgInfo.dir;
plugin = (0, apply_plugins_1.resolvePlugin)(ctx, installPlugin);
if (!plugin.name) {
throw new Error('the [name] field must be specified in plugin.yml');
}
installVersion = constant_1.LATEST_VERSION;
checkoutTag = constant_1.INVALID_VERSION;
repoFrom = installPlugin;
repoName = getDirRepoName(installPlugin);
return [3 /*break*/, 7];
case 3:
fromType = PkgInfo.appStore;
pluginName = void 0;
pluginVersion = void 0;
if (installPlugin.startsWith('@')) {
_a = __read(installPlugin.split('@'), 3), pluginName = _a[1], pluginVersion = _a[2];
pluginName = "@".concat(pluginName);
}
else {
_b = __read(installPlugin.split('@'), 2), pluginName = _b[0], pluginVersion = _b[1];
}
return [4 /*yield*/, getRepoInfo(ctx, pluginName)];
case 4:
repoInfo = _c.sent();
if (!repoInfo) {
ctx.logger.warn("cant found message about ".concat(pluginName, " from Feflow Application market, please check if it exists"));
return [2 /*return*/];
}
repoFrom = repoInfo.repo;
repoName = repoInfo.name;
if (!(isGitRepo(repoFrom) && !repoInfo.tnpm)) return [3 /*break*/, 6];
if (pluginVersion) {
pluginVersion = version_1.default.toFull(pluginVersion);
if (!version_1.default.check(pluginVersion)) {
throw new Error("invalid version: ".concat(pluginVersion, " for ").concat(pluginName));
}
}
installVersion = pluginVersion || constant_1.LATEST_VERSION;
return [4 /*yield*/, (0, git_1.getTag)(repoFrom, installVersion === constant_1.LATEST_VERSION ? undefined : installVersion)];
case 5:
checkoutTag = _c.sent();
return [3 /*break*/, 7];
case 6: return [2 /*return*/];
case 7:
if (!checkoutTag) {
throw new Error("the version [".concat(installVersion, "] was not found"));
}
return [2 /*return*/, new PkgInfo(repoName, repoFrom, installVersion, checkoutTag, fromType)];
}
});
});
}
exports.getPkgInfo = getPkgInfo;
var PkgInfo = /** @class */ (function () {
function PkgInfo(repoName, repoUrl, installVersion, checkoutTag, fromType) {
this.lastCheckoutTag = '';
this.repoName = repoName;
this.repoFrom = repoUrl;
this.installVersion = installVersion;
this.checkoutTag = checkoutTag;
this.fromType = fromType;
}
PkgInfo.prototype.showName = function () {
if (this.fromType !== PkgInfo.appStore) {
return this.repoFrom;
}
return this.repoName;
};
PkgInfo.git = 1;
PkgInfo.appStore = 2;
PkgInfo.dir = 3;
return PkgInfo;
}());
exports.PkgInfo = PkgInfo;
function uninstallUniversalPlugin(ctx, pluginName) {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var logger, universalPkg, version, plugin, repoPath;
return __generator(this, function (_c) {
logger = ctx.logger, universalPkg = ctx.universalPkg;
version = universalPkg.getInstalled().get(pluginName);
if (!version) {
logger.error('this plugin is not currently installed');
return [2 /*return*/];
}
try {
repoPath = path_1.default.join(ctx.universalModules, "".concat(pluginName, "@").concat(version));
try {
plugin = (0, apply_plugins_1.resolvePlugin)(ctx, repoPath);
}
catch (e) {
logger.debug("resolve plugin failure, ".concat(e));
}
(_a = plugin === null || plugin === void 0 ? void 0 : plugin.preUninstall) === null || _a === void 0 ? void 0 : _a.run();
universalPkg.uninstall(pluginName, version);
}
catch (e) {
logger.error("uninstall failure, ".concat(e));
return [2 /*return*/];
}
try {
removeInvalidPkg(ctx);
(_b = plugin === null || plugin === void 0 ? void 0 : plugin.postUninstall) === null || _b === void 0 ? void 0 : _b.runLess();
logger.info('uninstall success');
}
catch (e) {
logger.info("uninstall succeeded, but failed to clean the data, ".concat(e));
}
return [2 /*return*/];
});
});
}
function uninstallNpmPlugin(ctx, dependencies) {
return __awaiter(this, void 0, void 0, function () {
var logger, root, bin, lib, packageManager;
return __generator(this, function (_a) {
logger = ctx.logger, root = ctx.root, bin = ctx.bin, lib = ctx.lib;
dependencies.forEach(function (pkg) {
var _a;
var npmPluginInfoPath = path_1.default.join(root, constant_1.NPM_PLUGIN_INFO_JSON);
try {
if (fs_1.default.existsSync(npmPluginInfoPath)) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
var npmPluginInfo = require(npmPluginInfoPath);
var pluginGlobalCmd = ((_a = npmPluginInfo === null || npmPluginInfo === void 0 ? void 0 : npmPluginInfo[pkg]) === null || _a === void 0 ? void 0 : _a.globalCmd) || [];
pluginGlobalCmd.forEach(function (cmd) {
new linker_1.default().remove(bin, lib, cmd);
});
updateNpmPluginInfo(ctx, pkg, false);
}
}
catch (e) {
logger.debug("remove plugin registered cmd link failure, ".concat(e));
}
});
packageManager = (ctx.config || {}).packageManager;
if (!packageManager) {
logger.error('cannot find \'packageManager\' in config file');
return [2 /*return*/];
}
return [2 /*return*/, (0, npm_1.install)(packageManager, ctx.root, packageManager === 'yarn' ? 'remove' : 'uninstall', dependencies, false).then(function () {
logger.info('uninstall success');
})];
});
});
}
function removePkg(ctx, pkg, version) {
var bin = ctx.bin, lib = ctx.lib, universalPkg = ctx.universalPkg;
var pluginPath = path_1.default.join(ctx.universalModules, "".concat(pkg, "@").concat(version));
if (fs_1.default.existsSync(pluginPath)) {
var useName = toSimpleCommand(pkg);
var curPlugin = (0, apply_plugins_1.resolvePlugin)(ctx, pluginPath);
if (curPlugin.name) {
useName = curPlugin.name;
}
deleteDir(pluginPath);
if (!universalPkg.isInstalled(pkg)) {
try {
new linker_1.default().remove(bin, lib, useName);
}
catch (e) {
ctx.logger.debug("remove link failure, ".concat(e));
}
}
}
}
function removeInvalidPkg(ctx) {
var e_7, _a;
var universalPkg = ctx.universalPkg;
var invalidDep = universalPkg.removeInvalidDependencies();
try {
for (var invalidDep_1 = __values(invalidDep), invalidDep_1_1 = invalidDep_1.next(); !invalidDep_1_1.done; invalidDep_1_1 = invalidDep_1.next()) {
var _b = __read(invalidDep_1_1.value, 2), invalidPkg = _b[0], invalidVersion = _b[1];
removePkg(ctx, invalidPkg, invalidVersion);
}
}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally {
try {
if (invalidDep_1_1 && !invalidDep_1_1.done && (_a = invalidDep_1.return)) _a.call(invalidDep_1);
}
finally { if (e_7) throw e_7.error; }
}
}
// update only the plugins installed globally
function updateUniversalPlugin(ctx, pkg, version, plugin) {
return __awaiter(this, void 0, void 0, function () {
var universalPkg, dependedOn, dependedOn_1, dependedOn_1_1, _a, dependedOnPkg, dependedOnVersion, e_8_1, newVersion;
var e_8, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
universalPkg = ctx.universalPkg;
dependedOn = universalPkg.getDepended(pkg, version);
if (!dependedOn) return [3 /*break*/, 8];
_c.label = 1;
case 1:
_c.trys.push([1, 6, 7, 8]);
dependedOn_1 = __values(dependedOn), dependedOn_1_1 = dependedOn_1.next();
_c.label = 2;
case 2:
if (!!dependedOn_1_1.done) return [3 /*break*/, 5];
_a = __read(dependedOn_1_1.value, 2), dependedOnPkg = _a[0], dependedOnVersion = _a[1];
if (dependedOnVersion !== constant_1.LATEST_VERSION) {
return [3 /*break*/, 4];
}
return [4 /*yield*/, updatePlugin(ctx, dependedOnPkg, dependedOnVersion)];
case 3:
_c.sent();
_c.label = 4;
case 4:
dependedOn_1_1 = dependedOn_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_8_1 = _c.sent();
e_8 = { error: e_8_1 };
return [3 /*break*/, 8];
case 7:
try {
if (dependedOn_1_1 && !dependedOn_1_1.done && (_b = dependedOn_1.return)) _b.call(dependedOn_1);
}
finally { if (e_8) throw e_8.error; }
return [7 /*endfinally*/];
case 8:
newVersion = universalPkg.getInstalled().get(pkg);
if (!(newVersion === version && version === constant_1.LATEST_VERSION && plugin.autoUpdate)) return [3 /*break*/, 10];
return [4 /*yield*/, updatePlugin(ctx, pkg, version)];
case 9:
_c.sent();
_c.label = 10;
case 10: return [2 /*return*/];
}
});
});
}
exports.updateUniversalPlugin = updateUniversalPlugin;
function updatePlugin(ctx, pkg, version) {
return __awaiter(this, void 0, void 0, function () {
var universalPkg, isGlobal, e_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
universalPkg = ctx.universalPkg;
isGlobal = universalPkg.isInstalled(pkg, version);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, installPlugin(ctx, "".concat(pkg, "@").concat(version), isGlobal)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
e_9 = _a.sent();
ctx.logger.error("[".concat(pkg, "] update failure: "), e_9);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
}
//# sourceMappingURL=install.js.map