Skip to content

Commit

Permalink
Revert "refactor: plugin platform code cleanup (#10384)" (#10403)
Browse files Browse the repository at this point in the history
This reverts commit 7474ba6.
  • Loading branch information
jhockett committed May 12, 2022
1 parent 2612585 commit b2998d7
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 189 deletions.
3 changes: 1 addition & 2 deletions .eslint-dictionary.json
Expand Up @@ -74,7 +74,6 @@
"parens",
"pathname",
"pipelined",
"pluggable",
"positionally",
"postauthorcd",
"promisify",
Expand Down Expand Up @@ -120,4 +119,4 @@
"xcode",
"yaml",
"yyyymmddhhmmss"
]
]
120 changes: 53 additions & 67 deletions packages/amplify-cli/src/plugin-helpers/scan-plugin-platform.ts
@@ -1,24 +1,20 @@
/* eslint-disable no-param-reassign */
import { $TSAny, isPackaged, JSONUtilities } from 'amplify-cli-core';
import * as fs from 'fs-extra';
import * as path from 'path';
import sequential from 'promise-sequential';
import { constants } from '../domain/constants';
import * as fs from 'fs-extra';
import { PluginCollection } from '../domain/plugin-collection';
import { PluginInfo } from '../domain/plugin-info';
import { PluginManifest } from '../domain/plugin-manifest';
import { PluginPlatform } from '../domain/plugin-platform';
import { constants } from '../domain/constants';
import { getGlobalNodeModuleDirPath } from '../utils/global-prefix';
import isChildPath from '../utils/is-child-path';
import { PluginManifest } from '../domain/plugin-manifest';
import { PluginInfo } from '../domain/plugin-info';
import { verifyPlugin } from './verify-plugin';
import { readPluginsJsonFile, writePluginsJsonFile } from './access-plugins-file';
import { twoPluginsAreTheSame } from './compare-plugins';
import { checkPlatformHealth } from './platform-health-check';
import { verifyPlugin } from './verify-plugin';
import isChildPath from '../utils/is-child-path';
import { JSONUtilities, $TSAny, isPackaged } from 'amplify-cli-core';
import sequential from 'promise-sequential';

/**
* scan for platform plugins, including both core and custom plugins
*/
export const scanPluginPlatform = async (pluginPlatform?: PluginPlatform): Promise<PluginPlatform> => {
export async function scanPluginPlatform(pluginPlatform?: PluginPlatform): Promise<PluginPlatform> {
pluginPlatform = pluginPlatform || readPluginsJsonFile() || new PluginPlatform();

pluginPlatform!.plugins = new PluginCollection();
Expand All @@ -33,7 +29,7 @@ export const scanPluginPlatform = async (pluginPlatform?: PluginPlatform): Promi
});

const scanUserLocationTasks = pluginPlatform!.userAddedLocations.map(
pluginDirPath => async () => verifyAndAdd(pluginPlatform!, pluginDirPath),
pluginDirPath => async () => await verifyAndAdd(pluginPlatform!, pluginDirPath),
);
await sequential(scanUserLocationTasks);
}
Expand All @@ -45,18 +41,19 @@ export const scanPluginPlatform = async (pluginPlatform?: PluginPlatform): Promi
if (pluginPlatform!.pluginDirectories.length > 0 && pluginPlatform!.pluginPrefixes.length > 0) {
const scanDirTasks = pluginPlatform!.pluginDirectories.map(directory => async () => {
directory = normalizePluginDirectory(directory);
// adding subDir based on amplify-
const subDirNames = await fs.readdir(directory);
// adding plugin based on @aws-amplify/amplify-
if (subDirNames.includes('@aws-amplify')) {
const nameSpacedDir = path.join(directory, '@aws-amplify');
const nameSpacedPackages = await fs.readdir(nameSpacedDir);
await addPluginPrefixWithMatchingPattern(nameSpacedPackages, nameSpacedDir, pluginPlatform!);
} else {
const exists = await fs.pathExists(directory);
if (exists) {
//adding subDir based on amplify-
const subDirNames = await fs.readdir(directory);
await addPluginPrefixWithMatchingPattern(subDirNames, directory, pluginPlatform!);
//ading plugin based on @aws-amplify/amplify-
if (subDirNames.includes('@aws-amplify')) {
const nameSpacedDir = path.join(directory, '@aws-amplify');
const nameSpacedPackages = await fs.readdir(nameSpacedDir);
await addPluginPrefixWithMatchingPattern(nameSpacedPackages, nameSpacedDir, pluginPlatform!);
}
}
});

await sequential(scanDirTasks);
}

Expand All @@ -66,23 +63,19 @@ export const scanPluginPlatform = async (pluginPlatform?: PluginPlatform): Promi
await checkPlatformHealth(pluginPlatform);

return pluginPlatform;
};
}

