1 | 'use strict';
|
2 |
|
3 | const Bluebird = require(`bluebird`);
|
4 | const debug = require(`debug`)(`npm-publish-git-tag`);
|
5 | const gitLatestSemverTag = Bluebird.promisify(require(`git-latest-semver-tag`));
|
6 | const readPkg = require(`read-pkg`);
|
7 | const semver = require(`semver`);
|
8 | const setNpmAuthTokenForCI = require(`@hutson/set-npm-auth-token-for-ci`);
|
9 | const shell = require(`shelljs`);
|
10 | const writePkg = require(`write-pkg`);
|
11 |
|
12 | module.exports = deployGitTag(shell);
|
13 | module.exports.deployGitTag = deployGitTag;
|
14 |
|
15 | function deployGitTag (shell) {
|
16 | return options =>
|
17 | gitLatestSemverTag()
|
18 | .then(latestTag => {
|
19 | debug(`latest semver tag retrieved from disk is ${latestTag} `);
|
20 | if (semver.valid(latestTag)) {
|
21 | return latestTag;
|
22 | }
|
23 | throw new Error(`No valid semantic version tag available for deploying.`);
|
24 | })
|
25 | .then(updateVersion)
|
26 | .then(() => options.skipToken || setToken())
|
27 | .then(() => deploy({ access: options.access }));
|
28 |
|
29 | function updateVersion (version) {
|
30 | debug(`updating version in package.json to ${version}`);
|
31 |
|
32 | return readPkg().then(pkg => writePkg(Object.assign(pkg, { version })));
|
33 | }
|
34 |
|
35 | function setToken () {
|
36 | if (!process.env.NPM_TOKEN) {
|
37 | throw new Error(`Cannot find NPM_TOKEN set in your environment.`);
|
38 | }
|
39 | setNpmAuthTokenForCI();
|
40 | }
|
41 |
|
42 | function deploy (options) {
|
43 | let command = `npm publish`;
|
44 |
|
45 | if (typeof options.access === `string`) {
|
46 | debug(`publishing package with the following access level`, options.access);
|
47 | command += ` --access ${options.access}`;
|
48 | }
|
49 |
|
50 | debug(`executing publish command - ${command}`);
|
51 | const result = shell.exec(command, { silent: true });
|
52 |
|
53 | if (result.code !== 0) {
|
54 | throw new Error(result.stderr);
|
55 | }
|
56 |
|
57 | return true;
|
58 | }
|
59 | }
|