Skip to content

Commit

Permalink
feat(cli): cLI updates and new features for Amplify Console (#2742)
Browse files Browse the repository at this point in the history
* feat(cli): cLI updates and new features for Amplify Console

* change amplify console command

* dev

* add migraiton check in attach workflow

* dev

* dev

* allow pulling different env

* update message printout

* region map

* update aws-sdk version

* yarn

* handle unsupported region of Amplify service

* handle unsupported regions of Amplify service

* yarn

* dev

* address PR review comments

* text changes to address PR review comments
  • Loading branch information
UnleashedMind committed Nov 23, 2019
1 parent 0393535 commit 0fd0dd5
Show file tree
Hide file tree
Showing 55 changed files with 1,485 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ packages/*/package-lock.json
**/.env
amplify-integ*/
packages/amplify-storage-simulator/lib
packages/**/reports/junit/*
packages/**/reports/junit/*
2 changes: 1 addition & 1 deletion packages/amplify-appsync-simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@types/node": "^8.10.51",
"@types/pino": "4.7.1",
"amplify-graphiql-explorer": "0.23.0",
"aws-sdk": "^2.510.0",
"aws-sdk": "^2.577.0",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"tslint": "^5.18.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function getTemplateMappings(context) {
};
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = require(providerPlugins[providerName]);
const regionMapping = provider.getPinpointRegionMapping(context);
const regionMapping = provider.getPinpointRegionMapping();
Object.keys(regionMapping).forEach(region => {
Mappings.RegionMapping[region] = {
pinpointRegion: regionMapping[region],
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-category-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"amplify-category-auth": "1.30.0",
"amplify-category-function": "1.30.0",
"amplify-category-storage": "1.31.0",
"aws-sdk": "^2.510.0",
"aws-sdk": "^2.577.0",
"fs-extra": "^8.1.0",
"graphql": "^0.13.2",
"graphql-relational-schema-transformer": "1.24.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-category-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"amplify-category-function": "1.30.0",
"aws-sdk": "^2.510.0",
"aws-sdk": "^2.577.0",
"chalk": "^2.4.2",
"chalk-pipe": "^2.0.0",
"enquirer": "^2.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Richard Threlkeld",
"license": "Apache-2.0",
"dependencies": {
"aws-sdk": "^2.94.0",
"aws-sdk": "^2.577.0",
"aws-serverless-express": "^3.3.5",
"body-parser": "^1.17.2",
"express": "^4.15.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"dependencies": {
"aws-sdk": "^2.49.0",
"aws-sdk": "^2.577.0",
"aws-serverless-express": "^3.3.5",
"body-parser": "^1.17.1",
"express": "^4.15.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async function writeData(context) {
writeBackendConfig(context, pinpointMeta, context.amplify.pathManager.getBackendConfigFilePath());
writeBackendConfig(context, pinpointMeta, context.amplify.pathManager.getCurrentBackendConfigFilePath());
writeAmplifyMeta(context, categoryMeta, context.amplify.pathManager.getAmplifyMetaFilePath());
writeAmplifyMeta(context, categoryMeta, context.amplify.pathManager.getCurentAmplifyMetaFilePath());
writeAmplifyMeta(context, categoryMeta, context.amplify.pathManager.getCurrentAmplifyMetaFilePath());
await context.amplify.onCategoryOutputsChange(context);
}

Expand Down
49 changes: 25 additions & 24 deletions packages/amplify-cli/amplify-plugin.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
{
"name": "core",
"type": "core",
"commands": [
"categories",
"configure",
"console",
"delete",
"env",
"help",
"init",
"migrate",
"publish",
"push",
"run",
"status",
"plugin",
"version"
],
"commandAliases":{
"h": "help",
"serve": "run",
"ls": "status"
}
}
"name": "core",
"type": "core",
"commands": [
"categories",
"configure",
"console",
"delete",
"env",
"help",
"init",
"migrate",
"publish",
"push",
"pull",
"run",
"status",
"plugin",
"version"
],
"commandAliases": {
"h": "help",
"serve": "run",
"ls": "status"
}
}
1 change: 1 addition & 0 deletions packages/amplify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"lodash": "^4.17.15",
"mime-types": "^2.1.24",
"moment": "^2.24.0",
"open": "^6.4.0",
"optionator": "^0.8.2",
"ora": "^3.4.0",
"promise-sequential": "^1.1.1",
Expand Down
47 changes: 34 additions & 13 deletions packages/amplify-cli/src/commands/console.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
const inquirer = require('inquirer');
const fs = require('fs-extra');
const open = require('open');

const providerName = 'awscloudformation';

module.exports = {
name: 'console',
run: async context => {
const categoryPlugins = context.amplify.getCategoryPlugins(context);
const providerPlugins = context.amplify.getProviderPlugins(context);
let consoleUrl = getDefaultURL();

const combinedPlugins = { ...categoryPlugins, ...providerPlugins };
try {
const localEnvInfoFilePath = context.amplify.pathManager.getLocalEnvFilePath();
if (fs.existsSync(localEnvInfoFilePath)) {
const teamProviderInfoFilePath = context.amplify.pathManager.getProviderInfoFilePath();
if (fs.existsSync(teamProviderInfoFilePath)) {
const localEnvInfo = context.amplify.readJsonFile(localEnvInfoFilePath);
const teamProviderInfo = context.amplify.readJsonFile(teamProviderInfoFilePath);
const { envName } = localEnvInfo;
const { AmplifyAppId } = teamProviderInfo[envName][providerName];

const answer = await inquirer.prompt({
type: 'list',
name: 'pluginKey',
message: 'Please select the category or provider',
choices: Object.keys(combinedPlugins),
default: Object.keys(providerPlugins)[0],
});
if (envName && AmplifyAppId) {
consoleUrl = constructStatusURL(AmplifyAppId, envName);
}
}
}
} catch (e) {
context.print.error(e.message);
}

const plugin = require(combinedPlugins[answer.pluginKey]);
await plugin.console(context);
context.print.green(consoleUrl);
open(consoleUrl, { wait: false });
},
};

function constructStatusURL(appId, envName) {
const prodURL = `https://console.aws.amazon.com/amplify/home#/${appId}/YmFja2VuZA/${envName}`; // eslint-disable-line
return prodURL;
}

function getDefaultURL() {
const prodURL = `https://console.aws.amazon.com/amplify/home#/create`; // eslint-disable-line
return prodURL;
}
4 changes: 2 additions & 2 deletions packages/amplify-cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const initFrontendHandler = require('../lib/init-steps/s1-initFrontend');
const initProviders = require('../lib/init-steps/s2-initProviders');
const onFailure = require('../lib/init-steps/s9-onFailure');
const onSuccess = require('../lib/init-steps/s9-onSuccess');
const { normalizeInputParams } = require('../lib/input-params-manager');
const { constructInputParams } = require('../lib/amplify-service-helper');

module.exports = {
name: 'init',
Expand All @@ -24,6 +24,6 @@ module.exports = {

function constructExeInfo(context) {
context.exeInfo = {
inputParams: normalizeInputParams(context),
inputParams: constructInputParams(context),
};
}
48 changes: 48 additions & 0 deletions packages/amplify-cli/src/commands/pull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const fs = require('fs-extra');

const { pullBackend } = require('../lib/pull-backend');
const { attachBackend } = require('../lib/attach-backend');
const { constructInputParams } = require('../lib/amplify-service-helper');
const { run: envCheckout } = require('./env/checkout');

module.exports = {
name: 'pull',
run: async context => {
const inputParams = constructInputParams(context);

const currentAmplifyMetaFilePath = context.amplify.pathManager.getCurrentAmplifyMetaFilePath(process.cwd());
if (fs.existsSync(currentAmplifyMetaFilePath)) {
const { appId: inputAppId, envName: inputEnvName } = inputParams.amplify;

const teamProviderInfoFilePath = context.amplify.pathManager.getProviderInfoFilePath(process.cwd());
const localEnvInfoFilePath = context.amplify.pathManager.getLocalEnvFilePath(process.cwd());
const teamProviderInfo = context.amplify.readJsonFile(teamProviderInfoFilePath);
const { envName } = context.amplify.readJsonFile(localEnvInfoFilePath);
const { AmplifyAppId } = teamProviderInfo[envName].awscloudformation;
const localEnvNames = Object.keys(teamProviderInfo);

if (inputAppId && AmplifyAppId && inputAppId !== AmplifyAppId) {
context.print.error('Amplify appId mismatch.');
context.print.info(`You are currently working in the amplify project with Id ${AmplifyAppId}`);
process.exit(1);
}

if (inputEnvName) {
if (inputEnvName === envName) {
await pullBackend(context, inputParams);
} else if (localEnvNames.includes(inputEnvName)) {
context.parameters.options = {};
context.parameters.first = inputEnvName;
await envCheckout(context);
} else {
inputParams.amplify.appId = inputAppId;
await attachBackend(context, inputParams);
}
} else {
await pullBackend(context, inputParams);
}
} else {
await attachBackend(context, inputParams);
}
},
};
9 changes: 9 additions & 0 deletions packages/amplify-cli/src/commands/push.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const { initializeEnv } = require('../lib/initialize-env');

module.exports = {
name: 'push',
run: async context => {
try {
context.amplify.constructExeInfo(context);
context.exeInfo.forcePush = false;
context.exeInfo.restoreBackend = false;
// The following line of code pulls the latest backend to #current-cloud-backend
// so the amplify status is correctly shown to the user before the user confirms
// to push his local developments
await initializeEnv(context);

await context.amplify.pushResources(context);
} catch (e) {
if (e.name !== 'InvalidDirectiveError') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sequential = require('promise-sequential');

async function onCategoryOutputsChange(context, cloudAmplifyMeta, localMeta) {
if (!cloudAmplifyMeta) {
const currentAmplifyMetafilePath = context.amplify.pathManager.getCurentAmplifyMetaFilePath();
const currentAmplifyMetafilePath = context.amplify.pathManager.getCurrentAmplifyMetaFilePath();
if (fs.existsSync(currentAmplifyMetafilePath)) {
cloudAmplifyMeta = readJsonFile(currentAmplifyMetafilePath);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function getAmplifyMetaFilePath(projectPath) {
return path.normalize(path.join(getBackendDirPath(projectPath), amplifyCLIConstants.amplifyMetaFileName));
}

function getCurentAmplifyMetaFilePath(projectPath) {
function getCurrentAmplifyMetaFilePath(projectPath) {
return path.normalize(path.join(getCurrentCloudBackendDirPath(projectPath), amplifyCLIConstants.amplifyMetaFileName));
}

Expand All @@ -138,7 +138,7 @@ module.exports = {
getPluginConfigFilePath,
getAmplifyMetaFilePath,
getGitIgnoreFilePath,
getCurentAmplifyMetaFilePath,
getCurrentAmplifyMetaFilePath,
getLocalEnvFilePath,
getProviderInfoFilePath,
getBackendConfigFilePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function pushResources(context, category, resourceName, filteredResources)
if (continueToPush) {
try {
// Get current-cloud-backend's amplify-meta
const currentAmplifyMetaFilePath = context.amplify.pathManager.getCurentAmplifyMetaFilePath();
const currentAmplifyMetaFilePath = context.amplify.pathManager.getCurrentAmplifyMetaFilePath();
const currentAmplifyMeta = readJsonFile(currentAmplifyMetaFilePath);

await providersPush(context, category, resourceName, filteredResources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async function getResourceStatus(category, resourceName, providerName, filteredR
if (amplifyProjectInitStatus === CLOUD_INITIALIZED) {
const amplifyMetaFilePath = pathManager.getAmplifyMetaFilePath();
amplifyMeta = readJsonFile(amplifyMetaFilePath);
const currentamplifyMetaFilePath = pathManager.getCurentAmplifyMetaFilePath();
const currentamplifyMetaFilePath = pathManager.getCurrentAmplifyMetaFilePath();
currentamplifyMeta = readJsonFile(currentamplifyMetaFilePath);
} else if (amplifyProjectInitStatus === CLOUD_NOT_INITIALIZED) {
const backendConfigFilePath = pathManager.getBackendConfigFilePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function updateAwsMetaFile(filePath, category, resourceName, attribute, value, t

function moveBackendResourcesToCurrentCloudBackend(resources) {
const amplifyMetaFilePath = pathManager.getAmplifyMetaFilePath();
const amplifyCloudMetaFilePath = pathManager.getCurentAmplifyMetaFilePath();
const amplifyCloudMetaFilePath = pathManager.getCurrentAmplifyMetaFilePath();
const backendConfigFilePath = pathManager.getBackendConfigFilePath();
const backendConfigCloudFilePath = pathManager.getCurrentBackendConfigFilePath();

Expand Down Expand Up @@ -104,7 +104,7 @@ function updateProvideramplifyMeta(providerName, options) {

function updateamplifyMetaAfterResourceUpdate(category, resourceName, attribute, value) {
const amplifyMetaFilePath = pathManager.getAmplifyMetaFilePath();
// let amplifyCloudMetaFilePath = pathManager.getCurentAmplifyMetaFilePath();
// let amplifyCloudMetaFilePath = pathManager.getCurrentAmplifyMetaFilePath();
const currentTimestamp = new Date();
if (attribute === 'dependsOn') {
checkForCyclicDependencies(category, resourceName, value);
Expand Down Expand Up @@ -174,7 +174,7 @@ function updateAmplifyMetaAfterPackage(resource, zipFilename) {
}

function updateamplifyMetaAfterResourceDelete(category, resourceName) {
const amplifyMetaFilePath = pathManager.getCurentAmplifyMetaFilePath();
const amplifyMetaFilePath = pathManager.getCurrentAmplifyMetaFilePath();
const amplifyMeta = readJsonFile(amplifyMetaFilePath);

const resourceDir = path.normalize(path.join(pathManager.getCurrentCloudBackendDirPath(), category, resourceName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { readJsonFile } = require('./read-json-file');

function updateBackendConfigAfterResourceAdd(category, resourceName, options) {
const backendConfigFilePath = pathManager.getBackendConfigFilePath();
const backendConfig = readJsonFile(backendConfigFilePath);
const backendConfig = getExistingBackendConfig(backendConfigFilePath);

if (!backendConfig[category]) {
backendConfig[category] = {};
}
Expand All @@ -18,7 +19,7 @@ function updateBackendConfigAfterResourceAdd(category, resourceName, options) {

function updateBackendConfigDependsOn(category, resourceName, attribute, value) {
const backendConfigFilePath = pathManager.getBackendConfigFilePath();
const backendConfig = readJsonFile(backendConfigFilePath);
const backendConfig = getExistingBackendConfig(backendConfigFilePath);

if (!backendConfig[category]) {
backendConfig[category] = {};
Expand All @@ -35,7 +36,7 @@ function updateBackendConfigDependsOn(category, resourceName, attribute, value)

function updateBackendConfigAfterResourceRemove(category, resourceName) {
const backendConfigFilePath = pathManager.getBackendConfigFilePath();
const backendConfig = readJsonFile(backendConfigFilePath);
const backendConfig = getExistingBackendConfig(backendConfigFilePath);

if (backendConfig[category] && backendConfig[category][resourceName] !== undefined) {
delete backendConfig[category][resourceName];
Expand All @@ -45,6 +46,14 @@ function updateBackendConfigAfterResourceRemove(category, resourceName) {
fs.writeFileSync(backendConfigFilePath, jsonString, 'utf8');
}

function getExistingBackendConfig(backendConfigFilePath) {
let backendConfig = {};
if (fs.existsSync(backendConfigFilePath)) {
backendConfig = readJsonFile(backendConfigFilePath);
}
return backendConfig;
}

module.exports = {
updateBackendConfigAfterResourceAdd,
updateBackendConfigAfterResourceRemove,
Expand Down

0 comments on commit 0fd0dd5

Please sign in to comment.