/**
* get core plugin directory path
*/
export const getCorePluginDirPath = (): string => path.join(__dirname, '..', '..');
export function getCorePluginDirPath(): string {
return path.normalize(path.join(__dirname, '../../'));
}

/**
* get version from root package.json
*/
export const getCorePluginVersion = (): string => {
const packageJsonFilePath = path.join(__dirname, '..', '..', 'package.json');
export function getCorePluginVersion(): string {
const packageJsonFilePath = path.normalize(path.join(__dirname, '..', '..', 'package.json'));
const packageJson = JSONUtilities.readJson<$TSAny>(packageJsonFilePath);
return packageJson.version;
};
}

const addCore = async (pluginPlatform: PluginPlatform): Promise<void> => {
async function addCore(pluginPlatform: PluginPlatform) {
const corePluginDirPath = getCorePluginDirPath();
const pluginVerificationResult = await verifyPlugin(corePluginDirPath);
if (pluginVerificationResult.verified) {
Expand All @@ -95,42 +88,38 @@ const addCore = async (pluginPlatform: PluginPlatform): Promise<void> => {
} else {
throw new Error('The local Amplify-CLI is corrupted');
}
};
}

/**
* path resolution for various plugin directories
*/
export const normalizePluginDirectory = (directory: string): string => {
export function normalizePluginDirectory(directory: string): string {
switch (directory) {
case constants.PackagedNodeModules:
return path.join(__dirname, '..', '..', '..', '..');
return path.normalize(path.join(__dirname, '../../../..'));
case constants.LocalNodeModules:
return path.join(__dirname, '..', '..', 'node_modules');
return path.normalize(path.join(__dirname, '../../node_modules'));
case constants.ParentDirectory:
return path.join(__dirname, '..', '..', '..');
return path.normalize(path.join(__dirname, '../../../'));
case constants.GlobalNodeModules:
return getGlobalNodeModuleDirPath();
default:
return directory;
}
};
}

const isMatchingNamePattern = (pluginPrefixes: string[], pluginDirName: string): boolean => {
function isMatchingNamePattern(pluginPrefixes: string[], pluginDirName: string): boolean {
if (pluginPrefixes && pluginPrefixes.length > 0) {
return pluginPrefixes.some(prefix => {
const regex = new RegExp(`^${prefix}`);
return regex.test(pluginDirName);
});
}
return true;
};

const verifyAndAdd = async (pluginPlatform: PluginPlatform, pluginDirPath: string): Promise<void> => {
}
async function verifyAndAdd(pluginPlatform: PluginPlatform, pluginDirPath: string) {
const pluginVerificationResult = await verifyPlugin(pluginDirPath);
if (
pluginVerificationResult.verified
pluginVerificationResult.verified &&
// Only the current core is added by the addCore(.) method, other packages can not be core
&& pluginVerificationResult.manifest!.name !== constants.CORE
pluginVerificationResult.manifest!.name !== constants.CORE
) {
const manifest = pluginVerificationResult.manifest as PluginManifest;
const { name, version } = pluginVerificationResult.packageJson;
Expand All @@ -150,37 +139,34 @@ const verifyAndAdd = async (pluginPlatform: PluginPlatform, pluginDirPath: strin
}
}
}
};
}

