@@ -3,10 +3,14 @@ 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' ;
6
7
import buildUtils from './build-utils' ;
7
8
import stringArgv from 'string-argv' ;
8
9
const { debug } = buildUtils ;
9
- const archMap = new Map ( [ [ 'x64' , 'amd64' ] , [ 'x86' , '386' ] ] ) ;
10
+ const archMap = new Map ( [
11
+ [ 'x64' , 'amd64' ] ,
12
+ [ 'x86' , '386' ] ,
13
+ ] ) ;
10
14
const platformMap = new Map ( [ [ 'win32' , 'windows' ] ] ) ;
11
15
12
16
// Location where the `go` binary will be installed after `postinstall`
@@ -126,35 +130,50 @@ export async function downloadGo(
126
130
platform = process . platform ,
127
131
arch = process . arch
128
132
) {
129
- // Check if `go` is already installed in user's `$PATH`
130
- const { failed , stdout } = await execa ( 'go' , [ 'version' ] , { reject : false } ) ;
133
+ // Check default `Go` in user machine
134
+ const isUserGo = await pathExists ( join ( homedir ( ) , 'go' ) ) ;
131
135
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
- }
136
-
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 ) ;
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' ] ) ;
144
140
145
- if ( ! res . ok ) {
146
- throw new Error ( `Failed to download: ${ url } ( ${ res . status } )` ) ;
141
+ if ( parseInt ( stdout . split ( '.' ) [ 1 ] ) >= 11 ) {
142
+ return createGo ( dir , platform , arch ) ;
147
143
}
148
144
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
- } ) ;
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
+ } ) ;
176
+ }
177
+ return createGo ( dir , platform , arch ) ;
158
178
}
159
- return createGo ( dir , platform , arch ) ;
160
179
}
0 commit comments