1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path_1 = require("path");
|
4 | const common_1 = require("./common");
|
5 | async function getPlugins(config) {
|
6 | const deps = getDependencies(config);
|
7 | const plugins = await Promise.all(deps.map(async (p) => resolvePlugin(config, p)));
|
8 | return plugins.filter(p => !!p);
|
9 | }
|
10 | exports.getPlugins = getPlugins;
|
11 | async function resolvePlugin(config, name) {
|
12 | try {
|
13 | const rootPath = common_1.resolveNode(config, name);
|
14 | if (!rootPath) {
|
15 | common_1.logFatal(`Unable to find node_modules/${name}. Are you sure ${name} is installed?`);
|
16 | return null;
|
17 | }
|
18 | const packagePath = path_1.join(rootPath, 'package.json');
|
19 | const meta = await common_1.readJSON(packagePath);
|
20 | if (!meta) {
|
21 | return null;
|
22 | }
|
23 | if (meta.capacitor) {
|
24 | return {
|
25 | id: name,
|
26 | name: fixName(name),
|
27 | version: meta.version,
|
28 | rootPath: rootPath,
|
29 | repository: meta.repository,
|
30 | manifest: meta.capacitor
|
31 | };
|
32 | }
|
33 | const pluginXMLPath = path_1.join(rootPath, 'plugin.xml');
|
34 | const xmlMeta = await common_1.readXML(pluginXMLPath);
|
35 | return {
|
36 | id: name,
|
37 | name: fixName(name),
|
38 | version: meta.version,
|
39 | rootPath: rootPath,
|
40 | repository: meta.repository,
|
41 | xml: xmlMeta.plugin
|
42 | };
|
43 | }
|
44 | catch (e) { }
|
45 | return null;
|
46 | }
|
47 | exports.resolvePlugin = resolvePlugin;
|
48 | function getDependencies(config) {
|
49 | const dependencies = config.app.package.dependencies ? config.app.package.dependencies : [];
|
50 | const devDependencies = config.app.package.devDependencies ? config.app.package.devDependencies : [];
|
51 | return Object.keys(dependencies).concat(Object.keys(devDependencies));
|
52 | }
|
53 | exports.getDependencies = getDependencies;
|
54 | function fixName(name) {
|
55 | name = name
|
56 | .replace(/\//g, '_')
|
57 | .replace(/-/g, '_')
|
58 | .replace(/@/g, '')
|
59 | .replace(/_\w/g, (m) => m[1].toUpperCase());
|
60 | return name.charAt(0).toUpperCase() + name.slice(1);
|
61 | }
|
62 | exports.fixName = fixName;
|
63 | function removeScope(name) {
|
64 | var parts = name.split('/');
|
65 | if (parts.length > 1) {
|
66 | name = parts[parts.length - 1];
|
67 | }
|
68 | return name;
|
69 | }
|
70 | exports.removeScope = removeScope;
|
71 | function printPlugins(plugins, platform, type = 'capacitor') {
|
72 | const plural = plugins.length === 1 ? '' : 's';
|
73 | if (type === 'cordova') {
|
74 | common_1.log(` Found ${plugins.length} Cordova plugin${plural} for ${platform}`);
|
75 | }
|
76 | else if (type === 'incompatible' && plugins.length > 0) {
|
77 | common_1.log(` Found ${plugins.length} incompatible Cordova plugin${plural} for ${platform}, skipped install`);
|
78 | }
|
79 | else if (type === 'capacitor') {
|
80 | common_1.log(` Found ${plugins.length} Capacitor plugin${plural} for ${platform}:`);
|
81 | }
|
82 | const chalk = require('chalk');
|
83 | for (let p of plugins) {
|
84 | common_1.log(` ${chalk.bold(`${p.id}`)} (${chalk.green(p.version)})`);
|
85 | }
|
86 | }
|
87 | exports.printPlugins = printPlugins;
|
88 | function getPluginPlatform(p, platform) {
|
89 | const platforms = p.xml.platform;
|
90 | if (platforms) {
|
91 | const platforms = p.xml.platform.filter(function (item) { return item.$.name === platform; });
|
92 | return platforms[0];
|
93 | }
|
94 | return [];
|
95 | }
|
96 | exports.getPluginPlatform = getPluginPlatform;
|
97 | function getPlatformElement(p, platform, elementName) {
|
98 | const platformTag = getPluginPlatform(p, platform);
|
99 | if (platformTag) {
|
100 | const element = platformTag[elementName];
|
101 | if (element) {
|
102 | return element;
|
103 | }
|
104 | }
|
105 | return [];
|
106 | }
|
107 | exports.getPlatformElement = getPlatformElement;
|
108 | function getPluginType(p, platform) {
|
109 | if (platform === 'ios') {
|
110 | return p.ios.type;
|
111 | }
|
112 | if (platform === 'android') {
|
113 | return p.android.type;
|
114 | }
|
115 | return 0 ;
|
116 | }
|
117 | exports.getPluginType = getPluginType;
|
118 |
|
119 |
|
120 |
|
121 | function getJSModules(p, platform) {
|
122 | return getAllElements(p, platform, 'js-module');
|
123 | }
|
124 | exports.getJSModules = getJSModules;
|
125 |
|
126 |
|
127 |
|
128 | function getAssets(p, platform) {
|
129 | return getAllElements(p, platform, 'asset');
|
130 | }
|
131 | exports.getAssets = getAssets;
|
132 | function getFilePath(config, plugin, path) {
|
133 | if (path.startsWith('node_modules')) {
|
134 | let pathSegments = path.split('/').slice(1);
|
135 | if (pathSegments[0].startsWith('@')) {
|
136 | pathSegments = [pathSegments[0] + '/' + pathSegments[1], ...pathSegments.slice(2)];
|
137 | }
|
138 | let filePath = common_1.resolveNode(config, ...pathSegments);
|
139 | if (!filePath) {
|
140 | throw new Error(`Can't resolve module ${pathSegments[0]}`);
|
141 | }
|
142 | return filePath;
|
143 | }
|
144 | return path_1.join(plugin.rootPath, path);
|
145 | }
|
146 | exports.getFilePath = getFilePath;
|
147 |
|
148 |
|
149 |
|
150 | function getAllElements(p, platform, elementName) {
|
151 | let modules = [];
|
152 | if (p.xml[elementName]) {
|
153 | modules = modules.concat(p.xml[elementName]);
|
154 | }
|
155 | const platformModules = getPluginPlatform(p, platform);
|
156 | if (platformModules && platformModules[elementName]) {
|
157 | modules = modules.concat(platformModules[elementName]);
|
158 | }
|
159 | return modules;
|
160 | }
|
161 | exports.getAllElements = getAllElements;
|