@@ -4,19 +4,45 @@ const fs = require('graceful-fs')
4
4
const log = require ( 'npmlog' )
5
5
const path = require ( 'path' )
6
6
7
- function getBaseConfigGypi ( ) {
8
- const config = JSON . parse ( JSON . stringify ( process . config ) )
7
+ function parseConfigGypi ( config ) {
8
+ // translated from tools/js2c.py of Node.js
9
+ // 1. string comments
10
+ config = config . replace ( / # .* / g, '' )
11
+ // 2. join multiline strings
12
+ config = config . replace ( / ' $ \s + ' / mg, '' )
13
+ // 3. normalize string literals from ' into "
14
+ config = config . replace ( / ' / g, '"' )
15
+ return JSON . parse ( config )
16
+ }
17
+
18
+ async function getBaseConfigGypi ( { gyp, nodeDir } ) {
19
+ // try reading $nodeDir/include/node/config.gypi first when:
20
+ // 1. --dist-url or --nodedir is specified
21
+ // 2. and --force-process-config is not specified
22
+ const shouldReadConfigGypi = ( gyp . opts . nodedir || gyp . opts [ 'dist-url' ] ) && ! gyp . opts [ 'force-process-config' ]
23
+ if ( shouldReadConfigGypi && nodeDir ) {
24
+ try {
25
+ const baseConfigGypiPath = path . resolve ( nodeDir , 'include/node/config.gypi' )
26
+ const baseConfigGypi = await fs . promises . readFile ( baseConfigGypiPath )
27
+ return parseConfigGypi ( baseConfigGypi . toString ( ) )
28
+ } catch ( err ) {
29
+ log . warn ( 'read config.gypi' , err . message )
30
+ }
31
+ }
32
+
33
+ // fallback to process.config if it is invalid
34
+ return JSON . parse ( JSON . stringify ( process . config ) )
35
+ }
36
+
37
+ async function getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } ) {
38
+ const config = await getBaseConfigGypi ( { gyp, nodeDir } )
9
39
if ( ! config . target_defaults ) {
10
40
config . target_defaults = { }
11
41
}
12
42
if ( ! config . variables ) {
13
43
config . variables = { }
14
44
}
15
- return config
16
- }
17
45
18
- function getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } ) {
19
- const config = getBaseConfigGypi ( )
20
46
const defaults = config . target_defaults
21
47
const variables = config . variables
22
48
@@ -85,13 +111,13 @@ function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
85
111
return config
86
112
}
87
113
88
- function createConfigGypi ( { gyp, buildDir, nodeDir, vsInfo } , callback ) {
114
+ async function createConfigGypi ( { gyp, buildDir, nodeDir, vsInfo } ) {
89
115
const configFilename = 'config.gypi'
90
116
const configPath = path . resolve ( buildDir , configFilename )
91
117
92
118
log . verbose ( 'build/' + configFilename , 'creating config file' )
93
119
94
- const config = getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } )
120
+ const config = await getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } )
95
121
96
122
// ensures that any boolean values in config.gypi get stringified
97
123
function boolsToString ( k , v ) {
@@ -108,12 +134,13 @@ function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) {
108
134
109
135
const json = JSON . stringify ( config , boolsToString , 2 )
110
136
log . verbose ( 'build/' + configFilename , 'writing out config file: %s' , configPath )
111
- fs . writeFile ( configPath , [ prefix , json , '' ] . join ( '\n' ) , ( err ) => {
112
- callback ( err , configPath )
113
- } )
137
+ await fs . promises . writeFile ( configPath , [ prefix , json , '' ] . join ( '\n' ) )
138
+
139
+ return configPath
114
140
}
115
141
116
142
module . exports = createConfigGypi
117
143
module . exports . test = {
144
+ parseConfigGypi : parseConfigGypi ,
118
145
getCurrentConfigGypi : getCurrentConfigGypi
119
146
}
0 commit comments