-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·365 lines (292 loc) · 13.3 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env node
'use strict'
const fs = require('fs-extra')
const path = require('path')
const shell = require("shelljs")
const semver = require('semver')
const args = require('yargs').argv
const prompts = require('prompts')
const chalk = require("chalk");
const replace = require("replace-in-file");
const packageVersionBump = async (vv) => {
const pkgPath = path.join(process.cwd(), 'package.json')
if (fs.existsSync(pkgPath)) {
let pkgContent;
try {
pkgContent = await fs.readJSON(pkgPath);
} catch (err) {
throw new Error(`Couldn't parse "package.json"`)
}
pkgContent.version = vv;
try {
await fs.writeJSON(pkgPath, pkgContent, {
spaces: 2
});
} catch (err) {
throw new Error(`Couldn't write to "package.json"`)
}
return true;
}
return true;
}
module.exports = function () {
(async () => {
const totalArguments = Object.values(args).length;
if (totalArguments > 5) {
await console.log(chalk.red.bold('Too many arguments!'))
return;
}
const haveOption = (
args.p ||
args.m ||
args.patch ||
args.minor ||
args.major ||
args.reverse ||
args.custom ||
args.info
)
if (!haveOption || args.h) {
await console.log(chalk.red.bold(`'Please specify the increment type') [-p, -m, --minor, --patch, --major, --reverse, --custom, --info, --soft]'
Options:
-p, --patch # Will increase the version from 1.0.0 to 1.0.1
-m, --minor # Will increase the version from 1.0.0 to 1.1.0
--major # Will increase the version from 1.0.0 to 2.0.0
--reverse # Will remove the last tag and revert to previously created one.
--info # Get some info about current project.
--custom # Define the new Semantic version manually.
--soft # Create a soft tag. This will not commit the changes to git or create a new git tag.
-h, --help # Show this message.
`));
await console.log(chalk.blue('Example: ') + chalk.yellow('tagy --patch'))
return;
}
if (args.m && args.a) {
await console.log(chalk.red.bold('Did you mean `--major`? Try again!'))
return;
}
if (args.r && args.e) {
await console.log(chalk.red.bold('Did you mean `--reverse`? Try again!'))
return;
}
// abort if multiple args are passed from this list [p, m, major, minor, patch]
// if in array then abort
const multipleArgs = ['p', 'm', 'major', 'minor', 'patch', 'reverse', 'custom', 'info'];
const multipleArgsPassed = multipleArgs.filter(arg => args[arg]);
if (multipleArgsPassed.length > 1) {
await console.log(chalk.red.bold('Too many arguments!'))
return;
}
// read package.json
const pkgPath = path.join(process.cwd(), 'package.json')
if (!fs.existsSync(pkgPath)) {
await console.log(chalk.red.bold('package.json not found. Make sure that you are in the right directory!'))
return;
}
// Get package.json configuration
let pkgContent;
try {
pkgContent = await fs.readJSON(pkgPath);
} catch (err) {
throw new Error(`Couldn't parse "package.json"`)
}
const tagPrefix = (pkgContent && pkgContent.tagy && pkgContent.tagy.tagPrefix) || '';
let vv;
// This will soft create a tag
// ----------------------------------------------------------------------------
const isSoft = args.soft || (pkgContent && pkgContent.tagy && (pkgContent.tagy.soft || pkgContent.tagy.method === 'soft'));
if (pkgContent && pkgContent.tagy && pkgContent.tagy.method === 'soft'){
console.log(chalk.red(`{"method": "soft"} is deprecated, please use {"soft": true} instead.`))
}
if (isSoft) {
console.log(chalk.green('➡️ Soft Processing!'));
}
let branchName;
// This will create the tag in git
// ----------------------------------------------------------------------------
try {
if (!isSoft) {
let currentBranchName = shell.exec("git branch | grep \\* | cut -d ' ' -f2", {silent: true}).stdout;
if (!currentBranchName) {
return console.log(chalk.red.bold('Can\'t determine the branch name!'))
}
branchName = currentBranchName.trim();
if (!(branchName === 'master' || branchName === 'main')) {
// await console.log(chalk.red.bold('You can create tags only from "master" branch.'))
// return;
const confirmBranch = await prompts({
type: 'confirm',
name: 'value',
message: `Current branch name is '${branchName}'. Do you want to tag this branch?`,
initial: false
});
if (!confirmBranch.value) {
return console.log(chalk.red.bold('Aborted! Please switch the branch.'))
}
}
shell.exec('git fetch --tags', {silent: true});
// vv = shell.exec('git tag --sort=v:refname | grep -E \'^[0-9]\' | tail -1', {silent: true}).stdout;
vv = shell.exec(`git tag --sort=v:refname | grep -E '^${tagPrefix}[0-9]' | tail -1`, {silent: true}).stdout;
if (args.info) {
if (!vv) {
await console.log(chalk.blue(`Looks like no tags were created by this moment.`))
} else {
await console.log(chalk.blue(`Last created tag is: ${vv}`))
}
return;
}
} else {
vv = pkgContent.version;
}
if (args.reverse) {
if (isSoft) {
return console.log(chalk.red(`Can't perform a reverse when the method is soft. Please reverse it manually.`))
}
if (!vv) {
await console.log(chalk.blue(`Looks like no tags were created by this moment. Nothing to delete.`))
} else {
const confirmReverse = await prompts({
type: 'confirm',
name: 'value',
message: `Are you sure that you want to remove this tag?(${vv.trim()})`,
initial: false
})
if (!confirmReverse.value) {
return console.log(chalk.red.bold('Aborted!'))
}
shell.exec(`git tag -d ${vv}`);
shell.exec(`git push origin :refs/tags/${vv}`);
await console.log(chalk.blue(`Tag ${vv} --> deleted!.`))
}
return;
}
let currentTag;
if (args.custom) {
const customVer = await prompts({
type: 'text',
name: 'value',
message: 'Please enter a custom version. Make sure to be valid according to semver.org standard.',
initial: false,
// validate: val => {
// if (!/\d+\.\d+\.\d+/g.test(val)) {
// return 'Invalid version!';
// }
//
// return true;
// }
validate: val => {
if (!new RegExp(`^\\d+\\.\\d+\\.\\d+$`).test(val)) {
return 'Invalid version!';
}
return true;
}
})
if (!customVer.value) {
return console.log(chalk.red.bold('Aborted!'))
}
vv = customVer.value;
currentTag = vv;
} else {
if (!vv) {
vv = '0.0.0';
}
vv = vv.trim();
if (semver.ltr(vv, '0.0.0')) {
vv = '0.0.0'
}
currentTag = vv;
if (args.p || args.patch) {
vv = semver.inc(vv, 'patch')
} else if (args.m || args.minor) {
vv = semver.inc(vv, 'minor')
} else if (args.major) {
vv = semver.inc(vv, 'major')
} else {
console.log(chalk.red(`Something went wrong!.`))
return;
}
if (args.major) {
const confirmMajorRelease = await prompts({
type: 'confirm',
name: 'value',
message: `Are you sure that you want to create a major release? Current tag is "${currentTag}" and the next will be "${vv}"`,
initial: false
})
if (!confirmMajorRelease.value) {
return console.log(chalk.red.bold('Aborted!'))
}
}
}
let canCreate
// Bump the version in package.json
try {
canCreate = await packageVersionBump(vv);
} catch (err) {
return console.log(err.message);
}
// Check for custom config inside of current directory.
const tagyExtraFile = path.resolve(`${process.cwd()}/tagy.js`);
try {
if (fs.existsSync(tagyExtraFile)) {
console.log('"tagy.js" file is found!');
const tagyExtra = require(tagyExtraFile)
await tagyExtra(vv, currentTag, args)
}
} catch (err) {
console.error(err)
}
// In some configurations, the replacements can be defined in the package.json.
const replacementMethods = pkgContent && pkgContent.tagy && pkgContent.tagy.replace && Array.isArray(pkgContent.tagy.replace) ? pkgContent.tagy.replace : [];
if (replacementMethods.length > 0) {
console.log(chalk.green('♾️ Replacement methods are found!'));
for (let i = 0; i < replacementMethods.length; i++) {
const {files, from, to, flags} = replacementMethods[i];
if (files && from && to) {
const _files = Array.isArray(files) ? files : [files];
const replaceConf = {
files: _files.map(file => path.resolve(`${process.cwd()}/${file}`)),
from: new RegExp(from.replaceAll('__CURRENT_TAG__', currentTag).replaceAll('__VERSION__', vv), flags !== false ? (flags || 'g') : undefined),
to: to.replaceAll('__CURRENT_TAG__', currentTag).replaceAll('__VERSION__', vv),
};
replace.sync(replaceConf);
}
}
}
if (canCreate) {
if (!isSoft) {
await shell.exec(`git config --global core.autocrlf true`);// Replace CRLF with LF on Windows OS.
await shell.exec(`git config --global core.safecrlf false`);// Disable CRLF warnings.
// await shell.exec(`git commit -a -m "Release ${vv}"`);
// await shell.exec(`git push origin ${branchName}`);
// await shell.exec(`git tag ${vv}`);
// await shell.exec(`git push origin ${vv}`);
await shell.exec(`git commit -a -m "Release ${tagPrefix}${vv}"`);
await shell.exec(`git push origin ${branchName}`);
await shell.exec(`git tag ${tagPrefix}${vv}`);
await shell.exec(`git push origin ${tagPrefix}${vv}`);
// Check if github CLI is installed and create a release
const ghInstalled = shell.exec(`gh --version`, {silent: true}).stdout;
if (ghInstalled) {
const autoRelease = args['auto-release'] || (pkgContent && pkgContent.tagy && pkgContent.tagy['auto-release']);
let releaseIt = autoRelease;
if (!autoRelease) {
const ghRelease = await prompts({
type: 'confirm',
name: 'value',
message: `Do you want to create a release on GitHub?`,
initial: false
});
releaseIt = ghRelease.value;
}
if (releaseIt) {
await shell.exec(`gh release create ${tagPrefix}${vv} --title "${tagPrefix}${vv}" --notes "Release ${tagPrefix}${vv}"`)
}
}
}
await console.log(chalk.blue(`💥 Tag ${vv} --> created!.`))
}
} catch (e) {
console.log(e)
}
})()
}