Skip to content

Commit

Permalink
plugin: refactor after-prepare-patch-npm-packages (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
okhiroyuki authored Oct 28, 2023
1 parent ee151bf commit 42ba2f7
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions install/hooks/both/after-prepare-patch-npm-packages.js
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");
}
}
};

0 comments on commit 42ba2f7

Please sign in to comment.