@speedy-js/mono
Version:
Monorepo development & continuous integration tooling.
118 lines • 4.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.patch = exports.removeGitHead = void 0;
/**
* Module dependencies
*/
const path_1 = require("path");
const fs_1 = require("fs");
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const execa_1 = __importDefault(require("execa"));
const boxen_1 = __importDefault(require("boxen"));
const text_table_1 = __importDefault(require("text-table"));
const string_width_1 = __importDefault(require("string-width"));
const shared_1 = require("./shared");
/**
* Remove duplicate gitHead field when "lerna publish" fails.
*
* @param {String} path
* @returns {void}
*/
function removeGitHead(path) {
const GIT_HEAD_REG = /,[\n\t\s]*"gitHead":\s"[^"]+"/;
let content = (0, fs_1.readFileSync)(path, 'utf-8');
content = content.replace(GIT_HEAD_REG, '');
(0, fs_1.writeFileSync)(path, content, 'utf-8');
}
exports.removeGitHead = removeGitHead;
/**
* Enter a interactive cli to know the current situation of `lerna publish`.
*
* @param {String} cwd
* @param {String} version
* @param {String} tag
* @returns {Promise<void>}
*/
async function patch(options) {
const { cwd, tag, runInBand = false, ignoreScripts = false } = options;
let { version } = options;
shared_1.logger.info('Patch Started');
if (!tag) {
throw new Error('[MONO] "tag" is required for "patch"');
}
if (!version) {
try {
const config = (0, shared_1.resolveLernaConfig)(cwd);
version = config === null || config === void 0 ? void 0 : config.data.version;
}
catch (e) {
// dot not handle it for now
}
}
shared_1.logger.info(`Version: ${chalk_1.default.cyan(version)}`);
shared_1.logger.info(`Tag: ${chalk_1.default.cyan(tag)}`);
const pkgs = await (0, shared_1.resolvePackages)({ cwd, tag });
pkgs.forEach((pkg) => removeGitHead((0, path_1.join)(pkg.path, 'package.json')));
if (pkgs.every((pkg) => pkg.version === version)) {
return console.log(`${chalk_1.default.cyan('❯ ')}${chalk_1.default.gray('[MONO]')} Do not need "patch" since all packages've been published correctly! `);
}
const table = pkgs.map((pkg) => {
if (pkg.version === version) {
return [
chalk_1.default.gray(pkg.name),
chalk_1.default.gray(pkg.version),
chalk_1.default.gray('√'),
'-',
];
}
return [chalk_1.default.cyan(pkg.name), chalk_1.default.green(pkg.version), '❌', version];
});
table.unshift([
'Package Name',
`Remote Version (tag: ${tag})`,
'Status',
'Target Version',
].map((v) => chalk_1.default.dim(v)));
console.log((0, boxen_1.default)((0, text_table_1.default)(table, {
stringLength: string_width_1.default,
})));
const { yes } = await inquirer_1.default.prompt([
{
name: 'yes',
message: 'Continue to patch',
type: 'list',
choices: ['N', 'Y'],
},
]);
if (yes === 'Y') {
const patchedPkgs = pkgs.filter((pkg) => pkg.version !== version);
const releasePkg = async (pkg) => {
const commandCwd = (0, path_1.join)(cwd, `packages/${pkg.dirname}`);
console.log(chalk_1.default.gray(`$ npm publish --tag ${tag} # ${commandCwd}`));
const npmArgs = ['publish', '--tag', tag];
if (ignoreScripts) {
npmArgs.push('--ignore-scripts');
}
await (0, execa_1.default)('npm', npmArgs, {
// silent: true,
stdio: 'inherit',
cwd: commandCwd,
});
console.log(`+ ${pkg.name}@${version}\n`);
};
if (runInBand) {
for (const pkg of patchedPkgs) {
await releasePkg(pkg);
}
}
else {
await Promise.all(patchedPkgs.map(releasePkg));
}
}
}
exports.patch = patch;
//# sourceMappingURL=patch.js.map