-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
index.js
299 lines (290 loc) · 12.8 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const inquirer = require("inquirer");
const fs = require("fs").promises;
const git = require("simple-git/promise")();
const openurl = require("openurl");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const retrieveInformation = require("./information/retrieve");
const {Octokit} = require("@octokit/rest");
const octokit = new Octokit({
"auth": process.env.GITHUBAUTH
});
const path = require("path");
const ora = require("ora");
const os = require("os");
const npmFetch = require("npm-registry-fetch");
let package = require("../package.json");
(async function main () {
console.log("Welcome to the Dynamoose Publisher!\n\n\n");
if (!await checkCleanWorkingDir()) {
console.error("You must have a clean working directory in order to use this tool.");
console.error("Exiting.\n");
process.exit(1);
}
if (!process.env.GITHUBAUTH) {
console.error("You must set `GITHUBAUTH` in order to use this tool.");
console.error("Exiting.\n");
process.exit(1);
}
const originalBranch = (await git.status()).current;
let results = await inquirer.prompt([
{
"name": "branch",
"type": "list",
"message": "What branch would you like to publish?",
"choices": (await git.branchLocal()).all,
"default": originalBranch
}
]);
await git.checkout(results.branch);
package = require("../package.json");
const gitCoreEditor = await exec("git config --get core.editor");
const workspacePackages = require("./workspacePackages");
results = { // eslint-disable-line require-atomic-updates
...results,
...await inquirer.prompt([
{
"name": "version",
"type": "input",
"message": "What version would you like to publish?",
"default": package.version,
"validate": (val) => val !== package.version ? true : `${val} is the current version in the package.json. Please pick a new version to publish.`
},
{
"name": "isPrerelease",
"type": "confirm",
"message": "Is this version a prerelease version?",
"default": (res) => retrieveInformation(res.version).isPrerelease
},
{
"name": "workspacePackagesToPublish",
"type": "checkbox",
"message": "Which sub-packages would you like to publish?",
"choices": workspacePackages,
"default": workspacePackages
},
{
"name": "textEditor",
"type": "input",
"message": "What is the command line bin to launch your favorite text editor? (ex. `code`, `atom`, `nano`, etc.)",
"default": gitCoreEditor.stdout
// "validate": // TODO: ensure the command line thing exists and is valid (maybe by using `which` and checking to see if the output of that exists and is not `_____ not found`)
}
])
};
process.stdin.resume();
console.log(retrieveInformation(results.version));
results = {
...results,
...await inquirer.prompt([
{
"name": "confirm",
"type": "confirm",
"message": "Does all of the information look correct?",
"default": false
}
])
};
if (!results.confirm) {
console.error("No action has been taken.");
console.error("Exiting.\n");
process.exit(1);
}
// Create new branch
const branch = `version/${results.version}`;
const branchSpinner = ora(`Creating branch ${branch}`).start();
await git.checkoutBranch(branch, results.branch);
branchSpinner.succeed(`Created branch ${branch}`);
// Update version in package.json
const updateVersion = async (file) => {
const currentPath = path.join(__dirname, "..", file);
let fileContents = await fs.readFile(currentPath);
const fileContentsJSON = JSON.parse(fileContents);
fileContentsJSON.version = results.version;
if (fileContentsJSON.packages && fileContentsJSON.packages[""]) {
fileContentsJSON.packages[""].version = results.version;
}
fileContents = JSON.stringify(fileContentsJSON, null, 2);
await fs.writeFile(currentPath, `${fileContents}\n`);
};
const packageFiles = ["package.json"/*, "package-lock.json"*/];
const packageUpdateVersionsSpinner = ora(`Updating versions in ${packageFiles.join(" & ")} files`).start();
await Promise.all(packageFiles.map(updateVersion));
packageUpdateVersionsSpinner.succeed(`Updated versions in ${packageFiles.join(" & ")} files`);
// Add & Commit files to Git
const gitCommitPackage = ora("Committing files to Git").start();
await git.commit(`Bumping version to ${results.version}`, packageFiles.map((file) => path.join(__dirname, "..", file)));
gitCommitPackage.succeed("Committed files to Git");
for (const workspacePackage of results.workspacePackagesToPublish) {
const packageUpdateVersionsSpinnerSub = ora(`[${workspacePackage}] Updating versions in ${packageFiles.join(" & ")} files`).start();
const subPackageFiles = packageFiles.map((file) => `workspaces/${workspacePackage}/${file}`);
await Promise.all(subPackageFiles.map(updateVersion));
packageUpdateVersionsSpinnerSub.succeed(`[${workspacePackage}] Updated versions in ${packageFiles.join(" & ")} files`);
const primaryPackageUpdateVersionsSpinnerSub = ora(`[${workspacePackage}] Updating package.json files to point to new version`).start();
const packageJSON = require(`../workspaces/${workspacePackage}/package.json`);
const packages = ["", ...results.workspacePackagesToPublish.filter((pkg) => pkg !== workspacePackage).map((pkg) => `workspaces/${pkg}`)];
for (const pkg of packages) {
const currentPath = path.join(__dirname, "..", pkg, "package.json");
let fileContents = await fs.readFile(currentPath);
const primaryFileContentsJSON = JSON.parse(fileContents);
const dependenciesArray = ["dependencies", "devDependencies"];
dependenciesArray.forEach((dependencyName) => {
if (primaryFileContentsJSON[dependencyName] && primaryFileContentsJSON[dependencyName][workspacePackage]) {
primaryFileContentsJSON[dependencyName][workspacePackage] = packageJSON.version;
}
});
if (primaryFileContentsJSON.peerDependencies) {
if (primaryFileContentsJSON.peerDependencies[workspacePackage]) {
primaryFileContentsJSON.peerDependencies[workspacePackage] = packageJSON.version;
}
if (primaryFileContentsJSON.peerDependencies["dynamoose"]) {
primaryFileContentsJSON.peerDependencies["dynamoose"] = packageJSON.version;
}
}
await fs.writeFile(currentPath, `${JSON.stringify(primaryFileContentsJSON, null, 2)}\n`);
}
primaryPackageUpdateVersionsSpinnerSub.succeed(`[${workspacePackage}] Updated package.json files to point to new version`);
// Add & Commit files to Git
const gitCommitPackageSub = ora("Committing files to Git").start();
await git.commit(`Bumping version to ${results.version} for ${workspacePackage} package`, subPackageFiles.map((file) => path.join(__dirname, "..", file)));
gitCommitPackageSub.succeed("Committed files to Git");
}
const versionInfo = retrieveInformation(results.version);
const versionParts = versionInfo.main.split(".");
const shouldUpdateAllMinorVersions = versionParts.length === 3 && versionParts[2] === "0"; // If true it will update cases of `x.x` instead of `x.x.x` (ignoring patch version) as well
// Update README
if (!versionInfo.isPrerelease) {
const readmePath = path.join(__dirname, "..", "README.md");
const readmeFileContents = await fs.readFile(readmePath, "utf8");
if (readmeFileContents.includes(package.version)) {
let newREADME = readmeFileContents.replaceAll(package.version, results.version);
if (shouldUpdateAllMinorVersions) {
let oldVersionParts = package.version.split(".");
newREADME = newREADME.replaceAll(`${oldVersionParts[0]}.${oldVersionParts[1]}`, `${versionParts[0]}.${versionParts[1]}`);
}
await fs.writeFile(readmePath, `${newREADME}\n`);
const readmeUpdateVersionsSpinner = ora("Updating version in README.md").start();
readmeUpdateVersionsSpinner.succeed("Updated version in README.md");
// Add & Commit files to Git
const gitCommitReadme = ora("Committing files to Git").start();
await git.commit(`Updating README to ${results.version}`, ["README.md"].map((file) => path.join(__dirname, "..", file)));
gitCommitReadme.succeed("Committed files to Git");
} else {
const readmeUpdateVersionsSpinner = ora("Nothing to update in README.md").start();
readmeUpdateVersionsSpinner.succeed("Nothing to update in README.md");
}
}
// Push to GitHub
const gitPush = ora("Pushing files to GitHub").start();
await git.push("origin", branch);
gitPush.succeed("Pushed files to GitHub");
// Changelog
console.log("This tool will now open a web browser with a list of commits since the last version.\nPlease use this information to fill out a change log.\n");
console.log("Press any key to proceed.");
await keypress();
openurl.open(`https://github.com/dynamoose/dynamoose/compare/v${package.version}...${results.branch}`);
// await exec("npm i");
const utils = require("../dist/utils").default;
const versionFriendlyTitle = `Version ${[versionInfo.main, versionInfo.tag ? utils.capitalize_first_letter(versionInfo.tag) : "", versionInfo.tagNumber].filter((a) => Boolean(a)).join(" ")}`;
const changelogFilePath = path.join(os.tmpdir(), `${results.version}-changelog.md`);
let changelogTemplate = `## ${versionFriendlyTitle}`;
if (!versionInfo.isPrerelease) {
changelogTemplate += `\n\n${await fs.readFile(path.join(__dirname, "CHANGELOG_TEMPLATE.md"), "utf8")}`;
}
await fs.writeFile(changelogFilePath, changelogTemplate);
await exec(`${results.textEditor.trim()} ${changelogFilePath.trim()}`);
const pendingChangelogSpinner = ora("Waiting for user to finish changelog, press enter to continue.").start();
await keypress();
pendingChangelogSpinner.succeed("Finished changelog");
const versionChangelog = (await fs.readFile(changelogFilePath, "utf8")).trim();
if (!versionInfo.isPrerelease) {
const existingChangelog = await fs.readFile(path.join(__dirname, "..", "CHANGELOG.md"), "utf8");
const existingChangelogArray = existingChangelog.split("\n---\n");
existingChangelogArray.splice(1, 0, `\n${versionChangelog}\n`);
await fs.writeFile(path.join(__dirname, "..", "CHANGELOG.md"), existingChangelogArray.join("\n---\n"));
const gitCommit2 = ora("Committing files to Git").start();
await git.commit(`Adding changelog for ${results.version}`, [path.join(__dirname, "..", "CHANGELOG.md")]);
gitCommit2.succeed("Committed files to Git");
const gitPush2 = ora("Pushing files to GitHub").start();
await git.push("origin", branch);
gitPush2.succeed("Pushed files to GitHub");
}
// Create PR
const gitPR = ora("Creating PR on GitHub").start();
const labels = [versionInfo.isPrerelease ? "type:prerelease" : "type:version"];
const pr = (await octokit.pulls.create({
"owner": "dynamoose",
"repo": "dynamoose",
"title": versionFriendlyTitle,
"body": versionChangelog,
"labels": labels.join(","),
"head": branch,
"base": results.branch
})).data;
gitPR.succeed(`Created PR ${pr.number} on GitHub`);
openurl.open(`https://github.com/dynamoose/dynamoose/pull/${pr.number}`);
// Poll for PR to be merged
const gitPRPoll = ora(`Polling GitHub for PR ${pr.number} to be merged`).start();
await isPRMerged(pr.number);
gitPRPoll.succeed(`PR ${pr.number} has been merged`);
// Create release
const gitRelease = ora("Creating release on GitHub").start();
await octokit.repos.createRelease({
"owner": "dynamoose",
"repo": "dynamoose",
"tag_name": `v${results.version}`,
"target_commitish": results.branch,
"name": `v${results.version}`,
"body": versionChangelog,
"prerelease": versionInfo.isPrerelease
});
gitRelease.succeed("GitHub release created");
// Poll NPM for release
const npmPoll = ora("Polling NPM for release").start();
await isReleaseSubmitted(results.version);
npmPoll.succeed("Version successfully published to NPM");
// Restore Git to original state
const gitCheckoutOriginal = ora(`Checking out ${originalBranch} branch`).start();
await git.checkout(originalBranch);
gitCheckoutOriginal.succeed(`Checked out ${originalBranch} branch`);
const gitDeleteNewBranch = ora(`Deleting ${branch} branch`).start();
// TODO: using `raw` instead of `deleteLocalBranch` until https://github.com/steveukx/git-js/issues/441 gets fixed
// await git.deleteLocalBranch(branch);
await git.raw(["branch", "-D", branch]);
gitDeleteNewBranch.succeed(`Deleted ${branch} branch`);
// Complete
process.exit(0);
async function isPRMerged (pr) {
let data;
do {
data = (await octokit.pulls.get({
"owner": "dynamoose",
"repo": "dynamoose",
"pull_number": pr
})).data;
await utils.timeout(5000);
} while (!data.merged);
}
async function isReleaseSubmitted (release) {
try {
await npmFetch(`/dynamoose/${release}`);
} catch (e) {
await utils.timeout(5000);
return isReleaseSubmitted(release);
}
}
})();
async function checkCleanWorkingDir () {
return (await git.status()).isClean();
}
function keypress () {
process.stdin.resume();
process.stdin.setRawMode(true);
return new Promise((resolve) => {
process.stdin.once("data", () => {
process.stdin.setRawMode(false);
resolve();
process.stdin.pause();
});
});
}