embark/scripts/release.js

190 lines
5.6 KiB
JavaScript
Raw Normal View History

build: introduce a `prepare` script in embark's package.json **TL;DR** These changes affect workflow with yarn. To prevent embark's `prepare` script from running undesirably: - If node_modules is in place and you're reinstalling after switching branches: ``` yarn run install_all ``` - If node_modules is missing (fresh clone or deleted): ``` EMBARK_NO_PREPARE=t yarn install && yarn run install_all ``` It's not recommended to set `EMBARK_NO_PREPARE` in your environment (e.g. in `.bashrc`) since that would interfere with embark's `release` script if/when you run it. ----------------- **1.** Specify embark's build-related steps in the `prepare` script of package.json. When embark is installed directly from GitHub the `prepare` script results in a "pre install" phase (handled automatically by npm/yarn) that fetches devDependencies, builds embark (including embark-ui), packs a tarball with the same steps (minus testing and tree-checking) as would happen during an embark release, and finally does a production install from that tarball. Important point: installs from GitHub must be performed with yarn; they're no longer possible with npm since during the "pre install" phase npm will honor embark's `.npmrc` and `"engines"` settings. The following will work correctly after this commit is merged: ``` yarn [global] add git+https://github.com/embark-framework/embark.git ``` Use of "hosted git" shortcuts (e.g. `embark-framework/embark#bracnh`) won't work correctly because yarn doesn't fully support them. See: https://github.com/yarnpkg/yarn/issues/5235. It's important to use `git+https` urls. Following a succesful install with `git+https` it is possible to use a "hosted git" shortcut or `https` url, but that's owing to a subtle and unreliable interaction between yarn's cache and yarn's logic for installing from a url/shortcut. **2.** Adjust the npm configs (`.npmrc`) for embark/-ui so that `yarn run [cmd] [--opt]` can be used in place of `npm run [cmd] -- [--opt]`. Either way is okay for running scripts, they're equivalent, but note the requirement to use `--` before specifying command options with `npm run`. **3.** Introduce yarn configs (`.yarnrc`) for embark/-ui and include the `check-files` directive. H/t to @alaibe for the recommendation. **4.** Ignore embark's `dist/typings` and `scripts` directories when packing a tarball. **5.** Refactor embark/-ui's npm-scripts in relation to the `prepare` script, and make other small improvements. Notably, if the environment variable `EMBARK_NO_PREPARE` is truthy (from JS perspective) then embark's `prepare` script will exit early. This prevents `install_all` and `prepare` from getting stuck in a loop (`install:core` uses cross-env to set `EMBARK_NO_PREPARE`) and provides a mechanism for users to skip the `prepare` script when doing a fresh install: ``` EMBARK_NO_PREPARE=t yarn install ``` **6.** Give `.js` extensions to node scripts in embark's `scripts/`, remove the shebang lines, and have npm-scripts explicitly invoke them with node. This arrangement works for all platforms: Linux, macOS, and Windows. **7.** Adjust travis and appveyor configs. Since at present there aren't any tests or other CI steps that make use of embark-ui's production build, set `EMBARK_NO_PREPARE` in the CI environments and invoke `build:node` directly. Check the working tree after `yarn install` for embark/-ui. This detects situations where changes should have been committed to `yarn.lock` but were not. Check the working tree again at the end to detect situations where ignore files should have been adjusted but were not. Both checks could also detect other surprising behavior that needs to be investigated. Any time the working tree is not clean (there are untracked files or changes) CI will fail. Drop CI runs for node 8.11.3 because that version ships with an older npm that results in unstaged changes to the test apps' `package-lock.json` files, causing the working tree check to fail at the end of the CI run. A simple workaround isn't apparent, but the matter can be revisited. **8.** Refactor embark's `release` script in light of the `prepare` script. Notably, do the push step only after `npm publish` completes successfully. This allows embark's `prepare` and `prepublishOnly` scripts to detect problems before a commit and tag are pushed to GitHub, avoiding a need to rebase/revert the remote release branch; the local branch will still need to have a commit dropped and tag deleted before rerunning the `release` script. Prompt the user if the `release` script is not being run in `--dry-run` mode. Provide additional visual indicators of `--dry-run` mode. Force the user to supply `--repo-branch [branch]` if the intention is to release from a branch other than `master`.
2018-11-19 19:11:57 +00:00
/* global process require */
const chalk = require('chalk');
const {execSync} = require('child_process');
const minimist = require('minimist');
const {prompt} = require('promptly');
const standardVersion = require('standard-version');
const args = minimist(process.argv.slice(2));
const DEFAULT_UPSTREAM_REPO_BRANCH = 'master';
const DEFAULT_UPSTREAM_REPO_ORIGIN = 'origin';
const branch = args['repo-branch'] || DEFAULT_UPSTREAM_REPO_BRANCH;
const origin = args['repo-origin'] || DEFAULT_UPSTREAM_REPO_ORIGIN;
const distTag = args['npm-dist-tag'];
const dryRun = args['dry-run'];
const prerelease = args.prerelease;
const releaseAs = args['release-as'];
const sign = args.sign;
// eslint-disable-next-line no-confusing-arrow
const dryRunMaybe = () => dryRun ? leftPad1(chalk.yellow('(DRY RUN)')) : '';
const execSyncInherit = (cmd) => execSync(cmd, {stdio: 'inherit'});
// eslint-disable-next-line no-confusing-arrow
const leftPad1 = (str) => ' ' + str;
// eslint-disable-next-line no-confusing-arrow
const leftPad1Maybe = (str) => str ? leftPad1(str) : str;
const log = (mark, str) => console.log(mark, str.filter(s => !!s).join(' '));
const logError = (...str) => log(chalk.red('✘'), str);
const logInfo = (...str) => log(chalk.blue(''), str);
const logSuccess = (...str) => log(chalk.green('✔'), str);
const logWarning = (...str) => log(chalk.yellow('‼︎'), str);
const yesNoValidator = (input) => {
const _input = input && input[0].toLowerCase();
if (!['y', 'n'].includes(_input)) {
throw new Error(chalk.red('✘') + leftPad1(`Please answer [y]es or [n]o.`));
}
return _input;
};
const promptOpts = {default: 'blank', validator: yesNoValidator};
const proceedAnywayPrompt = async () => {
let answer = await prompt(
`${chalk.yellow('⁉︎')} Proceed anyway? [y/n]`,
promptOpts
);
if (answer === 'n') {
logWarning(`Stopping right here.`);
process.exit(0);
}
};
const dryRunPrompt = async () => {
let answer = await prompt(
`${chalk.blue('⁇')} This is ${chalk.yellow('NOT')} a --dry-run.` +
leftPad1(`Did you complete a successful --dry-run first? [y/n]`),
promptOpts
);
if (answer === 'n') await proceedAnywayPrompt();
};
(async () => {
try {
if (!dryRun) await dryRunPrompt();
logInfo(`Determining the current branch...`);
let currentBranch;
try {
currentBranch = execSync(`git rev-parse --abbrev-ref HEAD`)
.toString()
.trim();
} catch (e) {
logError(`Couldn't determine the branch. Please check the error above.`);
throw new Error();
}
if (currentBranch !== branch) {
logError(
`Current branch '${currentBranch}' is not the same as release branch`,
`'${branch}'. Please checkout the release branch before rerunning this`,
`script or rerun with '--repo-branch ${currentBranch}'.`
);
throw new Error();
}
logSuccess(`Current branch and release branch are the same.`);
logInfo(`Checking the working tree...`);
try {
execSyncInherit(`npm run --silent check-working-tree`);
logSuccess(`Working tree is clean.`);
} catch (e) {
logError(
`Working tree is dirty or has untracked files. Please make necessary`,
`changes or commits before rerunning this script.`
);
throw new Error();
}
logInfo(
`Fetching from origin '${origin}' to compare local and remote branches...`
);
try {
execSyncInherit(`git fetch ${origin}`);
} catch (e) {
logError(`Couldn't fetch latest commits. Please check the error above.`);
throw new Error();
}
let localRef, originRef;
try {
localRef = execSync(`git rev-parse ${branch}`).toString();
originRef = execSync(`git rev-parse ${origin}/${branch}`).toString();
} catch (e) {
logError(`A problem occured. Please check the error above.`);
throw new Error();
}
if (localRef !== originRef) {
logError(
`Local branch '${branch}' is not in sync with '${origin}/${branch}'.`,
`Please sync branches before rerunning this script.`
);
throw new Error();
}
logSuccess(`Local branch is in sync with remote branch.`);
logInfo(`Running Standard Version${dryRunMaybe()}...`);
await standardVersion({
dryRun,
prerelease,
releaseAs,
sign
});
logInfo(`Publishing new Embark version on npm${dryRunMaybe()}...`);
const npmPublishCommand = [
`npm publish`,
leftPad1Maybe(`${distTag ? `--tag ${distTag}` : ''}`),
leftPad1Maybe(`${dryRun ? '--dry-run' : ''}`)
].join('');
try {
execSyncInherit(npmPublishCommand);
logSuccess(`Successfully published latest version${dryRunMaybe()}.`);
} catch (e) {
logError(
`Couldn't publish version on npm${dryRunMaybe()}.`,
`Please check the error above.`
);
throw new Error();
}
logInfo(
`Pushing release commit and tag to origin '${origin}' on branch`,
`'${branch}'${dryRunMaybe()}...`
);
const gitPushCommand = [
`git push --follow-tags ${origin} ${branch}`,
leftPad1Maybe(`${dryRun ? '--dry-run' : ''}`)
].join('');
try {
execSyncInherit(gitPushCommand);
logSuccess(`Successfully pushed${dryRunMaybe()}.`);
} catch (e) {
logError(
`Couldn't push${dryRunMaybe()}. Please check the error above.`
);
throw new Error();
}
logSuccess(`Woohoo! Done${dryRunMaybe()}.`);
} catch (e) {
logError(
`Stopping right here${dryRunMaybe()}.`,
dryRun ? '' : `Make sure to clean up commits and tags as necessary.`
);
process.exit(1);
}
})();