UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path_1 = require("path");
4const common_1 = require("./common");
5async 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}
10exports.getPlugins = getPlugins;
11async 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}
47exports.resolvePlugin = resolvePlugin;
48function 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}
53exports.getDependencies = getDependencies;
54function 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}
62exports.fixName = fixName;
63function removeScope(name) {
64 var parts = name.split('/');
65 if (parts.length > 1) {
66 name = parts[parts.length - 1];
67 }
68 return name;
69}
70exports.removeScope = removeScope;
71function 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}
87exports.printPlugins = printPlugins;
88function 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}
96exports.getPluginPlatform = getPluginPlatform;
97function 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}
107exports.getPlatformElement = getPlatformElement;
108function 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 /* Core */;
116}
117exports.getPluginType = getPluginType;
118/**
119 * Get each JavaScript Module for the given plugin
120 */
121function getJSModules(p, platform) {
122 return getAllElements(p, platform, 'js-module');
123}
124exports.getJSModules = getJSModules;
125function getFilePath(config, plugin, path) {
126 if (path.startsWith('node_modules')) {
127 let pathSegments = path.split('/').slice(1);
128 if (pathSegments[0].startsWith('@')) {
129 pathSegments = [pathSegments[0] + '/' + pathSegments[1], ...pathSegments.slice(2)];
130 }
131 let filePath = common_1.resolveNode(config, ...pathSegments);
132 if (!filePath) {
133 throw new Error(`Can't resolve module ${pathSegments[0]}`);
134 }
135 return filePath;
136 }
137 return path_1.join(plugin.rootPath, path);
138}
139exports.getFilePath = getFilePath;
140/**
141 * For a given plugin, return all the plugin.xml elements with elementName, checking root and specified platform
142 */
143function getAllElements(p, platform, elementName) {
144 let modules = [];
145 if (p.xml[elementName]) {
146 modules = modules.concat(p.xml[elementName]);
147 }
148 const platformModules = getPluginPlatform(p, platform);
149 if (platformModules && platformModules[elementName]) {
150 modules = modules.concat(platformModules[elementName]);
151 }
152 return modules;
153}
154exports.getAllElements = getAllElements;