/**
* return true if the passed in plugin has already been scanned, otherwise return false
*/
export const isUnderScanCoverageSync = (pluginPlatform: PluginPlatform, pluginDirPath: string): boolean => {
export function isUnderScanCoverageSync(pluginPlatform: PluginPlatform, pluginDirPath: string): boolean {
let result = false;
pluginDirPath = path.normalize(pluginDirPath);
const pluginDirName = path.basename(pluginDirPath);

if (fs.existsSync(pluginDirPath) && isMatchingNamePattern(pluginPlatform.pluginPrefixes, pluginDirName)) {
result = pluginPlatform.pluginDirectories.some(directory => {
directory = normalizePluginDirectory(directory);
return fs.existsSync(directory) && isChildPath(pluginDirPath, directory);
if (fs.existsSync(directory) && isChildPath(pluginDirPath, directory)) {
return true;
}
});
}

return result;
};
}

const addPluginPrefixWithMatchingPattern = async (
subDirNames: string[],
directory: string,
pluginPlatform: PluginPlatform,
): Promise<void> => {
const addPluginPrefixWithMatchingPattern = async (subDirNames: string[], directory: string, pluginPlatform: PluginPlatform) => {
if (subDirNames.length > 0) {
const scanSubDirTasks = subDirNames.map(subDirName => async () => {
if (isMatchingNamePattern(pluginPlatform.pluginPrefixes, subDirName)) {
const pluginDirPath = path.join(directory, subDirName);
await verifyAndAdd(pluginPlatform, pluginDirPath);
}
const scanSubDirTasks = subDirNames.map(subDirName => {
return async () => {
if (isMatchingNamePattern(pluginPlatform.pluginPrefixes, subDirName)) {
const pluginDirPath = path.join(directory, subDirName);
await verifyAndAdd(pluginPlatform, pluginDirPath);
}
};
});
await sequential(scanSubDirTasks);
}
Expand Down
73 changes: 43 additions & 30 deletions packages/amplify-cli/src/plugin-helpers/verify-plugin.ts
@@ -1,20 +1,17 @@
import { $TSAny, JSONUtilities } from 'amplify-cli-core';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as fs from 'fs-extra';
import { constants } from '../domain/constants';
import { PluginManifest } from '../domain/plugin-manifest';
import { PluginVerificationError, PluginVerificationResult } from '../domain/plugin-verification-result';
import { PluginVerificationResult, PluginVerificationError } from '../domain/plugin-verification-result';
import { JSONUtilities, $TSAny } from 'amplify-cli-core';

type VerificationContext = {
pluginDirPath: string;
manifest?: PluginManifest;
pluginModule?: $TSAny;
pluginModule?: any;
};

/**
* Verify plugin is configured correctly
*/
export const verifyPlugin = async (pluginDirPath: string): Promise<PluginVerificationResult> => {
export async function verifyPlugin(pluginDirPath: string): Promise<PluginVerificationResult> {
let exists = await fs.pathExists(pluginDirPath);
if (exists) {
const stat = await fs.stat(pluginDirPath);
Expand All @@ -26,20 +23,14 @@ export const verifyPlugin = async (pluginDirPath: string): Promise<PluginVerific
return verifyNodePackage(pluginDirPath);
}
return new PluginVerificationResult(false, PluginVerificationError.PluginDirPathNotExist);
};
}

/**
* PluginNameValidationResult type
*/
export type PluginNameValidationResult = {
isValid: boolean;
message?: string;
};

/**
* Ensure no naming conflict exists between custom plugins and Amplify CLI core plugins
*/
export const validPluginName = async (pluginName: string): Promise<PluginNameValidationResult> => {
export async function validPluginName(pluginName: string): Promise<PluginNameValidationResult> {
const result: PluginNameValidationResult = {
isValid: true,
};
Expand All @@ -49,9 +40,9 @@ export const validPluginName = async (pluginName: string): Promise<PluginNameVal
result.message = 'Amplify CLI core command names can not be used as plugin name';
}
return result;
};
}

const verifyNodePackage = async (pluginDirPath: string): Promise<PluginVerificationResult> => {
async function verifyNodePackage(pluginDirPath: string): Promise<PluginVerificationResult> {
const pluginPackageJsonFilePath = path.join(pluginDirPath, constants.PACKAGEJSON_FILE_NAME);
try {
const packageJson = JSONUtilities.readJson(pluginPackageJsonFilePath);
Expand All @@ -66,9 +57,9 @@ const verifyNodePackage = async (pluginDirPath: string): Promise<PluginVerificat
} catch (err) {
return new PluginVerificationResult(false, PluginVerificationError.InvalidNodePackage, err);
}
};
}

const verifyAmplifyManifest = async (context: VerificationContext): Promise<PluginVerificationResult> => {
async function verifyAmplifyManifest(context: VerificationContext): Promise<PluginVerificationResult> {
const pluginManifestFilePath = path.join(context.pluginDirPath, constants.MANIFEST_FILE_NAME);
let exists = await fs.pathExists(pluginManifestFilePath);
if (exists) {
Expand All @@ -86,32 +77,54 @@ const verifyAmplifyManifest = async (context: VerificationContext): Promise<Plug
if (pluginNameValidationResult.isValid) {
context.manifest = manifest;

const pluginVerificationResult = await verifyEventHandlers(context);
pluginVerificationResult.manifest = manifest;
let result = verifyCommands(context);

return pluginVerificationResult;
result = result.verified ? verifyEventHandlers(context) : result;
result.manifest = manifest;

return result;
}

return new PluginVerificationResult(false, PluginVerificationError.InvalidManifest, pluginNameValidationResult.message);
} catch (err) {
return new PluginVerificationResult(false, PluginVerificationError.InvalidManifest, err);
}
};

const verifyEventHandlers = async (context: VerificationContext): Promise<PluginVerificationResult> => {
}

function verifyCommands(context: VerificationContext): PluginVerificationResult {
// let isVerified = true;
// if (manifest.commands && manifest.commands.length > 0) {
// isVerified = pluginModule.hasOwnProperty(constants.ExecuteAmplifyCommand) &&
// typeof pluginModule[constants.ExecuteAmplifyCommand] === 'function';
// }

// if (isVerified) {
// return new PluginVerificationResult(true);
// }
// return new PluginVerificationResult(
// false,
// PluginVerificationError.MissingExecuteAmplifyCommandMethod,
// );

// verification should be on the plugin type and if it implement all the required METHODS;
return new PluginVerificationResult(true);
}

function verifyEventHandlers(context: VerificationContext): PluginVerificationResult {
let isVerified = true;
if (context.manifest!.eventHandlers && context.manifest!.eventHandlers.length > 0) {
// Lazy load the plugin if not yet loaded
if (!context.pluginModule) {
context.pluginModule = await import(context.pluginDirPath);
context.pluginModule = require(context.pluginDirPath);
}

isVerified = context.pluginModule?.[constants.HandleAmplifyEvent]
&& typeof context.pluginModule[constants.HandleAmplifyEvent] === 'function';
isVerified =
context.pluginModule.hasOwnProperty(constants.HandleAmplifyEvent) &&
typeof context.pluginModule[constants.HandleAmplifyEvent] === 'function';
}

if (isVerified) {
return new PluginVerificationResult(true);
}
return new PluginVerificationResult(false, PluginVerificationError.MissingHandleAmplifyEventMethod);
};
}

0 comments on commit b2998d7

Please sign in to comment.