@@ -4,19 +4,45 @@ const fs = require('graceful-fs')
44const log = require ( 'npmlog' )
55const path = require ( 'path' )
66
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 } )
939 if ( ! config . target_defaults ) {
1040 config . target_defaults = { }
1141 }
1242 if ( ! config . variables ) {
1343 config . variables = { }
1444 }
15- return config
16- }
1745
18- function getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } ) {
19- const config = getBaseConfigGypi ( )
2046 const defaults = config . target_defaults
2147 const variables = config . variables
2248
@@ -85,13 +111,13 @@ function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
85111 return config
86112}
87113
88- function createConfigGypi ( { gyp, buildDir, nodeDir, vsInfo } , callback ) {
114+ async function createConfigGypi ( { gyp, buildDir, nodeDir, vsInfo } ) {
89115 const configFilename = 'config.gypi'
90116 const configPath = path . resolve ( buildDir , configFilename )
91117
92118 log . verbose ( 'build/' + configFilename , 'creating config file' )
93119
94- const config = getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } )
120+ const config = await getCurrentConfigGypi ( { gyp, nodeDir, vsInfo } )
95121
96122 // ensures that any boolean values in config.gypi get stringified
97123 function boolsToString ( k , v ) {
@@ -108,12 +134,13 @@ function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) {
108134
109135 const json = JSON . stringify ( config , boolsToString , 2 )
110136 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
114140}
115141
116142module . exports = createConfigGypi
117143module . exports . test = {
144+ parseConfigGypi : parseConfigGypi ,
118145 getCurrentConfigGypi : getCurrentConfigGypi
119146}
0 commit comments