forked from JaneaSystems/nodejs-mobile-cordova
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: refactor after-prepare-patch-npm-packages (#40)
- Loading branch information
1 parent
ee151bf
commit 42ba2f7
Showing
1 changed file
with
43 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,86 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
function getBinaryPathConfiguration(binaryPathConfiguration) { | ||
return binaryPathConfiguration | ||
.replace(/\{node_abi\}/g, "node_abi") | ||
.replace(/\{platform\}/g, "platform") | ||
.replace(/\{arch\}/g, "arch") | ||
.replace(/\{target_arch\}/g, "target_arch") | ||
.replace(/\{libc\}/g, "libc"); | ||
} | ||
|
||
// 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) | ||
{ | ||
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"); | ||
binaryPathConfiguration = binaryPathConfiguration.replace(/\{libc\}/g, "libc"); | ||
if (packageJSON?.binary?.module_path) { | ||
const binaryPathConfiguration = getBinaryPathConfiguration( | ||
packageJSON.binary.module_path | ||
); | ||
packageJSON.binary.module_path = binaryPathConfiguration; | ||
let packageWriteData = JSON.stringify(packageJSON, null, 2); | ||
const packageWriteData = JSON.stringify(packageJSON, null, 2); | ||
fs.writeFileSync(filePath, packageWriteData); | ||
} | ||
} | ||
|
||
// Visits every package.json to apply patches. | ||
function visitPackageJSON(folderPath) | ||
{ | ||
function visitPackageJSON(folderPath) { | ||
let files = fs.readdirSync(folderPath); | ||
for (var i in files) { | ||
for (let i in files) { | ||
let name = files[i]; | ||
let filePath = path.join(folderPath, files[i]); | ||
if (fs.lstatSync(filePath).isDirectory()) { | ||
visitPackageJSON(filePath); | ||
} else { | ||
if (name === 'package.json') { | ||
try { | ||
patchPackageJSON_preNodeGyp_modulePath(filePath); | ||
} catch (e) { | ||
console.warn( | ||
'Failed to patch the file : "' + | ||
} 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) | ||
); | ||
} | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Applies the patch to the selected platform | ||
function patchTargetPlatform(context, platform) { | ||
const platformPath = path.join(context.opts.projectRoot, 'platforms', platform); | ||
const platformAPI = require(path.join(platformPath, 'cordova', 'Api')); | ||
const platformPath = path.join( | ||
context.opts.projectRoot, | ||
"platforms", | ||
platform | ||
); | ||
const platformAPI = require(path.join(platformPath, "cordova", "Api")); | ||
let platformAPIInstance; | ||
try { | ||
platformAPIInstance = new platformAPI(); | ||
} catch (e) { | ||
platformAPIInstance = new platformAPI(platform, platformPath); | ||
} | ||
const wwwPath = platformAPIInstance.locations.www; | ||
const nodeModulesPathToPatch = path.join(wwwPath, 'nodejs-project', 'node_modules'); | ||
const nodeModulesPathToPatch = path.join( | ||
wwwPath, | ||
"nodejs-project", | ||
"node_modules" | ||
); | ||
if (fs.existsSync(nodeModulesPathToPatch)) { | ||
visitPackageJSON(nodeModulesPathToPatch); | ||
} | ||
} | ||
|
||
module.exports = function(context) | ||
{ | ||
if (context.opts.platforms.indexOf('android') >= 0) { | ||
patchTargetPlatform(context, 'android'); | ||
module.exports = function (context) { | ||
if (context.opts.platforms.indexOf("android") >= 0) { | ||
patchTargetPlatform(context, "android"); | ||
} | ||
if (context.opts.platforms.indexOf('ios') >= 0) { | ||
patchTargetPlatform(context, 'ios'); | ||
if (context.opts.platforms.indexOf("ios") >= 0) { | ||
patchTargetPlatform(context, "ios"); | ||
} | ||
} | ||
}; |