forked from CodeVachon/next-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·279 lines (242 loc) · 8.6 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
#!/usr/bin/env node
const inquirer = require("inquirer");
const chalk = require("chalk");
const figlet = require("figlet");
const simpleGit = require("simple-git/promise")();
const path = require("path");
const fs = require("fs");
const extend = require("extend");
const incrementVersionBy = require("./libs/incrementVersionBy");
const suggestedBranchName = require("./libs/suggestedBranchName");
const stdout = (string, color, bigOut) => {
if (bigOut) {
string = figlet.textSync(string, {
font: "Colossal"
});
}
console.info(chalk.keyword(color || "orange")(string));
} // close stdout
const stdErr = (string) => {
console.error(chalk.red("\n\nError\n=====\n\n") + string);
process.exit(1);
} // close stdErr
const init = () => {
stdout("Next", null, true);
stdout("Version", null, true);
} // close init
const askAQuestion = async (options) => {
const resultOfQustion = await inquirer.prompt([extend(false, {
name: "answer"
}, options)]);
return resultOfQustion.answer;
} // close
const isGitClean = (status) => {
let isClean = true;
[
"not_added",
"conflicted",
"created",
"deleted",
"modified",
"renamed",
"files",
"staged"
].forEach(key => {
if (
!Array.isArray(status[key]) ||
(status[key].length > 0)
) {
isClean = false;
}
});
return isClean;
} // close isGitClean
const readFile = (atPath) => {
return new Promise((resolve, reject) => {
fs.readFile(atPath, "utf8", (error, contents) => {
if (error) {
reject(error);
} else {
if (/\.json$/i.test(atPath)) {
resolve(JSON.parse(contents));
} else {
resolve(contents);
}
}
});
});
} // close readFile
const writeFile = (atPath, contents) => {
return new Promise((resolve, reject) => {
fs.writeFile(atPath, contents, (error, ok) => {
if (error) {
reject(error);
} else {
resolve(ok);
}
});
});
} // close writeFile
const updateFileset = (pkgFilePath, pkg, workingDir) => {
stdout(`Update ${ chalk.yellow("package.json") }`);
return writeFile(pkgFilePath, JSON.stringify(pkg, null, " ".repeat(2))).then(() => {
// Do Other THings...
// Like Change Log
// Like VERSION File
return new Promise((resolve, reject) => {
fs.readdir(workingDir, (error, contents) => {
if (error) {
reject(error);
} else {
resolve(contents);
}
});
}).then(async (ls) => {
const getFileName = (ls, pattern, options) => {
return ls.filter(filename => new RegExp(pattern, options).test(filename.toLowerCase()));
} // close getFileName
const hasFile = (ls, pattern, options) => {
return (getFileName(ls, pattern, options).length > 0);
} // close hasFile
if (hasFile(ls, "changelog(\.md)?")) {
const newLine = "\r\n";
const changelogFile = getFileName(ls, "changelog(\.md)?")[0];
const changelogPath = `${workingDir}${path.sep}${changelogFile}`
stdout(`Update ${chalk.yellow(changelogFile)}`);
await readFile(changelogPath).then(contents => {
const updatedContents = contents.replace(/^((?:#\s)?[^\n\r]{1,}(?:\n|\r){1,}(?:={3,}(?:\n|\r){1,})?)/, `$1${ newLine.repeat(2) }## ${ pkg.version }${ newLine.repeat(1) }- ${ newLine.repeat(2) }`).replace(/(\n|\r){3,}/g, newLine.repeat(2));
return writeFile(changelogPath, updatedContents);
});
}
});
});
} // close updateFileset
const run = async () => {
init();
const pwd = await askAQuestion({
type: "text",
message: "What is the Directory Path to the project?",
default: process.cwd()
});
const expectedWorkingBranch = await askAQuestion({
type: "list",
message: "What is the Default Branch for the project?",
choices: ["main", "master"],
default: "main"
});
const incValue = await askAQuestion({
type: "list",
message: "What would you like to increment?",
choices: ["Major", "Minor", "Patch"],
default: "Minor"
});
const createReleaseBranch = await askAQuestion({
type: "confirm",
message: "Would you like to auto-create and push a release branch?",
default: true
});
try {
await simpleGit.cwd(pwd);
} catch(e) {
stdErr(e.message);
}
const isRepo = await simpleGit.checkIsRepo();
if (!isRepo) {
stdErr(`invalid repository ${ pwd }. can not continue`);
}
const gitStatus = await simpleGit.status();
if (!isGitClean(gitStatus)) {
stdout(`Your current branch [${chalk.blue(gitStatus.current)}] is not clean.`);
const shouldIResetHard = await askAQuestion({
type: "confirm",
message: `Would you like perform "git reset --hard"?`,
default: false
});
if (shouldIResetHard) {
stdout(`-- git reset --hard`);
try {
await simpleGit.reset("hard");
} catch (e) {
stdErr(e.message);
}
} else {
stdErr(`git is dirty, can not continue`);
}
}
if (gitStatus.current != expectedWorkingBranch) {
stdout(`Your current branch is ${ chalk.red(gitStatus.current) }. expected ${ chalk.green(expectedWorkingBranch) }`);
const shouldIChangeBranches = await askAQuestion({
type: "confirm",
message: `Would you Like to Change to the ${expectedWorkingBranch} branch?`,
default: false
});
if (shouldIChangeBranches == true) {
stdout(`-- git checkout ${expectedWorkingBranch}`);
try {
await simpleGit.checkout(expectedWorkingBranch);
} catch (e) {
stdErr(e.message);
}
} else {
stdErr(`Can Not Complete Action on branch: ${gitStatus.current}`);
}
}
stdout(`-- git pull`);
try {
await simpleGit.pull();
} catch (e) {
stdErr(e.message);
}
const thisPkgFile = `${pwd}${path.sep}package.json`;
const thispkg = await readFile(thisPkgFile);
const newVersion = incrementVersionBy(thispkg.version, incValue);
stdout(`Change version from ${ chalk.red(thispkg.version) } to ${ chalk.green(newVersion) }`);
thispkg.version = newVersion;
if (createReleaseBranch) {
// Set the Remote to Upload To
let gitOrigin = "";
const repoRemotes = await simpleGit.getRemotes();
if (repoRemotes.length > 1) {
gitOrigin = await askAQuestion({
type: "list",
message: `Which Origin would you like to use?`,
choices: repoRemotes.map(remote => remote.name),
default: repoRemotes[0].name
});
} else {
gitOrigin = repoRemotes[0].name;
}
// Set the New Branch Name
const newBranchName = await askAQuestion({
message: "How should the new branch be named?",
default: suggestedBranchName(incValue, newVersion)
});
stdout(`-- git checkout -b ${newBranchName}`);
try {
await simpleGit.checkoutBranch(newBranchName, expectedWorkingBranch);
} catch(e) {
stdErr(e.message);
}
// Update the Files
await updateFileset(thisPkgFile, thispkg, pwd);
// Add newly updated files
try {
const newStatus = await simpleGit.status();
await simpleGit.add(newStatus.files.map(thisfile => {
stdout(`-- git add ${thisfile.path}`);
return thisfile.path
}));
stdout(`-- git commit -m "set version to ${newVersion} [next-version]"`);
await simpleGit.commit(`set version to ${newVersion} [next-version]`);
// Push to Remote
stdout(`-- git push -u ${gitOrigin} ${newBranchName}`);
await simpleGit.push(["-u", gitOrigin, newBranchName]);
} catch (e) {
stdErr(e.message);
}
} else {
await updateFileset(thisPkgFile, thispkg, pwd);
} // clsoe if (createReleaseBranch)
stdout("\nSuccess!", "green");
} // close run
run();