/
release.js
184 lines (155 loc) · 5.12 KB
/
release.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint-disable consistent-return */
const { exec } = require("child_process");
const fs = require("fs");
const inquirer = require("inquirer");
const promiseExec = (command, messageIfError) => {
try {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(messageIfError || error);
return;
}
resolve({ stdout, stderr });
});
});
} catch (exception) {
throw new Error(`[release.promiseExec] ${exception.message}`);
}
};
const releaseToNPM = async (version) => {
try {
await promiseExec(
`npm version ${version} --allow-same-version && npm publish --access public`
); // NOTE: --access public is due to the scoped package (@usehypothesis/js).
} catch (exception) {
throw new Error(`[release.releaseToNPM] ${exception.message}`);
}
};
const tagReleaseOnGit = async (version) => {
try {
await promiseExec(
`git tag -a ${version} -m "release ${version}" && git push origin ${version}`
);
} catch (exception) {
throw new Error(`[release.tagReleaseOnGit] ${exception.message}`);
}
};
const pushReleaseToGit = async () => {
try {
await promiseExec("git push origin master");
} catch (exception) {
throw new Error(`[release.pushReleaseToGit] ${exception.message}`);
}
};
const commitReleaseToRepo = async (version) => {
try {
await promiseExec(`git add . && git commit -m "release v${version}"`);
} catch (exception) {
throw new Error(`[release.commitReleaseToRepo] ${exception.message}`);
}
};
const runTests = async () => {
try {
await promiseExec(
"npm run test",
"❌ Tests failed! Run npm test and correct errors before releasing. \n"
);
} catch (exception) {
throw new Error(`[release.runTests] ${exception}`);
}
};
const runBuild = async () => {
try {
await promiseExec("npm run build");
} catch (exception) {
throw new Error(`[release.runBuild] ${exception.message}`);
}
};
const incrementVersion = (currentVersion, releaseType) => {
try {
// NOTE: Cast version parts as integers so we can do math on them below.
const versionParts = currentVersion
.split(".")
.map((versionPart) => parseInt(versionPart, 10));
if (currentVersion && releaseType && releaseType.includes("Major")) {
return `${versionParts[0] + 1}.0.0`;
}
if (currentVersion && releaseType && releaseType.includes("Minor")) {
return `${versionParts[0]}.${versionParts[1] + 1}.0`;
}
if (currentVersion && releaseType && releaseType.includes("Patch")) {
return `${versionParts[0]}.${versionParts[1]}.${versionParts[2] + 1}`;
}
return currentVersion;
} catch (exception) {
throw new Error(`[release.incrementVersion] ${exception.message}`);
}
};
const getCurrentVersionFromPackageJSON = (packageJSON) => {
try {
return packageJSON && packageJSON.version;
} catch (exception) {
throw new Error(
`[release.getCurrentVersionFromPackageJSON] ${exception.message}`
);
}
};
const getPackageLockJSON = () => {
try {
// NOTE: Reads relative to where the command was initially executed in the command line.
const packageLockJSON = fs.readFileSync("./package-lock.json", "utf-8");
return packageLockJSON && JSON.parse(packageLockJSON);
} catch (exception) {
throw new Error(`[release.getPackageLockJSON] ${exception.message}`);
}
};
const getPackageJSON = () => {
try {
// NOTE: Reads relative to where the command was initially executed in the command line.
const packageJSON = fs.readFileSync("./package.json", "utf-8");
return packageJSON && JSON.parse(packageJSON);
} catch (exception) {
throw new Error(`[release.getPackageJSON] ${exception.message}`);
}
};
const release = async (version) => {
try {
const packageJSON = getPackageJSON();
const currentVersion = getCurrentVersionFromPackageJSON(packageJSON);
if (!packageJSON) {
throw new Error(
"Could not find a package.json file for this project. Are you running this command in the right directory?"
);
}
if (!currentVersion) {
throw new Error(
"Could not find a current version in package.json. Try again or double-check your package.json has a version set."
);
}
const { releaseType } = await inquirer.prompt([
{
type: "list",
name: "releaseType",
message: "What type of release are you deploying?",
choices: ["Major (x.0.0)", "Minor (0.x.0)", "Patch (0.0.x)"],
},
]);
const version = incrementVersion(currentVersion, releaseType);
await runBuild();
console.log("✅ Build complete!");
await runTests();
console.log("✅ Tests passed!");
await commitReleaseToRepo(version);
console.log("✅ Release committed to repo!");
await pushReleaseToGit();
console.log("✅ Release pushed to repo!");
await tagReleaseOnGit(version);
console.log("✅ Version tag pushed to remote repo!");
await releaseToNPM(version);
console.log("✅ Released to NPM!");
} catch (exception) {
console.warn(`[release] ${exception.message}`);
}
};
release();