11#!/usr/bin/env node
22const fs = require ( "fs" ) ;
3+ const https = require ( "https" ) ;
34const path = require ( "path" ) ;
45const execSync = require ( "child_process" ) . execSync ;
5- const fetch = require ( "node-fetch" ) ;
6- const Spinner = require ( "cli-spinner" ) . Spinner ;
7- const inquirer = require ( 'inquirer' ) ;
8- const findProcess = require ( 'find-process' ) ;
6+
7+ const getFetch = ( ) => require ( "node-fetch" ) ;
8+ const getSpinner = ( ) => require ( "cli-spinner" ) . Spinner ;
9+ const getInquirer = ( ) => require ( "inquirer" ) ;
10+ const getFindProcess = ( ) => require ( "find-process" ) ;
911
1012const downloadPathTemplate =
1113 "https://github.com/devspace-sh/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}" ;
@@ -80,43 +82,27 @@ const sanitizeVersion = function(version) {
8082
8183const getLatestVersion = function ( callback ) {
8284 const releasesURL = "https://github.com/devspace-sh/devspace/releases/latest" ;
85+ https . get ( releasesURL , { headers : requestHeaders } , function ( res ) {
86+ const redirectUrl = res . headers . location ;
87+ const matches = redirectUrl && / \/ t a g \/ ( .* ) $ / . exec ( redirectUrl ) ;
8388
84- fetch ( releasesURL , { headers : requestHeaders , redirect : false } )
85- . then ( function ( res ) {
86- if ( ! res . ok ) {
87- console . error (
88- "Error requesting URL " +
89- releasesURL +
90- " (Status Code: " +
91- res . status +
92- ")"
93- ) ;
94- process . exit ( 1 ) ;
95- }
96-
97- const redirectUrl = res . url
98- if ( redirectUrl == null ) {
99- throw new Error ( 'Error fetching latest version' )
100- }
101-
102- const matches = / \/ t a g \/ ( .* ) $ / . exec ( redirectUrl )
103- if ( ! matches || matches . length !== 2 ) {
104- throw new Error ( 'Error fetching latest version' )
105- }
106-
107- const latestVersion = matches [ 1 ] . replace ( 'v' , '' )
108- if ( latestVersion ) {
109- callback ( latestVersion ) ;
110- } else {
111- console . error ( "Unable to identify latest devspace version" ) ;
112- process . exit ( 2 ) ;
113- }
114- } )
115- . catch ( function ( err ) {
89+ if ( ! matches || matches . length !== 2 ) {
11690 console . error ( "Error requesting URL " + releasesURL ) ;
117- console . error ( err )
11891 process . exit ( 1 ) ;
119- } )
92+ }
93+
94+ const latestVersion = matches [ 1 ] . replace ( 'v' , '' )
95+ if ( latestVersion ) {
96+ callback ( latestVersion ) ;
97+ } else {
98+ console . error ( "Unable to identify latest devspace version" ) ;
99+ process . exit ( 2 ) ;
100+ }
101+ } ) . on ( "error" , function ( err ) {
102+ console . error ( "Error requesting URL " + releasesURL ) ;
103+ console . error ( err )
104+ process . exit ( 1 ) ;
105+ } )
120106} ;
121107
122108if ( action === "update-version" ) {
@@ -311,6 +297,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
311297 removeScripts ( true ) ;
312298
313299 if ( askRemoveGlobalFolder && process . stdout . isTTY ) {
300+ const inquirer = getInquirer ( ) ;
314301 let removeGlobalFolder = function ( ) {
315302 try {
316303 let homedir = require ( 'os' ) . homedir ( ) ;
@@ -366,6 +353,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
366353 } ;
367354
368355 const downloadRelease = function ( version ) {
356+ const fetch = getFetch ( ) ;
369357 let downloadPath = downloadPathTemplate
370358 . replace ( "{{version}}" , version )
371359 . replace ( "{{platform}}" , platform )
@@ -377,6 +365,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
377365
378366 console . log ( "Download DevSpace CLI release: " + downloadPath + "\n" ) ;
379367
368+ const Spinner = getSpinner ( ) ;
380369 const spinner = new Spinner (
381370 "%s Downloading DevSpace CLI... (this may take a minute)"
382371 ) ;
@@ -466,25 +455,30 @@ let continueProcess = function (askRemoveGlobalFolder) {
466455}
467456
468457if ( process . ppid > 1 ) {
469- findProcess ( 'pid' , process . ppid )
470- . then ( function ( list ) {
471- if ( list . length === 1 && list [ 0 ] . ppid > 1 ) {
472- findProcess ( 'pid' , list [ 0 ] . ppid )
473- . then ( function ( list ) {
474- if ( list . length === 1 && / ( ( n p m - c l i .j s ( " | ' ) \s + u p ( d a t e ) ? ) | ( y a r n .j s ( " | ' ) \s + ( g l o b a l \s + ) ? u p g r a d e ) ) \s + .* ( ( \/ ) | ( \\ ) | ( \s ) ) d e v s p a c e ( ( \/ ) | ( \\ ) | ( \s ) | $ ) / . test ( list [ 0 ] . cmd ) ) {
475- continueProcess ( false ) ;
476- } else {
458+ try {
459+ const findProcess = getFindProcess ( ) ;
460+ findProcess ( 'pid' , process . ppid )
461+ . then ( function ( list ) {
462+ if ( list . length === 1 && list [ 0 ] . ppid > 1 ) {
463+ findProcess ( 'pid' , list [ 0 ] . ppid )
464+ . then ( function ( list ) {
465+ if ( list . length === 1 && / ( ( n p m - c l i .j s ( " | ' ) \s + u p ( d a t e ) ? ) | ( y a r n .j s ( " | ' ) \s + ( g l o b a l \s + ) ? u p g r a d e ) ) \s + .* ( ( \/ ) | ( \\ ) | ( \s ) ) d e v s p a c e ( ( \/ ) | ( \\ ) | ( \s ) | $ ) / . test ( list [ 0 ] . cmd ) ) {
466+ continueProcess ( false ) ;
467+ } else {
468+ continueProcess ( true ) ;
469+ }
470+ } , function ( ) {
477471 continueProcess ( true ) ;
478- }
479- } , function ( ) {
480- continueProcess ( true ) ;
481- } )
482- } else {
472+ } )
473+ } else {
474+ continueProcess ( true ) ;
475+ }
476+ } , function ( ) {
483477 continueProcess ( true ) ;
484- }
485- } , function ( ) {
486- continueProcess ( true ) ;
487- } )
478+ } )
479+ } catch ( e ) {
480+ continueProcess ( true ) ;
481+ }
488482} else {
489483 continueProcess ( true ) ;
490484}
0 commit comments