-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-package.js
137 lines (111 loc) · 4.11 KB
/
publish-package.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
checkMajorNodeVersion();
const availableNpmTags = new Set(['latest', 'rc', 'dev']);
const verRegEx = /^(?<base>\d+\.\d+\.\d+)(?:$|-(?<tag>\w+)\.(?<tagVersion>\d+)$)/;
const root = path.join.bind(path, __dirname);
const { env, isProd, isDev } = getEnv();
const allVersions = getAllVersions();
const { currentBaseVersion, nextBaseVersion } = getBaseVersions();
const variations = allVersions.filter(({ base }) => base === currentBaseVersion);
const isBaseVersionPublished = variations.some(v => !v.tag && !v.tagVersion);
const findNextByTag = (tag, useNextBaseVersion = true) => allVersions
.filter(({ base }) => base === (useNextBaseVersion ? nextBaseVersion : currentBaseVersion))
.filter(v => v.tag === tag)
.map(v => +v.tagVersion)
.concat(-1)
.reduce((a, b) => Math.max(a, b)) + 1;
if (isProd) {
if (isBaseVersionPublished) {
const nextRcTag = findNextByTag('rc');
const version = makeVersion(nextBaseVersion, 'rc', nextRcTag);
saveNewVersionToDist(version);
publish('rc');
} else {
const distPackageJsonPath = root('dist', 'package.json');
if (fs.existsSync(distPackageJsonPath) && require(distPackageJsonPath).version !== currentBaseVersion) {
// in the most cases the version is the same, however after deployment in 'dev' mode the version
// changed until creating new build and should be overwrote for the 'prod' build mode
saveNewVersionToDist(currentBaseVersion);
}
publish();
}
} else if (isDev) {
const nextDevTag = findNextByTag('dev', isBaseVersionPublished);
const version = makeVersion(
isBaseVersionPublished ? nextBaseVersion : currentBaseVersion,
'dev',
nextDevTag,
);
saveNewVersionToDist(version);
publish('dev');
} else {
throw new Error(`Unsupported environment: '${env}'`);
}
//
// functions
//
function makeVersion(base, tag, tagVersion) {
return `${base}-${tag}.${tagVersion}`;
}
function getEnv() {
const rawEnv = process.env.ENV || process.env.NODE_ENV;
let env;
switch (rawEnv) {
case 'prod':
case 'production':
env = 'production';
break;
case 'dev':
case 'development':
env = 'development';
break;
default:
throw new Error(`Wrong environment: '${rawEnv}'`);
}
const isProd = env === 'production';
const isDev = env === 'development';
return {env, isProd, isDev};
}
function publish(npmTag = 'latest') {
if (!availableNpmTags.has(npmTag)) {
throw new Error(`Wrong npm tag: ${npmTag}`);
}
execSync(`npm publish dist --access public --tag ${npmTag}`);
}
function saveNewVersionToDist(version) {
const originalPackageJson = require(root('package.json'));
const packageJson = { ...originalPackageJson, version };
const packageJsonStr = JSON.stringify(packageJson, null, 2);
fs.writeFileSync(root('dist', 'package.json'), packageJsonStr);
}
function getAllVersions() {
const output = execSync('npm view . versions --json');
const allVersions = JSON.parse(output.toString());
return allVersions
.map(v => v.match(verRegEx))
.map(v => v && v.groups)
.filter(Boolean)
.map(({ base, tag, tagVersion }) => ({ base, tag, tagVersion }));
}
function getBaseVersions() {
const packageJson = require(root('package.json'));
const currentVersion = packageJson.version;
const info = currentVersion && String(currentVersion).match(verRegEx);
if (!info || !info.groups) {
throw new Error(`Wrong current version: ${currentVersion}`);
}
const { base, tag, tagVersion } = info.groups;
if (tag !== undefined || tagVersion !== undefined) {
throw new Error(`Current version (${base}) should not have tag and tag version: ${tag}.${tagVersion}`);
}
const next = base.replace(/^(\d+)\.(\d+)\.(\d+)$/, (all, major, minor, patch) => `${major}.${minor}.${+patch + 1}`);
return { currentBaseVersion: base, nextBaseVersion: next };
}
function checkMajorNodeVersion(expected) {
const actual = +process.version.replace(/^v(\d+)\..*$/, '$1');
if (expected < actual) {
throw new Error(`Minimal node version is ${expected}, actual version is ${actual}`);
}
}