@xutl/version
Version:
Package Version Manipulation
104 lines (103 loc) • 4.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVersion = exports.bump = void 0;
const https_1 = require("https");
const semver_1 = __importDefault(require("semver"));
const aim_1 = __importDefault(require("@xutl/aim"));
const istream_1 = require("@xutl/istream");
const json_1 = __importDefault(require("@xutl/json"));
const path_1 = require("path");
const fs_1 = require("fs");
if ((module.id = '.')) {
const MODES = ['patch', 'minor', 'major'];
if (process.argv.length < 3 || process.argv.length > 4)
bail(1);
const base = path_1.resolve(process.argv[2]);
const mode = (process.argv[3] || undefined);
if (mode && !MODES.includes(mode))
bail(2);
if (!fs_1.statSync(base).isDirectory())
bail(3);
if (!fs_1.statSync(`${base}/package.json`).isFile())
bail(4);
main(base, mode).catch((e) => {
console.error(e);
bail(5);
});
async function main(pkgdir, mode) {
const { execSync } = require('child_process');
const pre = process.cwd();
try {
process.chdir(pkgdir);
const registry = execSync('npm config get registry').toString('utf-8').trim();
const commits = execSync('git log --pretty=format:"%H"')
.toString('utf-8')
.split(/\r?\n/)
.map((s) => s.trim())
.filter((s) => !!s);
const packageJSON = (await json_1.default.read(`./package.json`));
const original = packageJSON.version;
const version = (packageJSON.version = await getVersion(packageJSON.name, registry));
console.error(`@latest = ${version}`);
let res = -1;
try {
const relh = execSync(`git rev-list -n 1 v${version}`).toString('utf-8').trim();
res = commits.indexOf(relh);
}
catch (ex) { }
const chg = res < 0 ? Number.POSITIVE_INFINITY : res;
mode = (mode !== null && mode !== void 0 ? mode : chg) ? 'patch' : undefined;
if (bump(packageJSON, chg ? mode : undefined)) {
console.log(`${packageJSON.name} set to ${packageJSON.version} (${!chg ? 'published' : 'to publish'})`);
}
else {
console.log(`${packageJSON.name} is at ${packageJSON.version} (${!chg ? 'published' : 'to publish'})`);
}
if (original !== packageJSON.version)
await json_1.default.write(`${pkgdir}/package.json`, packageJSON, { whitespace: '\t' });
}
finally {
process.chdir(pre);
}
}
function bail(code = 1) {
console.error(`xutlversion <path/to/package/> [ ${MODES.join(' | ')} ]`);
process.exit(code);
}
}
async function bump(packageJSON, mode) {
const version = packageJSON.version;
const next = mode ? semver_1.default.inc(version, mode) : version;
if (!next)
throw new Error(`failed to increment ${version}`);
packageJSON.version = next;
return version !== next;
}
exports.bump = bump;
function getVersion(packageName, registry = 'https://registry.npmjs.org/') {
return new Promise((resolve, reject) => {
https_1.get(`${registry}/${encodeURIComponent(packageName)}`, async (res) => {
try {
if (res.statusCode === 404)
return resolve('0.0.0');
if (res.statusCode !== 200)
throw new Error(`http status: ${res.statusCode}`);
const result = json_1.default.parse((await aim_1.default(istream_1.strings(res)).array()).join(''))['dist-tags'].latest;
if (!result)
throw new Error('no version found');
const cleaned = semver_1.default.clean(result);
if (!cleaned)
throw new Error('invalid version found');
resolve(cleaned);
}
catch (err) {
reject(err);
}
}).on('error', reject);
});
}
exports.getVersion = getVersion;