UNPKG

msync

Version:

Easily manage building and syncing multiple node-modules in a flexibly defined workspace.

182 lines (181 loc) 8.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.publish = exports.cmd = exports.args = exports.description = exports.alias = exports.name = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var ls_cmd_1 = require("./ls.cmd"); exports.name = 'publish'; exports.alias = ['p', 'pub']; exports.description = 'Publishes all modules that are ahead of NPM.'; exports.args = {}; function cmd(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, publish()]; case 1: _a.sent(); return [2]; } }); }); } exports.cmd = cmd; function publish() { return tslib_1.__awaiter(this, void 0, void 0, function () { var settings, modules, total, prompt, answer, path, startedAt, publishCommand, current, publishResult; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, (0, common_1.loadSettings)({ npm: true, spinner: true })]; case 1: settings = _a.sent(); if (!settings) { common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR); return [2]; } modules = settings.modules.filter(function (pkg) { return isPublishRequired(pkg); }); (0, ls_cmd_1.printTable)(modules); total = modules.length; if (total === 0) { common_1.log.info.gray("\u2728\u2728 No modules need to be published.\n"); return [2]; } common_1.log.info(); prompt = "Publish ".concat(total, " ").concat((0, common_1.plural)('module', total), " to NPM now?"); return [4, promptYesNoSave(prompt)]; case 2: answer = _a.sent(); if (typeof answer === 'boolean' && answer === false) { common_1.log.info(); return [2]; } if (!(answer === 'save')) return [3, 4]; path = 'msync.publish.json'; return [4, common_1.SaveUtil.write(path, modules)]; case 3: _a.sent(); common_1.log.info.gray("saved publish manifest to ".concat(common_1.log.white(path))); common_1.log.info(); return [2]; case 4: common_1.log.info.gray("Publishing to NPM:\n"); startedAt = new Date(); publishCommand = function (pkg) { return "npm publish"; }; return [4, runCommand(modules, publishCommand, { concurrent: false, exitOnError: true, onStart: function (pkg) { return (current = pkg); }, })]; case 5: publishResult = _a.sent(); if (publishResult.success) { common_1.log.info("\n\u2728\u2728 Done ".concat(common_1.log.gray((0, common_1.elapsed)(startedAt)), "\n")); } else { common_1.log.info(); common_1.log.info.yellow("Failed on module:"); common_1.log.info.gray(" ".concat((0, common_1.formatModuleName)((current === null || current === void 0 ? void 0 : current.name) || 'UNKNOWN'))); common_1.log.info(); common_1.log.error(publishResult.error); } return [2]; } }); }); } exports.publish = publish; var runCommand = function (modules, cmd, options) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var concurrent, exitOnError, errors, task, tasks, runner, error_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: concurrent = options.concurrent, exitOnError = options.exitOnError; errors = []; task = function (pkg) { return { title: common_1.log.gray("".concat((0, common_1.formatModuleName)(pkg.name), ": ").concat(cmd(pkg))), task: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var command, res, containsError, isAlreadyPublishedError; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: options.onStart(pkg); command = "cd ".concat(pkg.dir, " && ").concat(cmd(pkg)); return [4, common_1.exec.cmd.run(command, { silent: true })]; case 1: res = _a.sent(); containsError = function () { var messages = []; for (var _i = 0; _i < arguments.length; _i++) { messages[_i] = arguments[_i]; } return errors.some(function (err) { return err.errors.some(function (line) { return messages.every(function (msg) { return line.includes(msg); }); }); }); }; if (res.error) { errors = tslib_1.__spreadArray(tslib_1.__spreadArray([], errors, true), [{ pkg: pkg, info: res.info, errors: res.errors }], false); isAlreadyPublishedError = containsError('403 Forbidden', 'cannot publish over the previously published'); if (!isAlreadyPublishedError) { throw res.error; } } return [4, common_1.time.wait(2500)]; case 2: _a.sent(); return [2, res]; } }); }); }, }; }; tasks = modules.map(function (pkg) { return task(pkg); }); runner = (0, common_1.listr)(tasks, { concurrent: concurrent, exitOnError: exitOnError }); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, runner.run()]; case 2: _a.sent(); return [2, { success: true, error: null }]; case 3: error_1 = _a.sent(); errors.forEach(function (_a) { var info = _a.info; info.forEach(function (line) { return common_1.log.info(line); }); }); return [2, { success: false, error: error_1 }]; case 4: return [2]; } }); }); }; function promptYesNoSave(message) { return tslib_1.__awaiter(this, void 0, void 0, function () { var res; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, common_1.inquirer.prompt({ type: 'list', name: 'answer', message: message, choices: [ { name: 'yes', value: true }, { name: 'no', value: false }, { name: 'save (json file)', value: 'save' }, ], })]; case 1: res = (_a.sent()); return [2, res.answer]; } }); }); } var isPublishRequired = function (pkg) { var _a; return ((_a = pkg.npm) === null || _a === void 0 ? void 0 : _a.latest) ? common_1.semver.gt(pkg.version, pkg.npm.latest) : false; };