plugin.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAllElements = exports.getFilePath = exports.getAssets = exports.getJSModules = exports.getPluginType = exports.getPlatformElement = exports.getPluginPlatform = exports.printPlugins = exports.fixName = exports.getDependencies = exports.resolvePlugin = exports.getPlugins = exports.getIncludedPluginPackages = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_fs_1 = require("@ionic/utils-fs");
  6. const path_1 = require("path");
  7. const colors_1 = tslib_1.__importDefault(require("./colors"));
  8. const errors_1 = require("./errors");
  9. const log_1 = require("./log");
  10. const node_1 = require("./util/node");
  11. const xml_1 = require("./util/xml");
  12. function getIncludedPluginPackages(config, platform) {
  13. var _a, _b, _c, _d;
  14. const { extConfig } = config.app;
  15. switch (platform) {
  16. case 'android':
  17. return (_b = (_a = extConfig.android) === null || _a === void 0 ? void 0 : _a.includePlugins) !== null && _b !== void 0 ? _b : extConfig.includePlugins;
  18. case 'ios':
  19. return (_d = (_c = extConfig.ios) === null || _c === void 0 ? void 0 : _c.includePlugins) !== null && _d !== void 0 ? _d : extConfig.includePlugins;
  20. }
  21. }
  22. exports.getIncludedPluginPackages = getIncludedPluginPackages;
  23. async function getPlugins(config, platform) {
  24. var _a;
  25. const possiblePlugins = (_a = getIncludedPluginPackages(config, platform)) !== null && _a !== void 0 ? _a : getDependencies(config);
  26. const resolvedPlugins = await Promise.all(possiblePlugins.map(async (p) => resolvePlugin(config, p)));
  27. return resolvedPlugins.filter((p) => !!p);
  28. }
  29. exports.getPlugins = getPlugins;
  30. async function resolvePlugin(config, name) {
  31. try {
  32. const packagePath = (0, node_1.resolveNode)(config.app.rootDir, name, 'package.json');
  33. if (!packagePath) {
  34. (0, errors_1.fatal)(`Unable to find ${colors_1.default.strong(`node_modules/${name}`)}.\n` +
  35. `Are you sure ${colors_1.default.strong(name)} is installed?`);
  36. }
  37. const rootPath = (0, path_1.dirname)(packagePath);
  38. const meta = await (0, utils_fs_1.readJSON)(packagePath);
  39. if (!meta) {
  40. return null;
  41. }
  42. if (meta.capacitor) {
  43. return {
  44. id: name,
  45. name: fixName(name),
  46. version: meta.version,
  47. rootPath,
  48. repository: meta.repository,
  49. manifest: meta.capacitor,
  50. };
  51. }
  52. const pluginXMLPath = (0, path_1.join)(rootPath, 'plugin.xml');
  53. const xmlMeta = await (0, xml_1.readXML)(pluginXMLPath);
  54. return {
  55. id: name,
  56. name: fixName(name),
  57. version: meta.version,
  58. rootPath: rootPath,
  59. repository: meta.repository,
  60. xml: xmlMeta.plugin,
  61. };
  62. }
  63. catch (e) {
  64. // ignore
  65. }
  66. return null;
  67. }
  68. exports.resolvePlugin = resolvePlugin;
  69. function getDependencies(config) {
  70. var _a, _b;
  71. return [
  72. ...Object.keys((_a = config.app.package.dependencies) !== null && _a !== void 0 ? _a : {}),
  73. ...Object.keys((_b = config.app.package.devDependencies) !== null && _b !== void 0 ? _b : {}),
  74. ];
  75. }
  76. exports.getDependencies = getDependencies;
  77. function fixName(name) {
  78. name = name
  79. .replace(/\//g, '_')
  80. .replace(/-/g, '_')
  81. .replace(/@/g, '')
  82. .replace(/_\w/g, m => m[1].toUpperCase());
  83. return name.charAt(0).toUpperCase() + name.slice(1);
  84. }
  85. exports.fixName = fixName;
  86. function printPlugins(plugins, platform, type = 'capacitor') {
  87. if (plugins.length === 0) {
  88. return;
  89. }
  90. let msg;
  91. const plural = plugins.length === 1 ? '' : 's';
  92. switch (type) {
  93. case 'cordova':
  94. msg = `Found ${plugins.length} Cordova plugin${plural} for ${colors_1.default.strong(platform)}:\n`;
  95. break;
  96. case 'incompatible':
  97. msg = `Found ${plugins.length} incompatible Cordova plugin${plural} for ${colors_1.default.strong(platform)}, skipped install:\n`;
  98. break;
  99. case 'capacitor':
  100. msg = `Found ${plugins.length} Capacitor plugin${plural} for ${colors_1.default.strong(platform)}:\n`;
  101. break;
  102. }
  103. msg += plugins.map(p => `${p.id}${colors_1.default.weak(`@${p.version}`)}`).join('\n');
  104. log_1.logger.info(msg);
  105. }
  106. exports.printPlugins = printPlugins;
  107. function getPluginPlatform(p, platform) {
  108. const platforms = p.xml.platform;
  109. if (platforms) {
  110. const platforms = p.xml.platform.filter(function (item) {
  111. return item.$.name === platform;
  112. });
  113. return platforms[0];
  114. }
  115. return [];
  116. }
  117. exports.getPluginPlatform = getPluginPlatform;
  118. function getPlatformElement(p, platform, elementName) {
  119. const platformTag = getPluginPlatform(p, platform);
  120. if (platformTag) {
  121. const element = platformTag[elementName];
  122. if (element) {
  123. return element;
  124. }
  125. }
  126. return [];
  127. }
  128. exports.getPlatformElement = getPlatformElement;
  129. function getPluginType(p, platform) {
  130. var _a, _b, _c, _d;
  131. switch (platform) {
  132. case 'ios':
  133. return (_b = (_a = p.ios) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : 0 /* PluginType.Core */;
  134. case 'android':
  135. return (_d = (_c = p.android) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : 0 /* PluginType.Core */;
  136. }
  137. return 0 /* PluginType.Core */;
  138. }
  139. exports.getPluginType = getPluginType;
  140. /**
  141. * Get each JavaScript Module for the given plugin
  142. */
  143. function getJSModules(p, platform) {
  144. return getAllElements(p, platform, 'js-module');
  145. }
  146. exports.getJSModules = getJSModules;
  147. /**
  148. * Get each asset tag for the given plugin
  149. */
  150. function getAssets(p, platform) {
  151. return getAllElements(p, platform, 'asset');
  152. }
  153. exports.getAssets = getAssets;
  154. function getFilePath(config, plugin, path) {
  155. if (path.startsWith('node_modules')) {
  156. let pathSegments = path.split('/').slice(1);
  157. if (pathSegments[0].startsWith('@')) {
  158. pathSegments = [
  159. pathSegments[0] + '/' + pathSegments[1],
  160. ...pathSegments.slice(2),
  161. ];
  162. }
  163. const filePath = (0, node_1.resolveNode)(config.app.rootDir, ...pathSegments);
  164. if (!filePath) {
  165. throw new Error(`Can't resolve module ${pathSegments[0]}`);
  166. }
  167. return filePath;
  168. }
  169. return (0, path_1.join)(plugin.rootPath, path);
  170. }
  171. exports.getFilePath = getFilePath;
  172. /**
  173. * For a given plugin, return all the plugin.xml elements with elementName, checking root and specified platform
  174. */
  175. function getAllElements(p, platform, elementName) {
  176. let modules = [];
  177. if (p.xml[elementName]) {
  178. modules = modules.concat(p.xml[elementName]);
  179. }
  180. const platformModules = getPluginPlatform(p, platform);
  181. if (platformModules === null || platformModules === void 0 ? void 0 : platformModules[elementName]) {
  182. modules = modules.concat(platformModules[elementName]);
  183. }
  184. return modules;
  185. }
  186. exports.getAllElements = getAllElements;