@theintern/dev
Version:
Development support scripts for Intern projects.
305 lines • 14.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var shelljs_1 = require("shelljs");
var semver = tslib_1.__importStar(require("semver"));
var fs_1 = require("fs");
var util_1 = require("util");
var common_1 = require("./common");
var path_1 = require("path");
var readline_1 = require("readline");
var chalk_1 = tslib_1.__importDefault(require("chalk"));
var child_process_1 = require("child_process");
function cleanup() {
common_1.log('Cleaning up...');
process.chdir(rootDir);
shelljs_1.rm('-rf', tmpDir);
}
function printUsage() {
shelljs_1.echo("\n Usage: intern-dev-release [help] [b=branch] [v=version] [p=prerelease] [t=tag]\n\n help Displays this message\n branch Branch to release; defaults to the current branch\n tag dist-tag to use when publishing; defaults to \"latest\"\n version Version to release; defaults to what is listed in the\n package.json in the branch. It should only be specified\n for pre-releases\n prerelease A prerelease tag to attach to the version, like \"alpha\"\n beta\n ".trim());
}
function prompt() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return tslib_1.__awaiter(this, void 0, void 0, function () {
var question;
return tslib_1.__generator(this, function (_a) {
question = util_1.format.apply(void 0, tslib_1.__spreadArrays([args[0]], args.slice(1)));
return [2, new Promise(function (resolve) {
rl.question(question, resolve);
})];
});
});
}
function loadPackageJson() {
return JSON.parse(fs_1.readFileSync('package.json', { encoding: 'utf8' }));
}
function updatePackageVersion(version) {
var packageJson = loadPackageJson();
packageJson.version = version;
fs_1.writeFileSync('package.json', JSON.stringify(packageJson, null, ' '));
}
var rl = readline_1.createInterface({
input: process.stdin,
output: process.stdout,
});
var branch = common_1.exec('git rev-parse --abbrev-ref HEAD').stdout.replace(/\s+$/, '');
var version;
var npmTag;
var preVersion;
var preTag;
var newBranch;
var branchVersion;
var userVersion;
for (var _i = 0, _a = process.argv.slice(2); _i < _a.length; _i++) {
var arg = _a[_i];
if (arg === 'help') {
printUsage();
process.exit(0);
}
var _b = arg.split('=', 2), key = _b[0], value = _b[1];
switch (key) {
case 'b':
branch = value;
break;
case 'v':
userVersion = value;
break;
case 't':
npmTag = value;
break;
case 'p':
preTag = value;
break;
default:
shelljs_1.echo("Invalid argument \"" + arg + "\"\n\n");
printUsage();
process.exit(1);
}
}
var rootDir = process.cwd();
var tmpDir = '_publish';
var exitCode = 0;
var pushBranches = [branch];
if (!npmTag) {
if (preTag || userVersion) {
npmTag = 'next';
}
else {
npmTag = 'latest';
}
}
function main() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var question_1, output, currentBranch, message, packageJson, tagLines, tags_1, sameVersionTags, tags, _i, _a, tag, publishDir, question, pubResult, _b, pushBranches_1, branch_1, remote, _c, _d, line, error_1;
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 4, 5, 6]);
if (!(branch !== 'master')) return [3, 2];
question_1 = "Are you sure you want to create a release from branch " + branch + "?\n" +
'Enter "y" to continue, any other key to abort.\n' +
'> ';
return [4, prompt(question_1)];
case 1:
if ((_e.sent()) !== 'y') {
throw new Error('Aborted');
}
_e.label = 2;
case 2:
output = void 0;
try {
output = common_1.exec('git config receive.denyCurrentBranch').stdout;
}
catch (_error) {
}
if (!output || output.indexOf('updateInstead') !== 0) {
throw new Error('Repository should have receive.denyCurrentBranch set to "updateInstead"');
}
currentBranch = common_1.exec('git rev-parse --abbrev-ref HEAD').stdout.replace(/\s+$/, '');
if (branch === currentBranch) {
try {
common_1.exec('git diff-index --quiet HEAD');
}
catch (error) {
common_1.log(chalk_1.default.red('Warning: You have uncommitted changes.'));
}
}
message = "Creating a new release from branch " + branch;
if (userVersion) {
message += " with version override " + userVersion;
}
common_1.log(message + ".");
process.chdir(rootDir);
if (shelljs_1.test('-d', tmpDir)) {
cleanup();
}
shelljs_1.mkdir(tmpDir);
common_1.exec("git clone --recursive . " + tmpDir);
process.chdir(tmpDir);
common_1.log("Building branch \"" + branch + "\"...");
common_1.exec("git checkout " + branch);
packageJson = loadPackageJson();
if (userVersion) {
if (semver.gte(packageJson.version, userVersion)) {
throw new Error('Provided version must be >= current version');
}
version = userVersion;
}
else {
version = packageJson.version;
if (!semver.prerelease(version)) {
throw new Error('Releases may only be generated from pre-release versions');
}
version = semver.major(version) + "." + semver.minor(version) + "." + semver.patch(version);
if (preTag) {
version += "-" + preTag;
tagLines = void 0;
try {
tagLines = common_1.exec('git show-ref --tags --abbrev')
.stdout.replace(/\s+$/, '')
.split('\n');
}
catch (error) {
tagLines = [];
}
tags_1 = tagLines.map(function (line) { return /refs\/tags\/(.*)/.exec(line)[1]; });
sameVersionTags = tags_1.filter(function (tag) {
try {
return (semver.major(tag) === semver.major(version) &&
semver.minor(tag) === semver.minor(version) &&
semver.patch(tag) === semver.patch(version) &&
semver.prerelease(tag)[0] === preTag);
}
catch (error) {
return false;
}
});
sameVersionTags.sort(function (a, b) {
var preA = Number(semver.prerelease(a)[1]);
var preB = Number(semver.prerelease(b)[1]);
return preB - preA;
});
version = semver.inc(sameVersionTags[0] || version, 'prerelease', preTag);
}
}
tags = common_1.exec('git tag').stdout;
for (_i = 0, _a = tags.split('\n'); _i < _a.length; _i++) {
tag = _a[_i];
if (tag === version) {
throw new Error('Version ' + tag + ' has already been tagged');
}
}
if (semver.major(version) === 0 ||
semver.patch(version) !== 0 ||
semver.prerelease(version)) {
preVersion = semver.inc(version, 'patch') + "-pre";
}
else {
newBranch = semver.major(version) + "." + semver.minor(version);
branchVersion = semver.inc(version, 'patch') + "-pre";
preVersion = semver.inc(version, 'minor') + "-pre";
}
updatePackageVersion(version);
common_1.exec("git commit -m \"Updating metadata for " + version + "\" package.json");
common_1.exec("git tag -a -m \"Release " + version + "\" " + version);
common_1.exec('git checkout HEAD^ package.json');
common_1.exec('git reset package.json');
updatePackageVersion(preVersion);
common_1.exec("git commit -m \"Updating source version to " + preVersion + "\" package.json");
if (newBranch && branchVersion) {
common_1.log("Creating new branch " + newBranch + "...");
common_1.exec("git checkout -b " + newBranch + " " + version);
updatePackageVersion(branchVersion);
common_1.exec("git commit -m \"Updating source version to " + branchVersion + "\" package.json");
pushBranches.push(newBranch);
}
common_1.log("Checking out and building " + version + "...");
common_1.exec("git checkout " + version);
common_1.exec('npm install');
common_1.exec('npm run build');
common_1.log('Done!');
publishDir = (common_1.internDev && common_1.internDev.publishDir);
if (!publishDir) {
publishDir = common_1.buildDir;
if (fs_1.existsSync(path_1.join(common_1.buildDir, 'src'))) {
publishDir = path_1.join(common_1.buildDir, 'src');
}
}
common_1.log("Package to be published from " + tmpDir + "/" + publishDir + ".");
question = 'Please confirm packaging success, then enter "y" to publish to npm\n' +
("'" + npmTag + "', push tag '" + version + "', and upload. ' +\n 'Enter any other key to bail.\n") +
'> ';
return [4, prompt(question)];
case 3:
if ((_e.sent()) !== 'y') {
common_1.log('Not publishing');
throw new Error('Aborted');
}
process.chdir(publishDir);
pubResult = child_process_1.spawnSync('npm', [
'publish',
'--tag',
npmTag,
'--access',
'public',
'--registry',
'https://registry.npmjs.org',
], {
stdio: 'inherit',
});
if (pubResult.error) {
throw pubResult.error;
}
else if (pubResult.status !== 0) {
throw new Error("npm publish failed: " + pubResult.status);
}
for (_b = 0, pushBranches_1 = pushBranches; _b < pushBranches_1.length; _b++) {
branch_1 = pushBranches_1[_b];
common_1.exec("git push origin " + branch_1);
}
common_1.exec('git push origin --tags');
process.chdir(rootDir);
remote = void 0;
for (_c = 0, _d = common_1.exec('git remote -v').stdout.split(/\n/); _c < _d.length; _c++) {
line = _d[_c];
if (/github.com:theintern\/.*\(push\)/.test(line)) {
remote = line.split('\t')[0];
break;
}
}
if (!remote) {
common_1.log('No origin remote; not pushing');
}
else {
common_1.exec("git push " + remote);
common_1.exec("git push " + remote + " --tags");
}
common_1.log('All done! Yay!');
return [3, 6];
case 4:
error_1 = _e.sent();
if (error_1.message !== 'Aborted') {
common_1.log("" + chalk_1.default.red(error_1.stack));
common_1.log('Aborted.');
exitCode = 1;
}
return [3, 6];
case 5:
if (exitCode !== 1) {
cleanup();
}
process.exit(exitCode);
return [7];
case 6: return [2];
}
});
});
}
main().catch(function (error) {
console.error(error);
});
//# sourceMappingURL=intern-dev-release.js.map