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

fix(cli): amplify plugin scan command print correct version of inactive hosting plugins #8130

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { JSONUtilities } from '../../../../amplify-cli-core/lib';
import { PluginInfo } from '../../domain/plugin-info';
import { PluginManifest } from '../../domain/plugin-manifest';
import { PluginPlatform } from '../../domain/plugin-platform';
import { checkPlatformHealth, getOfficialPlugins } from '../../plugin-helpers/platform-health-check';

jest.mock('chalk', () => ({
yellow: jest.fn().mockImplementation(input => input),
}));

const corePackageJson = {
name: '@aws-amplify/cli',
version: '5.4.0',
amplify: {
officialPlugins: {
core: {
name: 'core',
type: 'core',
packageName: '@aws-amplify/cli',
},
api: {
name: 'api',
type: 'category',
packageName: 'amplify-category-api',
},
hosting: [
{
name: 'hosting',
type: 'category',
packageName: 'amplify-category-hosting',
},
{
name: 'hosting',
type: 'category',
packageName: 'amplify-console-hosting',
},
],
codegen: {
name: 'codegen',
type: 'util',
packageName: 'amplify-codegen',
},
},
},
dependencies: {
'amplify-category-api': '2.31.20',
'amplify-category-hosting': '2.7.18',
'amplify-codegen': '^2.23.1',
'amplify-console-hosting': '1.9.9',
'amplify-container-hosting': '1.3.20',
},
};

jest.mock('amplify-cli-core', () => ({
JSONUtilities: {
readJson: jest.fn(),
},
}));

describe('platform-health-check', () => {
beforeAll(() => {
const JSONUtilitiesMock = JSONUtilities as jest.Mocked<typeof JSONUtilities>;
JSONUtilitiesMock.readJson.mockReturnValue(corePackageJson);
});

describe('getOfficialPlugins', () => {
it('returns official plugin descriptions', () => {
expect(getOfficialPlugins()).toEqual({
core: {
name: 'core',
packageName: '@aws-amplify/cli',
packageVersion: '5.4.0',
type: 'core',
},
api: {
name: 'api',
packageName: 'amplify-category-api',
packageVersion: '2.31.20',
type: 'category',
},
codegen: {
name: 'codegen',
packageName: 'amplify-codegen',
packageVersion: '^2.23.1',
type: 'util',
},
hosting: [
{
name: 'hosting',
packageName: 'amplify-category-hosting',
packageVersion: '2.7.18',
type: 'category',
},
{
name: 'hosting',
packageName: 'amplify-console-hosting',
packageVersion: '1.9.9',
type: 'category',
},
],
});
});
});

describe('checkPlatformHealth', () => {
beforeEach(() => {
console.log = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});

it('returns true when every official plugins is active and match plugin description', async () => {
const pluginPlatform: PluginPlatform = new PluginPlatform();
pluginPlatform.plugins = {
core: [new PluginInfo('@aws-amplify/cli', '5.4.0', '', new PluginManifest('core', 'core'))],
hosting: [
new PluginInfo('amplify-category-hosting', '2.7.18', '', new PluginManifest('hosting', 'category')),
new PluginInfo('amplify-console-hosting', '1.9.9', '', new PluginManifest('hosting', 'category')),
],
codegen: [new PluginInfo('amplify-codegen', '2.27.0', '', new PluginManifest('codegen', 'util'))],
api: [new PluginInfo('amplify-category-api', '2.31.20', '', new PluginManifest('api', 'category'))],
};
expect(await checkPlatformHealth(pluginPlatform)).toBe(true);
});

it('returns false when mismatch plugins exists', async () => {
const pluginPlatform: PluginPlatform = new PluginPlatform();
pluginPlatform.plugins = {
core: [new PluginInfo('@aws-amplify/cli', '5.4.0', '', new PluginManifest('core', 'core'))],
hosting: [
new PluginInfo('amplify-category-hosting', '2.7.18', '', new PluginManifest('hosting', 'category')),
new PluginInfo('amplify-console-hosting', '1.9.9', '', new PluginManifest('hosting', 'category')),
],
codegen: [
// version mismatch
new PluginInfo('amplify-codegen', '1.30.0', '', new PluginManifest('codegen', 'util')),
],
api: [new PluginInfo('amplify-category-api', '2.31.20', '', new PluginManifest('api', 'category'))],
};

expect(await checkPlatformHealth(pluginPlatform)).toBe(false);
expect(console.log).toBeCalledWith('The following official plugins have mismatched packages:');
expect(console.log).toBeCalledWith('Expected:');
expect(console.log).toBeCalledWith(' codegen: util | amplify-codegen@^2.23.1');
});

it('returns false when missing or inactive plugins exists', async () => {
const pluginPlatform: PluginPlatform = new PluginPlatform();
pluginPlatform.plugins = {
core: [new PluginInfo('@aws-amplify/cli', '5.4.0', '', new PluginManifest('core', 'core'))],
hosting: [
new PluginInfo('amplify-category-hosting', '2.7.18', '', new PluginManifest('hosting', 'category')),
// missing 'amplify-console-hosting'
],
codegen: [new PluginInfo('amplify-codegen', '2.27.0', '', new PluginManifest('codegen', 'util'))],
// missing 'api'
};

expect(await checkPlatformHealth(pluginPlatform)).toBe(false);
expect(console.log).toBeCalledWith('The following official plugins are missing or inactive:');
expect(console.log).toBeCalledWith(' hosting: category | amplify-console-hosting@1.9.9');
expect(console.log).toBeCalledWith(' api: category | amplify-category-api@2.31.20');
});
});
});
22 changes: 14 additions & 8 deletions packages/amplify-cli/src/plugin-helpers/platform-health-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const indent = ' ';

export async function checkPlatformHealth(pluginPlatform: PluginPlatform): Promise<boolean> {
const activePlugins = pluginPlatform.plugins;
const officialPlugins: { [key: string]: PluginDescription | Array<PluginDescription> } = getOfficialPlugins();
const officialPlugins = getOfficialPlugins();
const missingOfficialPlugins: Array<PluginDescription> = [];
const mismatchedOfficialPlugins: Array<PluginDescription> = [];

Expand Down Expand Up @@ -91,21 +91,27 @@ function isMatching(pluginDescription: PluginDescription, pluginInfo: PluginInfo
return result;
}

export function getOfficialPlugins() {
export function getOfficialPlugins(): { [key: string]: PluginDescription | Array<PluginDescription> } {
const packageJsonFilePath = path.normalize(path.join(__dirname, '../../package.json'));
const packageJson = JSONUtilities.readJson<$TSAny>(packageJsonFilePath);
const { officialPlugins } = packageJson.amplify;

const dependencies: { [key: string]: string } = packageJson.dependencies;

Object.keys(officialPlugins).forEach((plugin: string) => {
const { packageName } = officialPlugins[plugin];
if (dependencies[packageName]) {
const version = dependencies[packageName];
officialPlugins[plugin].packageVersion = version;
} else {
delete officialPlugins[plugin].packageVersion;
let plugins = officialPlugins[plugin];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider using const
const plugins = Array.isArray(officialPlugins[plugin]) ? officialPlugins[plugin] : [officialPlugins[plugin]];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lazpavel, using const looks good.

if (!Array.isArray(plugins)) {
plugins = [plugins];
}
plugins.forEach(officialPlugin => {
const { packageName } = officialPlugin;
if (dependencies[packageName]) {
const version = dependencies[packageName];
officialPlugin.packageVersion = version;
} else {
delete officialPlugin.packageVersion;
}
});
});

const coreVersion = packageJson.version;
Expand Down