1
1
#!/usr/bin/env node
2
2
3
3
const path = require ( 'path' )
4
- const { readFile , readdir , writeFile } = require ( 'fs/promises ' )
4
+ const execa = require ( 'execa ' )
5
5
const { copy } = require ( 'fs-extra' )
6
- const { execSync } = require ( 'child_process' )
6
+ const { Sema } = require ( 'async-sema' )
7
+ const { readFile, readdir, writeFile } = require ( 'fs/promises' )
7
8
8
9
const cwd = process . cwd ( )
9
10
10
11
; ( async function ( ) {
11
12
try {
13
+ const publishSema = new Sema ( 2 )
14
+
12
15
let version = JSON . parse (
13
16
await readFile ( path . join ( cwd , 'lerna.json' ) )
14
17
) . version
15
- let gitref = process . argv . slice ( 2 ) [ 0 ]
16
18
17
19
// Copy binaries to package folders, update version, and publish
18
20
let nativePackagesDir = path . join ( cwd , 'packages/next-swc/crates/napi/npm' )
19
21
let platforms = ( await readdir ( nativePackagesDir ) ) . filter (
20
22
( name ) => ! name . startsWith ( '.' )
21
23
)
22
24
23
- for ( let platform of platforms ) {
24
- try {
25
- let binaryName = `next-swc.${ platform } .node`
26
- await copy (
27
- path . join ( cwd , 'packages/next-swc/native' , binaryName ) ,
28
- path . join ( nativePackagesDir , platform , binaryName )
29
- )
30
- let pkg = JSON . parse (
31
- await readFile ( path . join ( nativePackagesDir , platform , 'package.json' ) )
32
- )
33
- pkg . version = version
34
- await writeFile (
35
- path . join ( nativePackagesDir , platform , 'package.json' ) ,
36
- JSON . stringify ( pkg , null , 2 )
37
- )
38
- execSync (
39
- `npm publish ${ path . join (
40
- nativePackagesDir ,
41
- platform
42
- ) } --access public ${
43
- gitref . includes ( 'canary' ) ? ' --tag canary' : ''
44
- } `
45
- )
46
- } catch ( err ) {
47
- // don't block publishing other versions on single platform error
48
- console . error ( `Failed to publish` , platform )
25
+ await Promise . all (
26
+ platforms . map ( async ( platform ) => {
27
+ await publishSema . acquire ( )
49
28
50
- if (
51
- err . message &&
52
- err . message . includes (
53
- 'You cannot publish over the previously published versions'
29
+ try {
30
+ let binaryName = `next-swc.${ platform } .node`
31
+ await copy (
32
+ path . join ( cwd , 'packages/next-swc/native' , binaryName ) ,
33
+ path . join ( nativePackagesDir , platform , binaryName )
34
+ )
35
+ let pkg = JSON . parse (
36
+ await readFile (
37
+ path . join ( nativePackagesDir , platform , 'package.json' )
38
+ )
39
+ )
40
+ pkg . version = version
41
+ await writeFile (
42
+ path . join ( nativePackagesDir , platform , 'package.json' ) ,
43
+ JSON . stringify ( pkg , null , 2 )
54
44
)
55
- ) {
56
- console . error ( 'Ignoring already published error' , platform )
57
- } else {
58
- // throw err
45
+ await execa (
46
+ `npm publish ${ path . join (
47
+ nativePackagesDir ,
48
+ platform
49
+ ) } --access public ${
50
+ version . includes ( 'canary' ) ? ' --tag canary' : ''
51
+ } `,
52
+ { stdio : 'inherit' }
53
+ )
54
+ } catch ( err ) {
55
+ // don't block publishing other versions on single platform error
56
+ console . error ( `Failed to publish` , platform )
57
+
58
+ if (
59
+ err . message &&
60
+ err . message . includes (
61
+ 'You cannot publish over the previously published versions'
62
+ )
63
+ ) {
64
+ console . error ( 'Ignoring already published error' , platform )
65
+ } else {
66
+ // throw err
67
+ }
68
+ } finally {
69
+ publishSema . release ( )
59
70
}
60
- }
61
- // lerna publish in next step sill fail if git status is not clean
62
- execSync (
63
- `git update-index --skip-worktree ${ path . join (
64
- nativePackagesDir ,
65
- platform ,
66
- 'package.json'
67
- ) } `
68
- )
69
- }
71
+ // lerna publish in next step sill fail if git status is not clean
72
+ await execa (
73
+ `git update-index --skip-worktree ${ path . join (
74
+ nativePackagesDir ,
75
+ platform ,
76
+ 'package.json'
77
+ ) } `,
78
+ { stdio : 'inherit' }
79
+ )
80
+ } )
81
+ )
70
82
71
83
// Update name/version of wasm packages and publish
72
84
let wasmDir = path . join ( cwd , 'packages/next-swc/crates/wasm' )
73
- for ( let wasmTarget of [ 'web' , 'nodejs' ] ) {
74
- let wasmPkg = JSON . parse (
75
- await readFile ( path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) )
76
- )
77
- wasmPkg . name = `@next/swc-wasm-${ wasmTarget } `
78
- wasmPkg . version = version
79
85
80
- await writeFile (
81
- path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) ,
82
- JSON . stringify ( wasmPkg , null , 2 )
83
- )
86
+ await Promise . all (
87
+ [ 'web' , 'nodejs' ] . map ( async ( wasmTarget ) => {
88
+ await publishSema . acquire ( )
84
89
85
- try {
86
- execSync (
87
- `npm publish ${ path . join (
88
- wasmDir ,
89
- `pkg-${ wasmTarget } `
90
- ) } --access public ${
91
- gitref . includes ( 'canary' ) ? ' --tag canary' : ''
92
- } `
90
+ let wasmPkg = JSON . parse (
91
+ await readFile ( path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) )
93
92
)
94
- } catch ( err ) {
95
- // don't block publishing other versions on single platform error
96
- console . error ( `Failed to publish` , wasmTarget )
93
+ wasmPkg . name = `@next/swc-wasm-${ wasmTarget } `
94
+ wasmPkg . version = version
97
95
98
- if (
99
- err . message &&
100
- err . message . includes (
101
- 'You cannot publish over the previously published versions'
96
+ await writeFile (
97
+ path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) ,
98
+ JSON . stringify ( wasmPkg , null , 2 )
99
+ )
100
+
101
+ try {
102
+ await execa (
103
+ `npm publish ${ path . join (
104
+ wasmDir ,
105
+ `pkg-${ wasmTarget } `
106
+ ) } --access public ${
107
+ version . includes ( 'canary' ) ? ' --tag canary' : ''
108
+ } `,
109
+ { stdio : 'inherit' }
102
110
)
103
- ) {
104
- console . error ( 'Ignoring already published error' , wasmTarget )
105
- } else {
106
- // throw err
111
+ } catch ( err ) {
112
+ // don't block publishing other versions on single platform error
113
+ console . error ( `Failed to publish` , wasmTarget )
114
+
115
+ if (
116
+ err . message &&
117
+ err . message . includes (
118
+ 'You cannot publish over the previously published versions'
119
+ )
120
+ ) {
121
+ console . error ( 'Ignoring already published error' , wasmTarget )
122
+ } else {
123
+ // throw err
124
+ }
125
+ } finally {
126
+ publishSema . release ( )
107
127
}
108
- }
109
- }
128
+ } )
129
+ )
110
130
111
131
// Update optional dependencies versions
112
132
let nextPkg = JSON . parse (
@@ -122,7 +142,9 @@ const cwd = process.cwd()
122
142
JSON . stringify ( nextPkg , null , 2 )
123
143
)
124
144
// lerna publish in next step will fail if git status is not clean
125
- execSync ( 'git update-index --skip-worktree packages/next/package.json' )
145
+ await execa ( 'git update-index --skip-worktree packages/next/package.json' , {
146
+ stdio : 'inherit' ,
147
+ } )
126
148
} catch ( err ) {
127
149
console . error ( err )
128
150
process . exit ( 1 )
0 commit comments