-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: patch node-pre-gyp module path variables
Patches node-pre-gyp's binary module_path substitutable variables with fixed values in the native modules' package.json, so that cross compiled native modules will be built in the same path where they are expected to be found at runtime by node-pre-gyp. Creates a temporary build directory in Android's build steps for this patching to take place.
- Loading branch information
1 parent
62c2670
commit d933954
Showing
3 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Patches a package.json in case it has variable substitution for | ||
// the module's binary at runtime. Since we are cross-compiling | ||
// for mobile, this substitution will have different values at | ||
// build time and runtime, so we pre-substitute them with fixed | ||
// values. | ||
function patchPackageJSON_preNodeGyp_modulePath(filePath) | ||
{ | ||
let packageReadData = fs.readFileSync(filePath); | ||
let packageJSON = JSON.parse(packageReadData); | ||
if ( packageJSON && packageJSON.binary && packageJSON.binary.module_path ) { | ||
let binaryPathConfiguration = packageJSON.binary.module_path; | ||
binaryPathConfiguration = binaryPathConfiguration.replace(/\{node_abi\}/g, "node_abi"); | ||
binaryPathConfiguration = binaryPathConfiguration.replace(/\{platform\}/g, "platform"); | ||
binaryPathConfiguration = binaryPathConfiguration.replace(/\{arch\}/g, "arch"); | ||
binaryPathConfiguration = binaryPathConfiguration.replace(/\{target_arch\}/g, "target_arch"); | ||
packageJSON.binary.module_path = binaryPathConfiguration; | ||
let packageWriteData = JSON.stringify(packageJSON, null, 2); | ||
fs.writeFileSync(filePath, packageWriteData); | ||
} | ||
} | ||
|
||
// Visits every package.json to apply patches. | ||
function visitPackageJSON(folderPath) | ||
{ | ||
let files = fs.readdirSync(folderPath); | ||
for (var i in files) { | ||
let name = files[i]; | ||
let filePath = path.join(folderPath, files[i]); | ||
if(fs.statSync(filePath).isDirectory()) { | ||
visitPackageJSON(filePath); | ||
} else { | ||
if (name === 'package.json') { | ||
try { | ||
patchPackageJSON_preNodeGyp_modulePath(filePath); | ||
} catch (e) { | ||
console.warn( | ||
'Failed to patch the file : "' + | ||
filePath + | ||
'". The following error was thrown: ' + | ||
JSON.stringify(e) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (process.argv.length >=3) | ||
{ | ||
if (fs.existsSync(process.argv[2])) { | ||
visitPackageJSON(process.argv[2]); | ||
} | ||
process.exit(0); | ||
} else { | ||
console.error("A path is expected as an argument."); | ||
process.exit(1); | ||
} |