-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.ts
121 lines (102 loc) · 3.42 KB
/
release.ts
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
import path from 'node:path';
import chalk from 'chalk';
import fs from 'fs-extra';
import yargs from 'yargs';
import open from 'open';
import githubRelease from 'new-github-release-url';
import SimpleGit from 'simple-git';
import { hideBin } from 'yargs/helpers';
import { execa } from 'execa';
import signale from 'signale';
import { getNextVersion, VersionStage, VersionIncrement } from 'version-next';
import { run } from './run';
import { updateVersion } from './update-version';
function getRepositoryInfo(gitUrl: string) {
const [user, repo] = gitUrl.replace('git+https://github.com/', '').replace('.git', '').split('/');
return { user, repo };
}
const packageJsonPath = path.join(process.cwd(), 'package/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;
const isLocalRelease: boolean = !!argv.local;
async function release() {
if (!isLocalRelease) {
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(execa('yarn'), {
info: 'Installing fresh dependencies',
success: 'Fresh dependencies have been installed',
error: 'Failed to install fresh dependencies',
});
await run(execa('yarn', ['run', 'clean']), {
info: 'Removing dist directory',
success: 'dist directory has been removed',
error: 'Failed to remove dist directory',
});
await run(execa('yarn', ['run', 'build']), {
info: 'Building the package',
success: 'The package has been built',
error: 'Failed to build the package',
});
await fs.copyFile(
path.join(process.cwd(), 'README.md'),
path.join(process.cwd(), 'package/README.md')
);
await fs.copyFile(
path.join(process.cwd(), 'LICENSE'),
path.join(process.cwd(), 'package/LICENSE')
);
const revertVersion = await updateVersion(nextVersion);
await run(
execa(
'yarn',
['npm', 'publish', '--access', 'public', '--tag', versionStage ? 'next' : 'latest'],
{ cwd: path.join(process.cwd(), 'package') }
),
{
info: 'Publishing the package to npm',
success: 'The package has been published to npm',
error: 'Failed to publish the package to npm',
},
revertVersion
);
if (!isLocalRelease) {
await git.add([packageJsonPath]);
await git.commit(`Release ${nextVersion}`);
await git.push();
open(
githubRelease({
...getRepositoryInfo(packageJson.repository.url),
tag: nextVersion,
title: nextVersion,
})
);
}
}
release();