1
1
'use strict'
2
2
3
- const fs = require ( 'graceful-fs' )
3
+ const fs = require ( 'graceful-fs' ) . promises
4
+ const { promisify } = require ( 'util' )
4
5
const path = require ( 'path' )
5
- const glob = require ( 'glob' )
6
- const log = require ( 'npmlog ' )
6
+ const glob = promisify ( require ( 'glob' ) )
7
+ const log = require ( './log ' )
7
8
const which = require ( 'which' )
8
9
const win = process . platform === 'win32'
9
10
10
- function build ( gyp , argv , callback ) {
11
- var platformMake = 'make'
11
+ async function build ( gyp , argv ) {
12
+ let platformMake = 'make'
12
13
if ( process . platform === 'aix' ) {
13
14
platformMake = 'gmake'
14
15
} else if ( process . platform === 'os400' ) {
@@ -21,113 +22,103 @@ function build (gyp, argv, callback) {
21
22
} )
22
23
}
23
24
24
- var makeCommand = gyp . opts . make || process . env . MAKE || platformMake
25
- var command = win ? 'msbuild' : makeCommand
26
- var jobs = gyp . opts . jobs || process . env . JOBS
27
- var buildType
28
- var config
29
- var arch
30
- var nodeDir
31
- var guessedSolution
25
+ const makeCommand = gyp . opts . make || process . env . MAKE || platformMake
26
+ let command = win ? 'msbuild' : makeCommand
27
+ const jobs = gyp . opts . jobs || process . env . JOBS
28
+ let buildType
29
+ let config
30
+ let arch
31
+ let nodeDir
32
+ let guessedSolution
32
33
33
- loadConfigGypi ( )
34
+ await loadConfigGypi ( )
34
35
35
36
/**
36
37
* Load the "config.gypi" file that was generated during "configure".
37
38
*/
38
39
39
- function loadConfigGypi ( ) {
40
- var configPath = path . resolve ( 'build' , 'config.gypi' )
41
-
42
- fs . readFile ( configPath , 'utf8' , function ( err , data ) {
43
- if ( err ) {
44
- if ( err . code === 'ENOENT' ) {
45
- callback ( new Error ( 'You must run `node-gyp configure` first!' ) )
46
- } else {
47
- callback ( err )
48
- }
49
- return
40
+ async function loadConfigGypi ( ) {
41
+ let data
42
+ try {
43
+ const configPath = path . resolve ( 'build' , 'config.gypi' )
44
+ data = await fs . readFile ( configPath , 'utf8' )
45
+ } catch ( err ) {
46
+ if ( err . code === 'ENOENT' ) {
47
+ throw new Error ( 'You must run `node-gyp configure` first!' )
48
+ } else {
49
+ throw err
50
50
}
51
- config = JSON . parse ( data . replace ( / # . + \n / , '' ) )
51
+ }
52
52
53
- // get the 'arch', 'buildType', and 'nodeDir' vars from the config
54
- buildType = config . target_defaults . default_configuration
55
- arch = config . variables . target_arch
56
- nodeDir = config . variables . nodedir
53
+ config = JSON . parse ( data . replace ( / # .+ \n / , '' ) )
57
54
58
- if ( 'debug' in gyp . opts ) {
59
- buildType = gyp . opts . debug ? 'Debug' : 'Release'
60
- }
61
- if ( ! buildType ) {
62
- buildType = 'Release'
63
- }
55
+ // get the 'arch', 'buildType', and 'nodeDir' vars from the config
56
+ buildType = config . target_defaults . default_configuration
57
+ arch = config . variables . target_arch
58
+ nodeDir = config . variables . nodedir
64
59
65
- log . verbose ( 'build type' , buildType )
66
- log . verbose ( 'architecture' , arch )
67
- log . verbose ( 'node dev dir' , nodeDir )
60
+ if ( 'debug' in gyp . opts ) {
61
+ buildType = gyp . opts . debug ? 'Debug' : 'Release'
62
+ }
63
+ if ( ! buildType ) {
64
+ buildType = 'Release'
65
+ }
68
66
69
- if ( win ) {
70
- findSolutionFile ( )
71
- } else {
72
- doWhich ( )
73
- }
74
- } )
67
+ log . verbose ( 'build type' , buildType )
68
+ log . verbose ( 'architecture' , arch )
69
+ log . verbose ( 'node dev dir' , nodeDir )
70
+
71
+ if ( win ) {
72
+ await findSolutionFile ( )
73
+ } else {
74
+ await doWhich ( )
75
+ }
75
76
}
76
77
77
78
/**
78
79
* On Windows, find the first build/*.sln file.
79
80
*/
80
81
81
- function findSolutionFile ( ) {
82
- glob ( 'build/*.sln' , function ( err , files ) {
83
- if ( err ) {
84
- return callback ( err )
85
- }
86
- if ( files . length === 0 ) {
87
- return callback ( new Error ( 'Could not find *.sln file. Did you run "configure"?' ) )
88
- }
89
- guessedSolution = files [ 0 ]
90
- log . verbose ( 'found first Solution file' , guessedSolution )
91
- doWhich ( )
92
- } )
82
+ async function findSolutionFile ( ) {
83
+ const files = await glob ( 'build/*.sln' )
84
+ if ( files . length === 0 ) {
85
+ throw new Error ( 'Could not find *.sln file. Did you run "configure"?' )
86
+ }
87
+ guessedSolution = files [ 0 ]
88
+ log . verbose ( 'found first Solution file' , guessedSolution )
89
+ await doWhich ( )
93
90
}
94
91
95
92
/**
96
93
* Uses node-which to locate the msbuild / make executable.
97
94
*/
98
95
99
- function doWhich ( ) {
96
+ async function doWhich ( ) {
100
97
// On Windows use msbuild provided by node-gyp configure
101
98
if ( win ) {
102
99
if ( ! config . variables . msbuild_path ) {
103
- return callback ( new Error (
104
- 'MSBuild is not set, please run `node-gyp configure`.' ) )
100
+ throw new Error ( 'MSBuild is not set, please run `node-gyp configure`.' )
105
101
}
106
102
command = config . variables . msbuild_path
107
103
log . verbose ( 'using MSBuild:' , command )
108
- doBuild ( )
104
+ await doBuild ( )
109
105
return
110
106
}
107
+
111
108
// First make sure we have the build command in the PATH
112
- which ( command , function ( err , execPath ) {
113
- if ( err ) {
114
- // Some other error or 'make' not found on Unix, report that to the user
115
- callback ( err )
116
- return
117
- }
118
- log . verbose ( '`which` succeeded for `' + command + '`' , execPath )
119
- doBuild ( )
120
- } )
109
+ const execPath = await which ( command )
110
+ log . verbose ( '`which` succeeded for `' + command + '`' , execPath )
111
+ await doBuild ( )
121
112
}
122
113
123
114
/**
124
115
* Actually spawn the process and compile the module.
125
116
*/
126
117
127
- function doBuild ( ) {
118
+ async function doBuild ( ) {
128
119
// Enable Verbose build
129
- var verbose = log . levels [ log . level ] <= log . levels . verbose
130
- var j
120
+ const verbose = log . logger . isVisible ( ' verbose' )
121
+ let j
131
122
132
123
if ( ! win && verbose ) {
133
124
argv . push ( 'V=1' )
@@ -147,10 +138,12 @@ function build (gyp, argv, callback) {
147
138
// Convert .gypi config target_arch to MSBuild /Platform
148
139
// Since there are many ways to state '32-bit Intel', default to it.
149
140
// N.B. msbuild's Condition string equality tests are case-insensitive.
150
- var archLower = arch . toLowerCase ( )
151
- var p = archLower === 'x64' ? 'x64'
152
- : ( archLower === 'arm' ? 'ARM'
153
- : ( archLower === 'arm64' ? 'ARM64' : 'Win32' ) )
141
+ const archLower = arch . toLowerCase ( )
142
+ const p = archLower === 'x64'
143
+ ? 'x64'
144
+ : ( archLower === 'arm'
145
+ ? 'ARM'
146
+ : ( archLower === 'arm64' ? 'ARM64' : 'Win32' ) )
154
147
argv . push ( '/p:Configuration=' + buildType + ';Platform=' + p )
155
148
if ( jobs ) {
156
149
j = parseInt ( jobs , 10 )
@@ -179,7 +172,7 @@ function build (gyp, argv, callback) {
179
172
180
173
if ( win ) {
181
174
// did the user specify their own .sln file?
182
- var hasSln = argv . some ( function ( arg ) {
175
+ const hasSln = argv . some ( function ( arg ) {
183
176
return path . extname ( arg ) === '.sln'
184
177
} )
185
178
if ( ! hasSln ) {
@@ -194,20 +187,20 @@ function build (gyp, argv, callback) {
194
187
log . verbose ( 'bin symlinks' , `adding symlinks (such as Python), at "${ buildBinsDir } ", to PATH` )
195
188
}
196
189
197
- var proc = gyp . spawn ( command , argv )
198
- proc . on ( 'exit' , onExit )
199
- }
200
-
201
- function onExit ( code , signal ) {
202
- if ( code !== 0 ) {
203
- return callback ( new Error ( '`' + command + '` failed with exit code: ' + code ) )
204
- }
205
- if ( signal ) {
206
- return callback ( new Error ( '`' + command + '` got signal: ' + signal ) )
207
- }
208
- callback ( )
190
+ const proc = gyp . spawn ( command , argv )
191
+ await new Promise ( ( resolve , reject ) => proc . on ( 'exit' , ( code , signal ) => {
192
+ if ( code !== 0 ) {
193
+ return reject ( new Error ( '`' + command + '` failed with exit code: ' + code ) )
194
+ }
195
+ if ( signal ) {
196
+ return reject ( new Error ( '`' + command + '` got signal: ' + signal ) )
197
+ }
198
+ resolve ( )
199
+ } ) )
209
200
}
210
201
}
211
202
212
- module . exports = build
203
+ module . exports = function ( gyp , argv , callback ) {
204
+ build ( gyp , argv ) . then ( callback . bind ( undefined , null ) , callback )
205
+ }
213
206
module . exports . usage = 'Invokes `' + ( win ? 'msbuild' : 'make' ) + '` and builds the module'
0 commit comments