Skip to content

Commit 4f81cce

Browse files
committed
Support multiple platforms when generating script manifests.
1 parent 0487fbc commit 4f81cce

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

plugin.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<repo>https://github.com/formulateco/cordova-plugin-injectview.git</repo>
99
<issue>https://github.com/formulateco/cordova-plugin-injectview/issues</issue>
1010

11-
<!-- Generate Cordova script manifest as part of compilation. -->
11+
<!--
12+
Generate Cordova script manifest as part of compilation.
13+
We use this manifest at runtime to determine which scripts to inject.
14+
-->
1215
<hook type="before_compile" src="scripts/before-compile.js" />
1316

1417
<platform name="android">

scripts/before-compile.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ function fatal(message) {
55
throw new Error(`cordova-plugin-injectview: ${message}`);
66
}
77

8-
module.exports = function(context) {
9-
let root = context.opts.projectRoot;
10-
if (!fs.existsSync(root)) {
11-
fatal(`invalid project root: ${root}`);
8+
function createCordovaManifest(rootPath, platformName, platformPath) {
9+
if (!platformName) {
10+
fatal(`invalid platform: ${platformName}`);
1211
}
1312

14-
let platform = context.opts.platforms[0];
15-
if (!platform) {
16-
fatal(`invalid platform: ${platform}`);
13+
if (!platformPath) {
14+
fatal(`invalid platform path for ${platformName}`);
1715
}
1816

19-
let platformPath = context.opts.paths[0];
2017
if (!fs.existsSync(platformPath)) {
2118
fatal(`platform output path does not exist: ${platformPath}`);
2219
}
@@ -31,19 +28,23 @@ module.exports = function(context) {
3128
fatal(`Cordova plugins file does not exist: ${cordovaPluginsFilename}`);
3229
}
3330

34-
let configFilename = path.join(root, 'platforms', platform, `${platform}.json`);
31+
// Load plugin info from platform configuration.
32+
let configFilename = path.join(rootPath, 'platforms', platformName, `${platformName}.json`);
3533
if (!fs.existsSync(configFilename)) {
36-
fatal(`platform configuration file does not exist for ${platform}: ${configFilename}`);
34+
fatal(`platform configuration file does not exist for ${platformName}: ${configFilename}`);
3735
}
3836

3937
let config = JSON.parse(fs.readFileSync(configFilename));
4038
let modules = (config && config.modules) || [];
4139

40+
// Always include cordova.js and cordova_plugins.js
41+
// as part of the Cordova script manifest.
4242
let scriptFilenames = [
4343
path.posix.join('www', 'cordova.js'),
4444
path.posix.join('www', 'cordova_plugins.js')
4545
];
4646

47+
// Include each plugin as part of the manifest.
4748
for (let module of modules) {
4849
let filename = module.file;
4950
if (!filename) {
@@ -58,10 +59,25 @@ module.exports = function(context) {
5859
scriptFilenames.push(path.posix.join('www', filename));
5960
}
6061

62+
// Write script manifest to be included as an app resource.
6163
let outputFilename = path.join(platformPath, 'cordova-plugin-injectview.json');
6264
fs.writeFileSync(outputFilename, JSON.stringify(scriptFilenames));
6365

6466
if (!fs.existsSync(outputFilename)) {
6567
fatal(`output file does not exist: ${outputFilename}`);
6668
}
69+
}
70+
71+
module.exports = function(context) {
72+
let rootPath = context.opts.projectRoot;
73+
if (!fs.existsSync(rootPath)) {
74+
fatal(`invalid project root: ${rootPath}`);
75+
}
76+
77+
// Create manifest for each specified platform.
78+
for (let i = 0; i < context.opts.platforms.length; i++) {
79+
let platformName = context.opts.platforms[i];
80+
let platformPath = context.opts.paths[i];
81+
createCordovaManifest(rootPath, platformName, platformPath);
82+
}
6783
};

0 commit comments

Comments
 (0)