forked from uber/baseweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-to-npm.js
113 lines (96 loc) · 3.22 KB
/
publish-to-npm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (c) 2018-2020 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// Sets local package.json to current baseui version.
/* eslint-env node */
// @flow
const fs = require('fs');
const path = require('path');
const {spawnSync} = require('child_process');
const ROOT_DIR = path.resolve(__dirname, '..');
const ESLINT_PLUGIN_DIR = path.resolve(
ROOT_DIR,
'packages',
'eslint-plugin-baseui',
);
const ALPHA_TAG = 'alpha';
const LATEST_TAG = 'latest';
function writeNpmTokenFromEnv() {
const token = process.env.NPM_TOKEN;
if (!token) {
throw new Error('NPM_TOKEN not found.');
}
const filepath = path.resolve(ROOT_DIR, '.npmrc');
const filePathEslintPlugin = path.resolve(ESLINT_PLUGIN_DIR, '.npmrc');
fs.unlinkSync(filepath);
fs.writeFileSync(filepath, `//registry.npmjs.org/:_authToken=${token}`);
fs.writeFileSync(
filePathEslintPlugin,
`//registry.npmjs.org/:_authToken=${token}`,
);
}
function readJSONFile(filepath) {
const contents = fs.readFileSync(filepath, 'utf8');
return JSON.parse(contents);
}
function copyPackageJSONVersionFromRoot(filepath) {
if (!filepath.endsWith('package.json')) {
throw new Error(
`Must copy version to a package.json file. Received: ${filepath}`,
);
}
const source = readJSONFile(path.resolve(ROOT_DIR, 'package.json'));
const dest = readJSONFile(filepath);
dest.version = source.version;
fs.writeFileSync(filepath, JSON.stringify(dest, null, 2));
}
function publishBaseui(tag) {
console.log('--- Publishing baseui to NPM');
spawnSync('yarn', ['build'], {stdio: 'inherit', cwd: ROOT_DIR});
spawnSync('npm', ['publish', 'dist', '--tag', tag], {
stdio: ['inherit', 'inherit', 'pipe'],
cwd: ROOT_DIR,
});
}
function publishEslintPlugin(tag) {
console.log('--- Publishing eslint-plugin-baseui to NPM');
copyPackageJSONVersionFromRoot(
path.resolve(ESLINT_PLUGIN_DIR, 'package.json'),
);
spawnSync('npm', ['publish', '--tag', tag], {
stdio: ['inherit', 'inherit', 'pipe'],
cwd: ESLINT_PLUGIN_DIR,
});
}
const rootPackageJSONPath = path.resolve(ROOT_DIR, 'package.json');
module.exports = function publishToNpm(params /*: any */) {
const {tag, commit} = params;
if (tag !== ALPHA_TAG && tag !== LATEST_TAG) {
throw new Error(
`NPM tag ${tag} must be either ${ALPHA_TAG} or ${LATEST_TAG}.`,
);
}
if (tag === ALPHA_TAG) {
console.log('+++ Updating package.json version to alpha.');
if (!commit) {
throw new Error(
'Must provide a commit param to publish an alpha release.',
);
}
const packageJSON = readJSONFile(rootPackageJSONPath);
const shortHash = commit.slice(-7);
packageJSON.version = `0.0.0-alpha-${shortHash}`;
console.log(
`Updated package.json version to ${packageJSON.version} for alpha release.`,
);
fs.writeFileSync(rootPackageJSONPath, JSON.stringify(packageJSON, null, 2));
}
writeNpmTokenFromEnv();
publishBaseui(tag);
publishEslintPlugin(tag);
// returns the final version so that alpha-test-web-code script can kick off a test with the release
const packageJSON = readJSONFile(rootPackageJSONPath);
return packageJSON.version;
};