11import path from 'path' ;
22import * as url from 'url' ;
3+ import cac from 'cac' ;
34import { $ } from 'execa' ;
45import fs from 'fs-extra' ;
5- import { inc } from 'semver' ;
66
7- const RELEASE_TAG = process . env . TAG || 'beta' ;
8- const RELEASE_DRY_RUN = process . env . DRY_RUN || 'true' ;
9- const RELEASE_VERSION_TYPE = process . env . VERSION || 'prerelease' ;
10- const RELEASE_NPM_TOKEN = process . env . NPM_TOKEN || '' ;
7+ let cli = cac ( 'release' ) ;
8+ cli . option (
9+ '--dry-run <run>' ,
10+ 'Perform a dry run without publishing or pushing tags' ,
11+ {
12+ default : 'false' ,
13+ } ,
14+ ) ;
15+ cli . option ( '--tag <tag>' , 'The npm tag to publish under (default: canary)' , {
16+ default : 'canary' ,
17+ } ) ;
1118
1219const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
1320const PKG_PATH = path . resolve ( __dirname , '../package.json' ) ;
1421const pkg = fs . readJsonSync ( PKG_PATH ) ;
15- const currentVersion = pkg . version ;
16- const nextVersion = inc ( currentVersion , RELEASE_VERSION_TYPE ) ;
17- if ( ! nextVersion ) {
22+ const publishVersion = pkg . version ;
23+
24+ const parsed = cli . parse ( ) ;
25+ const npmTag = parsed . options . tag ;
26+ const isDryRun = parsed . options . dryRun === 'true' ;
27+
28+ const allowedTags = [ 'latest' , 'canary' , 'alpha' , 'beta' , 'rc' , 'nightly' ] ;
29+ if ( ! allowedTags . includes ( npmTag ) ) {
1830 throw new Error (
19- `Failed to generate next version from " ${ currentVersion } " with type " ${ RELEASE_VERSION_TYPE } " ` ,
31+ `Invalid npm tag: ${ npmTag } . Allowed tags: ${ allowedTags . join ( ', ' ) } ` ,
2032 ) ;
2133}
2234
23- console . info ( `Release ${ RELEASE_TAG } version ${ nextVersion } ` ) ;
24-
25- // Update pkg version
26- console . info ( `Updating version from ${ currentVersion } to ${ nextVersion } ` ) ;
27- pkg . version = nextVersion ;
28- fs . writeJsonSync ( PKG_PATH , pkg , { spaces : 2 } ) ;
35+ const prereleaseTags = [ 'alpha' , 'beta' , 'rc' , 'canary' , 'nightly' ] ;
36+ if (
37+ npmTag === 'latest' &&
38+ prereleaseTags . some ( ( tag ) => publishVersion . includes ( tag ) )
39+ ) {
40+ throw Error ( `Can't release ${ publishVersion } to latest tag` ) ;
41+ }
2942
30- // Write npmrc
31- const npmrcPath = `${ process . env . HOME } /.npmrc` ;
32- console . info ( `Writing npmrc to ${ npmrcPath } ` ) ;
33- fs . writeFileSync (
34- npmrcPath ,
35- `//registry.npmjs.org/:_authToken=${ RELEASE_NPM_TOKEN } ` ,
43+ console . info (
44+ `Release ${ npmTag } version ${ publishVersion } ${ isDryRun ? '(dry-run)' : '' } ` ,
3645) ;
3746
38- // Publish to npm
39- console . info ( `Publishing to npm with tag ${ RELEASE_TAG } ` ) ;
40- const dryRun = RELEASE_DRY_RUN === 'true' ? [ '--dry-run' ] : [ ] ;
4147try {
42- await $ `pnpm publish ${ dryRun } --tag ${ RELEASE_TAG } --no-git-checks --provenance` ;
48+ const flags = isDryRun
49+ ? [ '--dry-run' , `--tag` , npmTag , `--no-git-checks` ]
50+ : [ `--tag` , npmTag , `--no-git-checks` ] ;
51+ await $ `pnpm publish ${ flags } ` ;
4352 console . info ( `Published successfully` ) ;
4453} catch ( e ) {
4554 console . error ( `Publish failed: ${ e . message } ` ) ;
4655 process . exit ( 1 ) ;
47- } finally {
48- fs . removeSync ( npmrcPath ) ;
4956}
5057
51- // Push tag to github
52- if ( RELEASE_DRY_RUN !== 'true' ) {
58+ // Push tag to GitHub
59+ if ( ! isDryRun ) {
5360 console . info ( `Pushing tag to github` ) ;
54- const tagName = `v${ nextVersion } ` ;
61+ const tagName = `v${ publishVersion } ` ;
5562 try {
5663 await $ `git config --global --add safe.directory /github/workspace` ;
5764 await $ `git config --global user.name "github-actions[bot]"` ;
@@ -64,17 +71,6 @@ if (RELEASE_DRY_RUN !== 'true') {
6471 console . error ( `Push tag failed: ${ e . message } ` ) ;
6572 process . exit ( 1 ) ;
6673 }
67-
68- try {
69- await $ `git add --all` ;
70- const commitMsg = `release ${ tagName } ` ;
71- await $ `git commit -m ${ commitMsg } ` ;
72- await $ `git push` ;
73- console . info ( `Pushed branch successfully` ) ;
74- } catch ( e ) {
75- console . error ( `Update branch failed: ${ e . message } ` ) ;
76- process . exit ( 1 ) ;
77- }
7874}
7975
8076console . info ( `Release completed` ) ;
0 commit comments