11#!/usr/bin/env node
22const fs = require ( "fs" ) ;
33const path = require ( "path" ) ;
4+ const http = require ( "http" ) ;
5+ const https = require ( "https" ) ;
46const 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' ) ;
97
108const downloadPathTemplate =
119 "https://github.com/devspace-sh/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}" ;
@@ -62,6 +60,70 @@ const requestHeaders = {
6260} ;
6361let packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) ) ;
6462
63+ const request = function ( requestUrl , options ) {
64+ const requestOptions = options || { } ;
65+ const headers = requestOptions . headers || { } ;
66+ const maxRedirects = requestOptions . maxRedirects == null ? 10 : requestOptions . maxRedirects ;
67+
68+ return new Promise ( function ( resolve , reject ) {
69+ const makeRequest = function ( currentUrl , redirectsRemaining ) {
70+ const parsedUrl = new URL ( currentUrl ) ;
71+ const client = parsedUrl . protocol === "https:" ? https : http ;
72+ const req = client . request ( parsedUrl , { headers } , function ( res ) {
73+ const statusCode = res . statusCode || 0 ;
74+
75+ if (
76+ statusCode >= 300 &&
77+ statusCode < 400 &&
78+ res . headers . location &&
79+ redirectsRemaining > 0
80+ ) {
81+ const redirectUrl = new URL ( res . headers . location , currentUrl ) . toString ( ) ;
82+ res . resume ( ) ;
83+ makeRequest ( redirectUrl , redirectsRemaining - 1 ) ;
84+ return ;
85+ }
86+
87+ resolve ( {
88+ ok : statusCode >= 200 && statusCode < 300 ,
89+ status : statusCode ,
90+ statusText : res . statusMessage || "" ,
91+ url : currentUrl ,
92+ body : res ,
93+ } ) ;
94+ } ) ;
95+
96+ req . on ( "error" , reject ) ;
97+ req . end ( ) ;
98+ } ;
99+
100+ makeRequest ( requestUrl , maxRedirects ) ;
101+ } ) ;
102+ } ;
103+
104+ const getSpinner = function ( ) {
105+ try {
106+ return require ( "cli-spinner" ) . Spinner ;
107+ } catch ( err ) {
108+ return function Spinner ( ) {
109+ this . setSpinnerString = function ( ) { } ;
110+ this . start = function ( ) { } ;
111+ this . stop = function ( ) { } ;
112+ } ;
113+ }
114+ } ;
115+
116+ const getInquirer = function ( ) {
117+ return require ( "inquirer" ) ;
118+ } ;
119+
120+ const getFindProcess = function ( ) {
121+ const findProcessModule = require ( "find-process" ) ;
122+ return typeof findProcessModule === "function"
123+ ? findProcessModule
124+ : findProcessModule . default ;
125+ } ;
126+
65127const sanitizeVersion = function ( version ) {
66128 if ( version == null ) {
67129 return "latest"
@@ -81,9 +143,10 @@ const sanitizeVersion = function(version) {
81143const getLatestVersion = function ( callback ) {
82144 const releasesURL = "https://github.com/devspace-sh/devspace/releases/latest" ;
83145
84- fetch ( releasesURL , { headers : requestHeaders , redirect : false } )
146+ request ( releasesURL , { headers : requestHeaders } )
85147 . then ( function ( res ) {
86148 if ( ! res . ok ) {
149+ res . body . resume ( ) ;
87150 console . error (
88151 "Error requesting URL " +
89152 releasesURL +
@@ -106,8 +169,10 @@ const getLatestVersion = function (callback) {
106169
107170 const latestVersion = matches [ 1 ] . replace ( 'v' , '' )
108171 if ( latestVersion ) {
172+ res . body . resume ( ) ;
109173 callback ( latestVersion ) ;
110174 } else {
175+ res . body . resume ( ) ;
111176 console . error ( "Unable to identify latest devspace version" ) ;
112177 process . exit ( 2 ) ;
113178 }
@@ -320,6 +385,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
320385 }
321386 } ;
322387
388+ const inquirer = getInquirer ( ) ;
323389 inquirer
324390 . prompt ( [
325391 {
@@ -377,6 +443,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
377443
378444 console . log ( "Download DevSpace CLI release: " + downloadPath + "\n" ) ;
379445
446+ const Spinner = getSpinner ( ) ;
380447 const spinner = new Spinner (
381448 "%s Downloading DevSpace CLI... (this may take a minute)"
382449 ) ;
@@ -391,15 +458,10 @@ let continueProcess = function (askRemoveGlobalFolder) {
391458 showRootError ( version ) ;
392459 } ) ;
393460
394- fetch ( downloadPath , { headers : requestHeaders , encoding : null } )
395- . catch ( function ( err ) {
396- writeStream . end ( ) ;
397- spinner . stop ( true ) ;
398- console . error ( "Error requesting URL: " + downloadPath ) ;
399- process . exit ( 6 ) ;
400- } )
461+ request ( downloadPath , { headers : requestHeaders } )
401462 . then ( function ( res ) {
402463 if ( ! res . ok ) {
464+ res . body . resume ( ) ;
403465 writeStream . end ( ) ;
404466 spinner . stop ( true ) ;
405467
@@ -457,6 +519,12 @@ let continueProcess = function (askRemoveGlobalFolder) {
457519 showRootError ( version ) ;
458520 }
459521 }
522+ } )
523+ . catch ( function ( err ) {
524+ writeStream . end ( ) ;
525+ spinner . stop ( true ) ;
526+ console . error ( "Error requesting URL: " + downloadPath ) ;
527+ process . exit ( 6 ) ;
460528 } ) ;
461529 } ;
462530
@@ -466,6 +534,15 @@ let continueProcess = function (askRemoveGlobalFolder) {
466534}
467535
468536if ( process . ppid > 1 ) {
537+ let findProcess ;
538+
539+ try {
540+ findProcess = getFindProcess ( ) ;
541+ } catch ( err ) {
542+ continueProcess ( true ) ;
543+ return ;
544+ }
545+
469546 findProcess ( 'pid' , process . ppid )
470547 . then ( function ( list ) {
471548 if ( list . length === 1 && list [ 0 ] . ppid > 1 ) {
0 commit comments