@@ -3,14 +3,10 @@ import execa from 'execa';
3
3
import fetch from 'node-fetch' ;
4
4
import { mkdirp , pathExists } from 'fs-extra' ;
5
5
import { dirname , join } from 'path' ;
6
- import { homedir } from 'os' ;
7
6
import buildUtils from './build-utils' ;
8
7
import stringArgv from 'string-argv' ;
9
8
const { debug } = buildUtils ;
10
- const archMap = new Map ( [
11
- [ 'x64' , 'amd64' ] ,
12
- [ 'x86' , '386' ] ,
13
- ] ) ;
9
+ const archMap = new Map ( [ [ 'x64' , 'amd64' ] , [ 'x86' , '386' ] ] ) ;
14
10
const platformMap = new Map ( [ [ 'win32' , 'windows' ] ] ) ;
15
11
16
12
// Location where the `go` binary will be installed after `postinstall`
@@ -130,50 +126,35 @@ export async function downloadGo(
130
126
platform = process . platform ,
131
127
arch = process . arch
132
128
) {
133
- // Check default `Go` in user machine
134
- const isUserGo = await pathExists ( join ( homedir ( ) , 'go' ) ) ;
129
+ // Check if `go` is already installed in user's `$PATH`
130
+ const { failed , stdout } = await execa ( 'go' , [ 'version' ] , { reject : false } ) ;
135
131
136
- // If we found GOPATH in ENV, or default `Go` path exists
137
- // asssume that user have `Go` installed
138
- if ( isUserGo || process . env . GOPATH !== undefined ) {
139
- const { stdout } = await execa ( 'go' , [ 'version' ] ) ;
132
+ if ( ! failed && parseInt ( stdout . split ( '.' ) [ 1 ] ) >= 11 ) {
133
+ debug ( 'Using system installed version of `go`: %o' , stdout . trim ( ) ) ;
134
+ return createGo ( dir , platform , arch ) ;
135
+ }
140
136
141
- if ( parseInt ( stdout . split ( '.' ) [ 1 ] ) >= 11 ) {
142
- return createGo ( dir , platform , arch ) ;
143
- }
137
+ // Check `go` bin in builder CWD
138
+ const isGoExist = await pathExists ( join ( dir , 'bin' ) ) ;
139
+ if ( ! isGoExist ) {
140
+ debug ( 'Installing `go` v%s to %o for %s %s' , version , dir , platform , arch ) ;
141
+ const url = getGoUrl ( version , platform , arch ) ;
142
+ debug ( 'Downloading `go` URL: %o' , url ) ;
143
+ const res = await fetch ( url ) ;
144
144
145
- throw new Error (
146
- `Your current ${ stdout } doesn't support Go Modules. Please update.`
147
- ) ;
148
- } else {
149
- // Check `Go` bin in builder CWD
150
- const isGoExist = await pathExists ( join ( dir , 'bin' ) ) ;
151
- if ( ! isGoExist ) {
152
- debug (
153
- 'Installing `go` v%s to %o for %s %s' ,
154
- version ,
155
- dir ,
156
- platform ,
157
- arch
158
- ) ;
159
- const url = getGoUrl ( version , platform , arch ) ;
160
- debug ( 'Downloading `go` URL: %o' , url ) ;
161
- const res = await fetch ( url ) ;
162
-
163
- if ( ! res . ok ) {
164
- throw new Error ( `Failed to download: ${ url } (${ res . status } )` ) ;
165
- }
166
-
167
- // TODO: use a zip extractor when `ext === "zip"`
168
- await mkdirp ( dir ) ;
169
- await new Promise ( ( resolve , reject ) => {
170
- res . body
171
- . on ( 'error' , reject )
172
- . pipe ( tar . extract ( { cwd : dir , strip : 1 } ) )
173
- . on ( 'error' , reject )
174
- . on ( 'finish' , resolve ) ;
175
- } ) ;
145
+ if ( ! res . ok ) {
146
+ throw new Error ( `Failed to download: ${ url } (${ res . status } )` ) ;
176
147
}
177
- return createGo ( dir , platform , arch ) ;
148
+
149
+ // TODO: use a zip extractor when `ext === "zip"`
150
+ await mkdirp ( dir ) ;
151
+ await new Promise ( ( resolve , reject ) => {
152
+ res . body
153
+ . on ( 'error' , reject )
154
+ . pipe ( tar . extract ( { cwd : dir , strip : 1 } ) )
155
+ . on ( 'error' , reject )
156
+ . on ( 'finish' , resolve ) ;
157
+ } ) ;
178
158
}
159
+ return createGo ( dir , platform , arch ) ;
179
160
}
0 commit comments