Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): locate plugins by allowlist #3762

Merged
merged 25 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async function removePluginsNativeFiles(config: Config) {

async function getPluginsTask(config: Config) {
return await runTask('Updating Android plugins', async () => {
const allPlugins = await getPlugins(config);
const allPlugins = await getPlugins(config, 'android');
const androidPlugins = await getAndroidPlugins(allPlugins);
return androidPlugins;
});
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export async function getCordovaPlugins(
config: Config,
platform: string,
): Promise<Plugin[]> {
const allPlugins = await getPlugins(config);
const allPlugins = await getPlugins(config, platform);
let plugins: Plugin[] = [];
if (platform === config.ios.name) {
plugins = await getIOSPlugins(allPlugins);
Expand Down
29 changes: 29 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ export interface CapacitorConfig {
* @default false
*/
hideLogs?: boolean;

/**
* Allowlist of plugins to include during `npx cap sync` for Android.
*
* Overrides global `includePlugins` option.
*
* @since 3.0.0
*/
includePlugins?: string[];
};

ios?: {
Expand Down Expand Up @@ -264,6 +273,15 @@ export interface CapacitorConfig {
* @default false
*/
hideLogs?: boolean;

/**
* Allowlist of plugins to include during `npx cap sync` for iOS.
*
* Overrides global `includePlugins` option.
*
* @since 3.0.0
*/
includePlugins?: string[];
};

server?: {
Expand Down Expand Up @@ -353,4 +371,15 @@ export interface CapacitorConfig {
};

plugins?: { [key: string]: any };

/**
* Allowlist of plugins to include during `npx cap sync`.
*
* This should be an array of strings representing the npm package name of
* plugins to include when running `npx cap sync`. If unset, Capacitor will
* inspect `package.json` for a list of potential plugins.
*
* @since 3.0.0
*/
includePlugins?: string[];
}
2 changes: 1 addition & 1 deletion cli/src/ios/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ function removeNoSystem(library: string, sourceFrameworks: string[]) {

async function getPluginsTask(config: Config) {
return await runTask('Updating iOS plugins', async () => {
const allPlugins = await getPlugins(config);
const allPlugins = await getPlugins(config, 'ios');
const iosPlugins = await getIOSPlugins(allPlugins);
return iosPlugins;
});
Expand Down
36 changes: 28 additions & 8 deletions cli/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,31 @@ export interface Plugin {
};
}

export async function getPlugins(config: Config): Promise<Plugin[]> {
const deps = getDependencies(config);
const plugins = await Promise.all(
deps.map(async p => resolvePlugin(config, p)),
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 plugins.filter(p => !!p) as Plugin[];

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

export async function resolvePlugin(
Expand Down Expand Up @@ -93,9 +112,10 @@ export async function resolvePlugin(
}

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

export function fixName(name: string): string {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tasks/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function listCommand(
}

export async function list(config: Config, platform: string): Promise<void> {
const allPlugins = await getPlugins(config);
const allPlugins = await getPlugins(config, platform);
let plugins: Plugin[] = [];
if (platform === config.ios.name) {
plugins = await getIOSPlugins(allPlugins);
Expand Down