-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease.js
125 lines (102 loc) · 4.06 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
'use strict';
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const { cwd } = require('process');
const enquirer = require('enquirer');
const { prompt } = enquirer;
async function main() {
const chalk = (await import('chalk')).default; // Importação dinâmica para ESM
const { execa } = await import('execa'); // Importação dinâmica para ESM
const currentVersion = JSON.parse(fs.readFileSync(path.resolve(cwd(), 'package.json'), 'utf-8')).version;
const versionIncrements = ['patch', 'minor', 'major'];
const inc = (i) => semver.inc(currentVersion, i);
const run = async (bin, args, opts = {}) => {
try {
await execa(bin, args, { stdio: 'inherit', ...opts });
} catch (err) {
console.error(chalk.red(`Error running command: ${bin} ${args.join(' ')}`));
console.error(err.message);
process.exit(1);
}
};
const step = (msg) => console.log(chalk.cyan(msg));
let targetVersion;
try {
// Prompt release type
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type:',
choices: versionIncrements.map((i) => `${i} (${inc(i)})`).concat(['custom']),
});
if (release === 'custom') {
const { version } = await prompt({
type: 'input',
name: 'version',
message: 'Input custom version:',
initial: currentVersion,
});
targetVersion = version;
} else {
targetVersion = release.match(/\((.*)\)/)[1];
}
if (!semver.valid(targetVersion)) {
throw new Error(`Invalid target version: ${targetVersion}`);
}
// Confirm release
const { yes: tagOk } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing v${targetVersion}. Confirm?`,
});
if (!tagOk) {
console.log(chalk.yellow('Release canceled.'));
return;
}
// Update the package version
step('\nUpdating the package version...');
updatePackage(targetVersion);
// Generate the changelog
step('\nGenerating the changelog...');
await run('pnpm', ['run', 'changelog']);
const { yes: changelogOk } = await prompt({
type: 'confirm',
name: 'yes',
message: `Changelog generated. Does it look good?`,
});
if (!changelogOk) {
console.log(chalk.yellow('Release canceled after changelog review.'));
return;
}
// Commit changes and create a Git tag
step('\nCommitting changes...');
await run('git', ['add', 'CHANGELOG.md', 'package.json']);
await run('git', ['commit', '-m', `release: v${targetVersion}`]);
await run('git', ['tag', `v${targetVersion}`]);
// Publish the package
step('\nPublishing the package...');
await run('pnpm', ['publish', '--access', 'public']);
// Push changes to GitHub
step('\nPushing to GitHub...');
await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`]);
await run('git', ['push']);
console.log(chalk.green(`\nSuccessfully released v${targetVersion}!`));
} catch (err) {
console.error(chalk.red(`\nAn error occurred during the release process:`));
console.error(err.message);
process.exit(1);
}
}
function updatePackage(version) {
const pkgPath = path.resolve(cwd(), 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
pkg.version = version;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 4) + '\n');
console.log(`Updated package.json version to ${version}`);
}
main().catch((err) => {
console.error(`\nUnexpected error:`);
console.error(err.message);
process.exit(1);
});