๐Ÿ“ฆ ionic-team / capacitor

๐Ÿ“„ plugin.ts ยท 227 lines
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227import { readJSON } from 'fs-extra';
import { dirname, join } from 'path';

import c from './colors';
import type { Config } from './definitions';
import { fatal } from './errors';
import { logger } from './log';
import { resolveNode } from './util/node';
import { readXML } from './util/xml';

export const enum PluginType {
  Core,
  Cordova,
  Incompatible,
}

export interface PluginManifest {
  readonly ios?: {
    readonly src?: string;
  };
  readonly android?: {
    readonly src?: string;
  };
}

export interface Plugin {
  id: string;
  name: string;
  version: string;
  rootPath: string;
  manifest?: PluginManifest;
  repository?: any;
  xml?: any;
  ios?: {
    name: string;
    type: PluginType;
    path: string;
  };
  android?: {
    type: PluginType;
    path: string;
  };
}

export function getIncludedPluginPackages(config: Config, platform: string): readonly string[] | undefined {
  const { extConfig } = config.app;

  switch (platform) {
    case 'android':
      return extConfig.android?.includePlugins ?? extConfig.includePlugins;
    case 'ios':
      return extConfig.ios?.includePlugins ?? extConfig.includePlugins;
  }
}

export async function getPlugins(config: Config, platform: string): Promise<Plugin[]> {
  const possiblePlugins = getIncludedPluginPackages(config, platform) ?? getDependencies(config);
  const resolvedPlugins = await Promise.all(possiblePlugins.map(async (p) => resolvePlugin(config, p)));

  return resolvedPlugins.filter((p): p is Plugin => !!p);
}

export async function resolvePlugin(config: Config, name: string): Promise<Plugin | null> {
  try {
    const packagePath = resolveNode(config.app.rootDir, name, 'package.json');
    if (!packagePath) {
      fatal(`Unable to find ${c.strong(`node_modules/${name}`)}.\n` + `Are you sure ${c.strong(name)} is installed?`);
    }

    const rootPath = dirname(packagePath);
    const meta = await readJSON(packagePath);
    if (!meta) {
      return null;
    }
    if (meta.capacitor) {
      return {
        id: name,
        name: fixName(name),
        version: meta.version,
        rootPath,
        repository: meta.repository,
        manifest: meta.capacitor,
      };
    }
    const pluginXMLPath = join(rootPath, 'plugin.xml');
    const xmlMeta = await readXML(pluginXMLPath);
    return {
      id: name,
      name: fixName(name),
      version: meta.version,
      rootPath: rootPath,
      repository: meta.repository,
      xml: xmlMeta.plugin,
    };
  } catch (e) {
    // ignore
  }
  return null;
}

export function getDependencies(config: Config): string[] {
  return [
    ...Object.keys(config.app.package.dependencies ?? {}),
    ...Object.keys(config.app.package.devDependencies ?? {}),
  ];
}

export function fixName(name: string): string {
  name = name
    .replace(/\//g, '_')
    .replace(/-/g, '_')
    .replace(/@/g, '')
    .replace(/_\w/g, (m) => m[1].toUpperCase());

  return name.charAt(0).toUpperCase() + name.slice(1);
}

export function printPlugins(
  plugins: Plugin[],
  platform: string,
  type: 'capacitor' | 'cordova' | 'incompatible' = 'capacitor',
): void {
  if (plugins.length === 0) {
    return;
  }

  let msg: string;
  const plural = plugins.length === 1 ? '' : 's';

  switch (type) {
    case 'cordova':
      msg = `Found ${plugins.length} Cordova plugin${plural} for ${c.strong(platform)}:\n`;
      break;
    case 'incompatible':
      msg = `Found ${plugins.length} incompatible Cordova plugin${plural} for ${c.strong(
        platform,
      )}, skipped install:\n`;
      break;
    case 'capacitor':
      msg = `Found ${plugins.length} Capacitor plugin${plural} for ${c.strong(platform)}:\n`;
      break;
  }

  msg += plugins.map((p) => `${p.id}${c.weak(`@${p.version}`)}`).join('\n');

  logger.info(msg);
}

export function getPluginPlatform(p: Plugin, platform: string): any {
  const platforms = p.xml.platform;
  if (platforms) {
    const platforms = p.xml.platform.filter(function (item: any) {
      return item.$.name === platform;
    });
    return platforms[0];
  }
  return [];
}

export function getPlatformElement(p: Plugin, platform: string, elementName: string): any {
  const platformTag = getPluginPlatform(p, platform);
  if (platformTag) {
    const element = platformTag[elementName];
    if (element) {
      return element;
    }
  }
  return [];
}

export function getPluginType(p: Plugin, platform: string): PluginType {
  switch (platform) {
    case 'ios':
      return p.ios?.type ?? PluginType.Core;
    case 'android':
      return p.android?.type ?? PluginType.Core;
  }

  return PluginType.Core;
}

/**
 * Get each JavaScript Module for the given plugin
 */
export function getJSModules(p: Plugin, platform: string): any {
  return getAllElements(p, platform, 'js-module');
}

/**
 * Get each asset tag for the given plugin
 */
export function getAssets(p: Plugin, platform: string): any {
  return getAllElements(p, platform, 'asset');
}

export function getFilePath(config: Config, plugin: Plugin, path: string): string {
  if (path.startsWith('node_modules')) {
    let pathSegments = path.split('/').slice(1);
    if (pathSegments[0].startsWith('@')) {
      pathSegments = [pathSegments[0] + '/' + pathSegments[1], ...pathSegments.slice(2)];
    }

    const filePath = resolveNode(config.app.rootDir, ...pathSegments);
    if (!filePath) {
      throw new Error(`Can't resolve module ${pathSegments[0]}`);
    }

    return filePath;
  }
  return join(plugin.rootPath, path);
}

/**
 * For a given plugin, return all the plugin.xml elements with elementName, checking root and specified platform
 */
export function getAllElements(p: Plugin, platform: string, elementName: string): any {
  let modules: string[] = [];
  if (p.xml[elementName]) {
    modules = modules.concat(p.xml[elementName]);
  }
  const platformModules = getPluginPlatform(p, platform);
  if (platformModules?.[elementName]) {
    modules = modules.concat(platformModules[elementName]);
  }
  return modules;
}