forked from lesteenman/cordova-plugin-injectview
-
Notifications
You must be signed in to change notification settings - Fork 7
/
update-manifest.js
93 lines (77 loc) · 3.45 KB
/
update-manifest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require('fs');
const path = require('path');
function fatal(message) {
throw new Error(`cordova-plugin-injectview: ${message}`);
}
function createManifest(rootPath, platformName, manifestFilename) {
let manifestDir = path.dirname(manifestFilename);
if (!fs.existsSync(manifestDir)) {
// The manifest's parent directory might not exist yet, e.g. after a cordova clean.
// In this case, we do not create the manifest yet and let a future hook handle it.
return;
}
// Load plugin info from platform configuration.
let configFilename = path.join(rootPath, 'platforms', platformName, `${platformName}.json`);
if (!fs.existsSync(configFilename)) {
fatal(`platform configuration file does not exist for ${platformName}: ${configFilename}`);
}
let config = JSON.parse(fs.readFileSync(configFilename));
let modules = (config && config.modules) || [];
// Always include cordova.js and cordova_plugins.js
// as part of the Cordova script manifest.
let scriptFilenames = [
path.posix.join('www', 'cordova.js'),
path.posix.join('www', 'cordova_plugins.js')
];
// Include each plugin as part of the manifest.
for (let module of modules) {
let filename = module.file;
if (!filename) {
continue;
}
scriptFilenames.push(path.posix.join('www', filename));
}
// Write script manifest to be included as an app resource.
fs.writeFileSync(manifestFilename, JSON.stringify(scriptFilenames));
}
function removeManifest(manifestFilename) {
if (!fs.existsSync(manifestFilename)) {
return;
}
fs.unlinkSync(manifestFilename);
}
module.exports = function(context) {
let rootPath = context.opts.projectRoot;
if (!fs.existsSync(rootPath)) {
fatal(`invalid project root: ${rootPath}`);
}
// Get the active platforms.
let cordovaPlatforms = (context.opts.cordova && context.opts.cordova.platforms) || [];
let optPlatforms = context.opts.platforms || [];
let allPlatforms = [...cordovaPlatforms, ...optPlatforms].map(name => (name || '').split('@')[0].trim()).filter(Boolean);
let platforms = new Set(allPlatforms);
for (let platformName of platforms) {
if (platformName != 'android' && platformName != 'ios') {
// Unsupported platform.
continue;
}
// Cordova has two locations for scripts. platform_www is a staging location that gets copied
// over to a platform-specific location as part of `cordova prepare`. The platform-specific
// location is used during compilation. Just to be thorough, we copy the manifest to both locations.
let platformPaths = [path.join(rootPath, 'platforms', platformName, 'platform_www')];
if (platformName == 'android') {
platformPaths.push(path.join(rootPath, 'platforms', platformName, 'app', 'src', 'main', 'assets', 'www'));
} else {
platformPaths.push(path.join(rootPath, 'platforms', platformName, 'www'));
}
for (let platformPath of platformPaths) {
let manifestFilename = path.join(platformPath, 'cordova-plugin-injectview.json');
// Create or remove manifest file based on hook.
if (context.hook == 'before_plugin_uninstall') {
removeManifest(manifestFilename);
} else {
createManifest(rootPath, platformName, manifestFilename);
}
}
}
};