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
+ )
54
39
)
55
- ) {
56
- console . error ( 'Ignoring already published error' , platform )
57
- } else {
58
- // throw err
40
+ pkg . version = version
41
+ await writeFile (
42
+ path . join ( nativePackagesDir , platform , 'package.json' ) ,
43
+ JSON . stringify ( pkg , null , 2 )
44
+ )
45
+ await execa (
46
+ `npm` ,
47
+ [
48
+ `publish` ,
49
+ `${ path . join ( nativePackagesDir , platform ) } ` ,
50
+ `--access` ,
51
+ `public` ,
52
+ ...( version . includes ( 'canary' ) ? [ '--tag' , 'canary' ] : [ ] ) ,
53
+ ] ,
54
+ { stdio : 'inherit' }
55
+ )
56
+ } catch ( err ) {
57
+ // don't block publishing other versions on single platform error
58
+ console . error ( `Failed to publish` , platform , err )
59
+
60
+ if (
61
+ err . message &&
62
+ err . message . includes (
63
+ 'You cannot publish over the previously published versions'
64
+ )
65
+ ) {
66
+ console . error ( 'Ignoring already published error' , platform , err )
67
+ } else {
68
+ // throw err
69
+ }
70
+ } finally {
71
+ publishSema . release ( )
59
72
}
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
- }
73
+ // lerna publish in next step sill fail if git status is not clean
74
+ await execa (
75
+ `git` ,
76
+ [
77
+ 'update-index' ,
78
+ '--skip-worktree' ,
79
+ `${ path . join ( nativePackagesDir , platform , 'package.json' ) } ` ,
80
+ ] ,
81
+ { stdio : 'inherit' }
82
+ )
83
+ } )
84
+ )
70
85
71
86
// Update name/version of wasm packages and publish
72
87
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
88
80
- await writeFile (
81
- path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) ,
82
- JSON . stringify ( wasmPkg , null , 2 )
83
- )
89
+ await Promise . all (
90
+ [ 'web' , 'nodejs' ] . map ( async ( wasmTarget ) => {
91
+ await publishSema . acquire ( )
84
92
85
- try {
86
- execSync (
87
- `npm publish ${ path . join (
88
- wasmDir ,
89
- `pkg-${ wasmTarget } `
90
- ) } --access public ${
91
- gitref . includes ( 'canary' ) ? ' --tag canary' : ''
92
- } `
93
+ let wasmPkg = JSON . parse (
94
+ await readFile ( path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) )
95
+ )
96
+ wasmPkg . name = `@next/swc-wasm-${ wasmTarget } `
97
+ wasmPkg . version = version
98
+
99
+ await writeFile (
100
+ path . join ( wasmDir , `pkg-${ wasmTarget } /package.json` ) ,
101
+ JSON . stringify ( wasmPkg , null , 2 )
93
102
)
94
- } catch ( err ) {
95
- // don't block publishing other versions on single platform error
96
- console . error ( `Failed to publish` , wasmTarget )
97
103
98
- if (
99
- err . message &&
100
- err . message . includes (
101
- 'You cannot publish over the previously published versions'
104
+ try {
105
+ await execa (
106
+ `npm` ,
107
+ [
108
+ 'publish' ,
109
+ `${ path . join ( wasmDir , `pkg-${ wasmTarget } ` ) } ` ,
110
+ '--access' ,
111
+ 'public' ,
112
+ ...( version . includes ( 'canary' ) ? [ '--tag' , 'canary' ] : [ ] ) ,
113
+ ] ,
114
+ { stdio : 'inherit' }
102
115
)
103
- ) {
104
- console . error ( 'Ignoring already published error' , wasmTarget )
105
- } else {
106
- // throw err
116
+ } catch ( err ) {
117
+ // don't block publishing other versions on single platform error
118
+ console . error ( `Failed to publish` , wasmTarget , err )
119
+
120
+ if (
121
+ err . message &&
122
+ err . message . includes (
123
+ 'You cannot publish over the previously published versions'
124
+ )
125
+ ) {
126
+ console . error ( 'Ignoring already published error' , wasmTarget )
127
+ } else {
128
+ // throw err
129
+ }
130
+ } finally {
131
+ publishSema . release ( )
107
132
}
108
- }
109
- }
133
+ } )
134
+ )
110
135
111
136
// Update optional dependencies versions
112
137
let nextPkg = JSON . parse (
@@ -122,7 +147,13 @@ const cwd = process.cwd()
122
147
JSON . stringify ( nextPkg , null , 2 )
123
148
)
124
149
// lerna publish in next step will fail if git status is not clean
125
- execSync ( 'git update-index --skip-worktree packages/next/package.json' )
150
+ await execa (
151
+ 'git' ,
152
+ [ 'update-index' , '--skip-worktree' , 'packages/next/package.json' ] ,
153
+ {
154
+ stdio : 'inherit' ,
155
+ }
156
+ )
126
157
} catch ( err ) {
127
158
console . error ( err )
128
159
process . exit ( 1 )
0 commit comments