UNPKG

1.78 kBJavaScriptView Raw
1'use strict';
2
3const Bluebird = require(`bluebird`);
4const debug = require(`debug`)(`npm-publish-git-tag`);
5const gitLatestSemverTag = Bluebird.promisify(require(`git-latest-semver-tag`));
6const readPkg = require(`read-pkg`);
7const semver = require(`semver`);
8const setNpmAuthTokenForCI = require(`@hutson/set-npm-auth-token-for-ci`);
9const shell = require(`shelljs`);
10const writePkg = require(`write-pkg`);
11
12module.exports = deployGitTag(shell);
13module.exports.deployGitTag = deployGitTag;
14
15function 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}