1
- #!/usr/bin/env node
2
- // Script to publish CLI to NPM.
3
1
// @ts -check
4
2
5
3
const { npmPublish } = require ( '@jsdevtools/npm-publish' )
6
- const { readFileSync, writeFileSync } = require ( 'fs' )
7
4
const { spawnSync } = require ( 'child_process' )
5
+ const { readFileSync, writeFileSync } = require ( 'fs' )
8
6
const manifestPath = './package.json'
9
7
10
8
publishToNpm ( ) . then ( handleSuccess ) . catch ( handleError )
@@ -16,15 +14,18 @@ async function publishToNpm() {
16
14
const { name, version : currentVersion } = manifest
17
15
const publishedVersion = getPublishedVersion ( name )
18
16
17
+ console . log ( 'Current published version:' , currentVersion )
18
+
19
19
if ( publishedVersion === currentVersion ) {
20
- return publishCanary ( manifest )
20
+ return await publishCanary ( manifest )
21
21
}
22
22
23
- return publishLatest ( )
23
+ return await publishLatest ( currentVersion )
24
24
}
25
25
26
- async function publishLatest ( ) {
27
- const result = await npmPublish ( { checkVersion : true } )
26
+ async function publishLatest ( version ) {
27
+ const result = await npmPublish ( { checkVersion : true , access : 'public' } )
28
+ gitTag ( version )
28
29
29
30
return result
30
31
}
@@ -35,14 +36,14 @@ async function publishCanary(manifest, tag = 'canary') {
35
36
writeFileSync ( manifestPath , JSON . stringify ( canaryManifest , null , 2 ) )
36
37
37
38
console . log ( 'Publishing' , tag , 'version:' , canaryVersion )
38
- return npmPublish ( { tag } )
39
+ return npmPublish ( { tag, access : 'public' } )
39
40
}
40
41
41
42
/** @param {import("@jsdevtools/npm-publish").Results } results */
42
- function handleSuccess ( { version , package : packageName } ) {
43
- console . log (
44
- `Link : https://www.npmjs.com/package/ ${ packageName } /v/ ${ version } `
45
- )
43
+ function handleSuccess ( { package : name , tag , version } ) {
44
+ console . log ( )
45
+ console . log ( `Usage: npm install ${ name } @ ${ tag } ` )
46
+ console . log ( `Link : https://www.npmjs.com/package/ ${ name } /v/ ${ version } ` )
46
47
}
47
48
48
49
function handleError ( error ) {
@@ -61,3 +62,11 @@ function getPublishedVersion(name) {
61
62
} )
62
63
return result . stdout . trim ( ) . split ( '\\' ) [ 0 ]
63
64
}
65
+
66
+ /** @param {string } version */
67
+ function gitTag ( version ) {
68
+ spawnSync ( 'git' , [ 'config' , '--local' , 'user.email' , `"[email protected] "` ] )
69
+ spawnSync ( 'git' , [ 'config' , '--local' , 'user.name' , `"GitHub Action"` ] )
70
+
71
+ spawnSync ( 'git' , [ 'tag' , '-a' , `v${ version } ` , '-m' , `Release v${ version } ` ] )
72
+ }
0 commit comments