-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgulpfile.js
104 lines (86 loc) · 3.31 KB
/
gulpfile.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
const { readFileSync, writeFileSync } = require('node:fs');
const chalk = require('chalk');
const gulp = require('gulp');
const semver = require('semver');
// const { execa } = require('execa');
const argv = require('./tools/args-parser.js');
/* ------------------------------------------ */
/* Configuration */
/* ------------------------------------------ */
// const production = process.env.NODE_ENV === 'production';
const packageJson = JSON.parse(readFileSync('package.json', { encoding: 'utf-8' }));
const stdio = 'inherit';
/* ------------------------------------------ */
/**
* Gets the target version based on on the current version and the argument passed as release.
* @param {string} currentVersion The current version
* @param {semver.ReleaseType} releaseType The release type,
* any of `['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease']`
* @returns {string} The target version
*/
function getTargetVersion(currentVersion, releaseType) {
if (['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].includes(releaseType)) {
return semver.inc(currentVersion, releaseType);
}
else {
return semver.valid(releaseType);
}
}
/* ------------------------------------------ */
/**
* Creates a changelog.
*/
async function changelog() {
const { execa } = await import('execa');
await execa('npx', ['standard-version', '--skip.bump', '--skip.tag', '--skip.commit'], { stdio });
}
/**
* Commits and pushes release to Github Upstream.
*/
async function commitTagPush() {
const { execa } = await import('execa');
const { version } = packageJson;
const commitMsg = `chore(release): 🚀 Release v${version}`;
await execa('git', ['add', '-A'], { stdio });
await execa('git', ['commit', '--message', commitMsg], { stdio });
await execa('git', ['tag', `v${version}`], { stdio });
await execa('git', ['push'], { stdio });
await execa('git', ['push', '--tags'], { stdio });
}
/* ------------------------------------------ */
/**
* Updates version and download URL.
* @param {function} cb Callback function
* @throws {Error} When missing release type
* @throws {Error} When incorrect version arguments
* @throws {Error} When target version is identical to current version
*/
async function bumpVersion(cb) {
try {
// @ts-ignore
const releaseType = argv.release || argv.r;
const currentVersion = packageJson.version;
if (!releaseType) {
return cb(Error('Missing release type'));
}
const targetVersion = getTargetVersion(currentVersion, releaseType);
if (!targetVersion) {
return cb(new Error(chalk.red('Error: Incorrect version arguments')));
}
if (targetVersion === currentVersion) {
return cb(new Error(chalk.red('Error: Target version is identical to current version')));
}
console.log(`Updating version number to '${targetVersion}'`);
packageJson.version = targetVersion;
writeFileSync('package.json', JSON.stringify(packageJson, null, ' '));
return cb();
}
catch (err) {
cb(err);
}
}
/* ------------------------------------------ */
/* Scripts */
/* ------------------------------------------ */
module.exports.bump = gulp.series(bumpVersion, changelog);
module.exports.release = gulp.series(commitTagPush);