-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrelease.ts
More file actions
88 lines (72 loc) · 2.51 KB
/
release.ts
File metadata and controls
88 lines (72 loc) · 2.51 KB
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
import path from 'node:path';
import { run } from './run';
import { updateVersion } from './update-version';
import chalk from 'chalk';
import fs from 'fs-extra';
import signale from 'signale';
import SimpleGit from 'simple-git';
import { getNextVersion, VersionIncrement, VersionStage } from 'version-next';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { $ } from 'zx';
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJson = fs.readJsonSync(packageJsonPath);
const { argv } = yargs(hideBin(process.argv)) as any;
const git = SimpleGit();
const versionIncrement: VersionIncrement = argv._[0] || 'patch';
const versionStage: VersionStage | undefined = argv.stage;
async function release() {
await run(git.pull(), {
info: 'Pulling the latest changes from the remote repository',
success: 'The latest changes have been pulled from the remote repository',
error: 'Failed to pull the latest changes from the remote repository',
});
const gitStatus = await git.status();
if (gitStatus.files.length > 0) {
signale.error(
'Working directory is not clean, commit all changes before publishing the package.'
);
process.exit(1);
}
const nextVersion = getNextVersion(packageJson.version, {
type: versionIncrement,
stage: versionStage,
});
signale.info(
`Publishing next ${chalk.cyan(versionIncrement)} version of ${chalk.cyan(
packageJson.name
)} to npm.`
);
signale.info(
`Current version: ${chalk.cyan(packageJson.version)}, next version: ${chalk.cyan(nextVersion)}`
);
await run($`yarn`, {
info: 'Installing fresh dependencies',
success: 'Fresh dependencies have been installed',
error: 'Failed to install fresh dependencies',
});
await run($`yarn run clean`, {
info: 'Removing dist directory',
success: 'dist directory has been removed',
error: 'Failed to remove dist directory',
});
await run($`yarn run build`, {
info: 'Building the package',
success: 'The package has been built',
error: 'Failed to build the package',
});
const revertVersion = await updateVersion(nextVersion);
await run(
$`npm publish --access public --tag ${versionStage || 'latest'}`,
{
info: 'Publishing the package to npm',
success: 'The package has been published to npm',
error: 'Failed to publish the package to npm',
},
revertVersion
);
await git.add([packageJsonPath]);
await git.commit(`Release ${nextVersion}`);
await git.push();
}
release();