1
- import * as child_process from ' child_process' ;
2
- import * as fs from 'fs' ;
3
- import * as path from ' path' ;
1
+ import * as child_process from " child_process"
2
+ import * as fs from "fs"
3
+ import * as path from " path"
4
4
import * as mkc from "./mkc"
5
5
import * as files from "./files"
6
- import { httpGetJsonAsync } from ' ./downloader' ;
7
- import { glob } from ' glob' ;
6
+ import { httpGetJsonAsync } from " ./downloader"
7
+ import { glob } from " glob"
8
8
9
9
export interface SpawnOptions {
10
- cmd : string ;
11
- args : string [ ] ;
12
- cwd ?: string ;
13
- shell ?: boolean ;
14
- pipe ?: boolean ;
15
- input ?: string ;
16
- silent ?: boolean ;
17
- allowNonZeroExit ?: boolean ;
10
+ cmd : string
11
+ args : string [ ]
12
+ cwd ?: string
13
+ shell ?: boolean
14
+ pipe ?: boolean
15
+ input ?: string
16
+ silent ?: boolean
17
+ allowNonZeroExit ?: boolean
18
18
}
19
19
20
20
export function spawnAsync ( opts : SpawnOptions ) {
21
21
opts . pipe = false
22
- return spawnWithPipeAsync ( opts )
23
- . then ( ( ) => { } )
22
+ return spawnWithPipeAsync ( opts ) . then ( ( ) => { } )
24
23
}
25
24
26
25
export function spawnWithPipeAsync ( opts : SpawnOptions ) {
@@ -32,81 +31,95 @@ export function spawnWithPipeAsync(opts: SpawnOptions) {
32
31
let ch = child_process . spawn ( opts . cmd , opts . args , {
33
32
cwd : opts . cwd ,
34
33
env : process . env ,
35
- stdio : opts . pipe ? [ opts . input == null ? process . stdin : "pipe" , "pipe" , process . stderr ] : "inherit" ,
36
- shell : opts . shell || false
34
+ stdio : opts . pipe
35
+ ? [
36
+ opts . input == null ? process . stdin : "pipe" ,
37
+ "pipe" ,
38
+ process . stderr ,
39
+ ]
40
+ : "inherit" ,
41
+ shell : opts . shell || false ,
37
42
} as any )
38
43
let bufs : Buffer [ ] = [ ]
39
44
if ( opts . pipe )
40
- ch . stdout . on ( ' data' , ( buf : Buffer ) => {
45
+ ch . stdout . on ( " data" , ( buf : Buffer ) => {
41
46
bufs . push ( buf )
42
47
if ( ! opts . silent ) {
43
48
process . stdout . write ( buf )
44
49
}
45
50
} )
46
- ch . on ( ' close' , ( code : number ) => {
51
+ ch . on ( " close" , ( code : number ) => {
47
52
if ( code != 0 && ! opts . allowNonZeroExit )
48
53
reject ( new Error ( "Exit code: " + code + " from " + info ) )
49
54
resolve ( Buffer . concat ( bufs ) )
50
- } ) ;
51
- if ( opts . input != null )
52
- ch . stdin . end ( opts . input , "utf8" )
55
+ } )
56
+ if ( opts . input != null ) ch . stdin . end ( opts . input , "utf8" )
53
57
} )
54
58
}
55
59
56
-
57
60
let readlineCount = 0
58
61
function readlineAsync ( ) {
59
- process . stdin . resume ( ) ;
60
- process . stdin . setEncoding ( ' utf8' ) ;
62
+ process . stdin . resume ( )
63
+ process . stdin . setEncoding ( " utf8" )
61
64
readlineCount ++
62
65
return new Promise < string > ( ( resolve , reject ) => {
63
- process . stdin . once ( ' data' , ( text : string ) => {
66
+ process . stdin . once ( " data" , ( text : string ) => {
64
67
resolve ( text )
65
68
} )
66
69
} )
67
70
}
68
71
69
72
export function queryAsync ( msg : string , defl : string ) {
70
73
process . stdout . write ( `${ msg } [${ defl } ]: ` )
71
- return readlineAsync ( )
72
- . then ( text => {
73
- text = text . trim ( )
74
- if ( ! text ) return defl
75
- else return text
76
- } )
74
+ return readlineAsync ( ) . then ( text => {
75
+ text = text . trim ( )
76
+ if ( ! text ) return defl
77
+ else return text
78
+ } )
77
79
}
78
80
79
81
export function needsGitCleanAsync ( ) {
80
82
return Promise . resolve ( )
81
- . then ( ( ) => spawnWithPipeAsync ( {
82
- cmd : "git" ,
83
- args : [ "status" , "--porcelain" , "--untracked-files=no" ]
84
- } ) )
83
+ . then ( ( ) =>
84
+ spawnWithPipeAsync ( {
85
+ cmd : "git" ,
86
+ args : [ "status" , "--porcelain" , "--untracked-files=no" ] ,
87
+ } )
88
+ )
85
89
. then ( buf => {
86
90
if ( buf . length )
87
- throw new Error ( "Please commit all files to git before running 'makecode --bump'" )
91
+ throw new Error (
92
+ "Please commit all files to git before running 'makecode --bump'"
93
+ )
88
94
} )
89
95
}
90
96
91
97
export function runGitAsync ( ...args : string [ ] ) {
92
98
return spawnAsync ( {
93
99
cmd : "git" ,
94
100
args : args ,
95
- cwd : "."
101
+ cwd : "." ,
96
102
} )
97
103
}
98
104
99
105
export function monoRepoConfigs ( folder : string , includingSelf = true ) {
100
- return glob . sync ( folder + "/**/pxt.json" )
101
- . filter ( e =>
102
- e . indexOf ( "pxt_modules" ) < 0 &&
103
- e . indexOf ( "node_modules" ) < 0 &&
104
- ( includingSelf || path . resolve ( folder , "pxt.json" ) != path . resolve ( e ) ) )
106
+ return glob
107
+ . sync ( folder + "/**/pxt.json" )
108
+ . filter (
109
+ e =>
110
+ e . indexOf ( "pxt_modules" ) < 0 &&
111
+ e . indexOf ( "node_modules" ) < 0 &&
112
+ ( includingSelf ||
113
+ path . resolve ( folder , "pxt.json" ) != path . resolve ( e ) )
114
+ )
105
115
}
106
116
107
- export async function bumpAsync ( prj : mkc . Project , versionFile : string , stage : boolean ) {
108
- if ( stage )
109
- mkc . log ( `operation staged, skipping git commit/push` )
117
+ export async function bumpAsync (
118
+ prj : mkc . Project ,
119
+ versionFile : string ,
120
+ stage : boolean
121
+ ) {
122
+ if ( stage ) mkc . log ( `operation staged, skipping git commit/push` )
110
123
111
124
if ( ! stage ) {
112
125
await needsGitCleanAsync ( )
@@ -121,10 +134,13 @@ export async function bumpAsync(prj: mkc.Project, versionFile: string, stage: bo
121
134
122
135
if ( versionFile ) {
123
136
mkc . log ( `writing version ${ newV } in ${ versionFile } ` )
124
- const versionSrc =
125
- `
137
+ const versionSrc = `
126
138
// Auto-generated file: do not edit.
127
- namespace ${ cfg . name . replace ( / ^ p x t - / , '' ) . split ( / - / g) . map ( ( p , i ) => i == 0 ? p : ( p [ 0 ] . toUpperCase ( ) + p . slice ( 1 ) ) ) . join ( "" ) } {
139
+ namespace ${ cfg . name
140
+ . replace ( / ^ p x t - / , "" )
141
+ . split ( / - / g)
142
+ . map ( ( p , i ) => ( i == 0 ? p : p [ 0 ] . toUpperCase ( ) + p . slice ( 1 ) ) )
143
+ . join ( "" ) } {
128
144
/**
129
145
* Version of the library
130
146
*/
@@ -135,7 +151,12 @@ namespace ${cfg.name.replace(/^pxt-/, '').split(/-/g).map((p, i) => i == 0 ? p :
135
151
136
152
const configs = monoRepoConfigs ( prj . directory , false )
137
153
if ( configs . length > 0 ) {
138
- if ( await queryAsync ( `Also update sub-packages (${ configs . length } ) in this repo?` , "y" ) == "y" ) {
154
+ if (
155
+ ( await queryAsync (
156
+ `Also update sub-packages (${ configs . length } ) in this repo?` ,
157
+ "y"
158
+ ) ) == "y"
159
+ ) {
139
160
for ( const fn of configs ) {
140
161
const cfg0 = JSON . parse ( fs . readFileSync ( fn , "utf8" ) )
141
162
cfg0 . version = newV
@@ -144,7 +165,11 @@ namespace ${cfg.name.replace(/^pxt-/, '').split(/-/g).map((p, i) => i == 0 ? p :
144
165
}
145
166
}
146
167
147
- await files . writeFilesAsync ( prj . directory , { "pxt.json" : mkc . stringifyConfig ( cfg ) } , true )
168
+ await files . writeFilesAsync (
169
+ prj . directory ,
170
+ { "pxt.json" : mkc . stringifyConfig ( cfg ) } ,
171
+ true
172
+ )
148
173
149
174
if ( ! stage ) {
150
175
await runGitAsync ( "commit" , "-a" , "-m" , newV )
@@ -155,17 +180,22 @@ namespace ${cfg.name.replace(/^pxt-/, '').split(/-/g).map((p, i) => i == 0 ? p :
155
180
const urlinfo = await spawnWithPipeAsync ( {
156
181
cmd : "git" ,
157
182
args : [ "remote" , "get-url" , "origin" ] ,
158
- pipe : true
159
- } ) . then ( v => v , err => {
160
- mkc . error ( err )
161
- return null as Buffer
162
- } )
183
+ pipe : true ,
184
+ } ) . then (
185
+ v => v ,
186
+ err => {
187
+ mkc . error ( err )
188
+ return null as Buffer
189
+ }
190
+ )
163
191
const url = urlinfo ?. toString ( "utf8" ) ?. trim ( )
164
192
if ( url ) {
165
193
const slug = url . replace ( / .* g i t h u b \. c o m \/ / i, "" )
166
194
if ( slug != url ) {
167
195
mkc . log ( `Github slug ${ slug } ; refreshing makecode.com cache` )
168
- const res = await httpGetJsonAsync ( "https://makecode.com/api/gh/" + slug + "/refs?nocache=1" )
196
+ const res = await httpGetJsonAsync (
197
+ "https://makecode.com/api/gh/" + slug + "/refs?nocache=1"
198
+ )
169
199
const sha = res ?. refs ?. [ "refs/tags/" + newTag ]
170
200
mkc . log ( `refreshed ${ newV } -> ${ sha } ` )
171
201
}
0 commit comments