1
- import util , { NPMRelease } from '../lib/util' ;
1
+ import util from '../lib/util' ;
2
2
import { execSync } from 'child_process' ;
3
3
import os from 'os' ;
4
4
import chalk from 'chalk' ;
@@ -84,7 +84,7 @@ async function decideActionVersion(version: string, options: { update: boolean;
84
84
let downgrade = false ;
85
85
86
86
if ( isUpdate ) {
87
- const current = util . getCurrentNodeCGVersion ( ) ;
87
+ current = util . getCurrentNodeCGVersion ( ) ;
88
88
89
89
if ( semver . eq ( target , current ) ) {
90
90
console . log (
@@ -120,7 +120,12 @@ async function decideActionVersion(version: string, options: { update: boolean;
120
120
}
121
121
122
122
if ( semver . lt ( target , 'v2.0.0' ) ) {
123
- actionV1 ( current , target , isUpdate ) ;
123
+ if ( current && semver . gte ( current , 'v2.0.0' ) ) {
124
+ console . error ( `You are attempting to downgrade NodeCG from v2.x to v1.x, which is not supported.` ) ;
125
+ return ;
126
+ }
127
+
128
+ await actionV1 ( current , target , isUpdate ) ;
124
129
} else if ( semver . lt ( target , 'v3.0.0' ) ) {
125
130
await actionV2 ( current , target , isUpdate ) ;
126
131
} else {
@@ -145,19 +150,16 @@ async function decideActionVersion(version: string, options: { update: boolean;
145
150
}
146
151
}
147
152
148
- function actionV1 ( current : string | undefined , target : string , isUpdate : boolean ) {
149
- if ( isUpdate ) {
153
+ async function actionV1 ( current : string | undefined , target : string , isUpdate : boolean ) {
154
+ const isGitRepo = fs . existsSync ( '.git' ) ;
155
+ if ( isGitRepo && isUpdate ) {
150
156
process . stdout . write ( 'Downloading latest release...' ) ;
151
157
try {
152
158
execSync ( 'git fetch' , { stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
153
159
process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
154
160
} catch ( e ) {
155
- /* istanbul ignore next */
156
161
process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
157
- /* istanbul ignore next */
158
- console . error ( e . stack ) ;
159
- /* istanbul ignore next */
160
- return ;
162
+ throw e ;
161
163
}
162
164
163
165
if ( current ) {
@@ -171,12 +173,8 @@ function actionV1(current: string | undefined, target: string, isUpdate: boolean
171
173
execSync ( `git clone ${ NODECG_GIT_URL } .` , { stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
172
174
process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
173
175
} catch ( e ) {
174
- /* istanbul ignore next */
175
176
process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
176
- /* istanbul ignore next */
177
- console . error ( e . stack ) ;
178
- /* istanbul ignore next */
179
- return ;
177
+ throw e ;
180
178
}
181
179
182
180
// Check out the target version.
@@ -185,40 +183,22 @@ function actionV1(current: string | undefined, target: string, isUpdate: boolean
185
183
execSync ( `git checkout ${ target } ` , { stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
186
184
process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
187
185
} catch ( e ) {
188
- /* istanbul ignore next */
189
186
process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
190
- /* istanbul ignore next */
191
- console . error ( e . stack ) ;
187
+ throw e ;
192
188
}
193
189
}
194
190
}
195
191
196
- async function actionV2 ( current : string | undefined , target : string , _isUpdate : boolean ) {
197
- let release : NPMRelease ;
192
+ async function actionV2 ( current : string | undefined , target : string , isUpdate : boolean ) {
193
+ if ( isUpdate ) {
194
+ const deletingDirectories = [ '.git' , 'build' , 'scripts' , 'schemas' ] ;
195
+ await Promise . all ( deletingDirectories . map ( ( dir ) => fs . promises . rm ( dir , { recursive : true , force : true } ) ) ) ;
196
+ }
198
197
199
- process . stdout . write ( 'Downloading latest release...' ) ;
200
- try {
201
- release = await util . getLatestNodeCGRelease ( ) ;
202
- // target is v1.2.3, release.version is 1.2.3
203
- if ( ! semver . satisfies ( release . version , target ) ) {
204
- process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
205
- console . error (
206
- `Expected latest npm release to be ${ chalk . magenta ( target ) } but instead it was ${ chalk . magenta (
207
- release . version ,
208
- ) } . Aborting.`,
209
- ) ;
210
- return ;
211
- }
198
+ process . stdout . write ( `Downloading ${ target } from npm...` ) ;
199
+ const release = await util . getNodeCGRelease ( target ) ;
212
200
213
- process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
214
- } catch ( e ) {
215
- /* istanbul ignore next */
216
- process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
217
- /* istanbul ignore next */
218
- console . error ( e . stack ) ;
219
- /* istanbul ignore next */
220
- return ;
221
- }
201
+ process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
222
202
223
203
if ( current ) {
224
204
logDownOrUpgradeMessage ( current , target , semver . lt ( target , current ) ) ;
@@ -265,20 +245,13 @@ function gitCheckoutUpdate(target: string) {
265
245
}
266
246
267
247
async function downloadAndExtractReleaseTarball ( tarballUrl : string ) {
268
- try {
269
- const res = await fetch ( tarballUrl ) ;
270
- if ( ! res . body ) {
271
- throw new Error ( `Failed to fetch release tarball from ${ tarballUrl } , status code ${ res . status } ` ) ;
272
- }
273
-
274
- await stream . pipeline ( res . body , tar . x ( { strip : 1 } ) ) ;
275
- process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
276
- } catch ( e ) {
277
- /* istanbul ignore next */
278
- process . stdout . write ( chalk . red ( 'failed!' ) + os . EOL ) ;
279
- /* istanbul ignore next */
280
- console . error ( e . stack ) ;
248
+ const res = await fetch ( tarballUrl ) ;
249
+ if ( ! res . body ) {
250
+ throw new Error ( `Failed to fetch release tarball from ${ tarballUrl } , status code ${ res . status } ` ) ;
281
251
}
252
+
253
+ await stream . pipeline ( res . body , tar . x ( { strip : 1 } ) ) ;
254
+ process . stdout . write ( chalk . green ( 'done!' ) + os . EOL ) ;
282
255
}
283
256
284
257
function logDownOrUpgradeMessage ( current : string , target : string , downgrade : boolean ) : void {
0 commit comments