Browse Source

release script

Daniel Zlotin 7 years ago
parent
commit
d2b5f9d316
2 changed files with 81 additions and 0 deletions
  1. 2
    0
      scripts/.npmrc
  2. 79
    0
      scripts/release.js

+ 2
- 0
scripts/.npmrc View File

@@ -0,0 +1,2 @@
1
+email=${NPM_EMAIL}
2
+//registry.npmjs.org/:_authToken=${NPM_TOKEN}

+ 79
- 0
scripts/release.js View File

@@ -0,0 +1,79 @@
1
+const cp = require('child_process');
2
+const p = require('path');
3
+const semver = require('semver');
4
+
5
+function execSync(cmd) {
6
+  cp.execSync(cmd, {stdio: ['inherit', 'inherit', 'inherit']});
7
+}
8
+
9
+function execSyncRead(cmd) {
10
+  return String(cp.execSync(cmd, {stdio: ['inherit', 'pipe', 'inherit']})).trim();
11
+}
12
+
13
+function execSyncSilently(cmd) {
14
+  cp.execSync(cmd, {stdio: ['ignore', 'ignore', 'ignore']});
15
+}
16
+
17
+function validateEnv() {
18
+  if (!process.env.CI || !process.env.TRAVIS) {
19
+    throw new Error(`releasing is only available from Travis CI`);
20
+  }
21
+
22
+  if (process.env.TRAVIS_BRANCH !== 'master') {
23
+    console.error(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
24
+    return false;
25
+  }
26
+
27
+  if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
28
+    console.error(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
29
+    return false;
30
+  }
31
+
32
+  return true;
33
+}
34
+
35
+function setupGit() {
36
+  execSyncSilently(`git config --global push.default simple`);
37
+  execSyncSilently(`git config --global user.email "${process.env.GIT_EMAIL}"`);
38
+  execSyncSilently(`git config --global user.name "${process.env.GIT_USER}"`);
39
+  const remoteUrl = new RegExp(`https?://(\\S+)`).exec(execSyncRead(`git remote -v`))[1];
40
+  execSyncSilently(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
41
+  execSync(`git checkout master`);
42
+}
43
+
44
+function calcNewVersion() {
45
+  const latestVersion = execSyncRead(`npm view ${process.env.npm_package_name}@next version`);
46
+  console.log(`latest version is: ${latestVersion}`);
47
+  const packageJsonVersion = process.env.npm_package_version;
48
+  console.log(`package.json version is: ${packageJsonVersion}`);
49
+  const diff = semver.diff(packageJsonVersion, latestVersion);
50
+  if (diff === 'major' || diff === 'minor') {
51
+    return packageJsonVersion;
52
+  } else {
53
+    return semver.inc(latestVersion, 'patch');
54
+  }
55
+}
56
+
57
+function copyNpmRc() {
58
+  const npmrcPath = p.resolve(`${__dirname}/.npmrc`);
59
+  execSync(`cp -rf ${npmrcPath} .`);
60
+}
61
+
62
+function tagAndPublish(newVersion) {
63
+  console.log(`new version is: ${newVersion}`);
64
+  execSync(`npm version ${newVersion} -m "v${newVersion} [ci skip]"`);
65
+  execSyncSilently(`git push deploy --tags`);
66
+  execSync(`npm publish`);
67
+}
68
+
69
+function run() {
70
+  if (!validateEnv()) {
71
+    return;
72
+  }
73
+  setupGit();
74
+  copyNpmRc();
75
+  tagAndPublish(calcNewVersion());
76
+}
77
+
78
+run();
79
+