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: hosting language updates #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions packages/amplify-category-hosting/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jest.mock('../lib/category-manager');
jest.mock('amplify-hosting-service');

const inquirer = require('inquirer');
const mockirer = require('mockirer');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jest.mock('promise-sequential');
jest.mock('fs-extra');
jest.mock('amplify-hosting-service');
jest.mock('../../lib/S3AndCloudFront/index');
const s3AndCFIndexModule = require('../../lib/S3AndCloudFront/index');

Expand Down
83 changes: 38 additions & 45 deletions packages/amplify-category-hosting/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
const inquirer = require('inquirer');
const sequential = require('promise-sequential');
const categoryManager = require('./lib/category-manager');

const category = 'hosting';

async function add(context) {
const {
availableServices,
disabledServices,
enabledServices,
} = categoryManager.getCategoryStatus(context);

if (availableServices.length > 0) {
if (disabledServices.length > 1) {
const answers = await inquirer.prompt({
type: 'checkbox',
name: 'selectedServices',
message: 'Please select the service(s) to add.',
choices: disabledServices,
default: disabledServices[0],
});
const tasks = [];
answers.selectedServices.forEach((service) => {
tasks.push(() => categoryManager.runServiceAction(context, service, 'enable'));
});
return sequential(tasks);
} else if (disabledServices.length === 1) {
return categoryManager.runServiceAction(context, disabledServices[0], 'enable');
const answers = await inquirer.prompt({
type: 'list',
name: 'selectedService',
message: 'Please select the service to add.',
choices: availableServices,
default: availableServices[0],
});
const { selectedService } = answers;

if (enabledServices.findIndex(service => service.value === selectedService) !== -1) {
context.print.error(`Hosting is already enabled for ${selectedService}`);
return;
}
const errorMessage = 'Hosting is already fully enabled.';
context.print.error(errorMessage);
throw new Error(errorMessage);
} else {
const errorMessage = 'Hosting is not available from enabled providers.';
context.print.error(errorMessage);
throw new Error(errorMessage);
return categoryManager.runServiceAction(context, selectedService, 'enable');
}
const errorMessage = 'Hosting is not available from enabled providers.';
context.print.error(errorMessage);
throw new Error(errorMessage);
}

async function configure(context) {
Expand All @@ -45,18 +38,16 @@ async function configure(context) {

if (availableServices.length > 0) {
if (enabledServices.length > 1) {
const answers = await inquirer.prompt({
type: 'checkbox',
name: 'selectedServices',
message: 'Please select the service(s) to configure.',
const serviceSelection = await inquirer.prompt({
type: 'list',
name: 'selectedService',
message: 'Please select the service to configure.',
choices: enabledServices,
default: enabledServices[0],
});
const tasks = [];
answers.selectedServices.forEach((service) => {
tasks.push(() => categoryManager.runServiceAction(context, service, 'configure'));
});
return sequential(tasks);

const service = serviceSelection.selectedService;
return categoryManager.runServiceAction(context, service, 'configure');
} else if (enabledServices.length === 1) {
return categoryManager.runServiceAction(context, enabledServices[0], 'configure');
}
Expand All @@ -66,19 +57,11 @@ async function configure(context) {
}
}

function publish(context, service, args) {
const {
enabledServices,
} = categoryManager.getCategoryStatus(context);

if (enabledServices.length > 0) {
if (enabledServices.includes(service)) {
return categoryManager.runServiceAction(context, service, 'publish', args);
}
throw new Error(`Hosting service ${service} is NOT enabled.`);
} else {
throw new Error('No hosting service is enabled.');
async function publish(context, selectedHostingService, args) {
if (selectedHostingService) {
return categoryManager.runServiceAction(context, selectedHostingService, 'publish', args);
}
throw new Error('No hosting service is enabled.');
}

async function console(context) {
Expand Down Expand Up @@ -125,11 +108,21 @@ async function getPermissionPolicies(context, resourceOpsMapping) {
return { permissionPolicies, resourceAttributes };
}

async function getEnabledServices(context) {
const {
enabledServices,
} = categoryManager.getCategoryStatus(context);

return enabledServices;
}


module.exports = {
add,
configure,
publish,
console,
migrate,
getPermissionPolicies,
getEnabledServices,
};
61 changes: 38 additions & 23 deletions packages/amplify-category-hosting/lib/category-manager.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
const fs = require('fs-extra');
const path = require('path');
const sequential = require('promise-sequential');
const constants = require('./constants');
const supportedServices = require('./supported-services');
const addAmplifyHosting = require('amplify-hosting-service').add;
const publishAmplifyHosting = require('amplify-hosting-service').publish;
const configureAmplifyHosting = require('amplify-hosting-service').configure;
const consoleAmplifyHosting = require('amplify-hosting-service').console;

const category = 'hosting';

function getAvailableServices(context) {
const availableServices = [];
const projectConfig = context.amplify.getProjectConfig();

Object.keys(supportedServices).forEach((service) => {
if (projectConfig.providers.includes(supportedServices[service].provider)) {
availableServices.push(service);
if (projectConfig.providers.includes(supportedServices[service].provider) ||
supportedServices[service].provider === 'NONE') {
availableServices.push({ name: supportedServices[service].description, value: service });
}
});

return availableServices;
}

function getCategoryStatus(context) {
const enabledServices = [];
const disabledServices = [];
let disabledServices = [];

const availableServices = getAvailableServices(context);
if (availableServices.length > 0) {
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();
const categoryDirPath = path.join(projectBackendDirPath, constants.CategoryName);
if (fs.existsSync(categoryDirPath)) {
const serviceDirnames = fs.readdirSync(categoryDirPath);
for (let i = 0; i < serviceDirnames.length; i++) {
const serviceDirPath = path.join(categoryDirPath, serviceDirnames[i]);
const stat = fs.lstatSync(serviceDirPath);
if (stat.isDirectory()) {
if (availableServices.includes(serviceDirnames[i])) {
enabledServices.push(serviceDirnames[i]);
}
const amplifyMeta = context.amplify.getProjectMeta();
if (amplifyMeta.hosting) {
const servicesAdded = Object.keys(amplifyMeta.hosting);
availableServices.forEach((availableService) => {
if (servicesAdded.includes(availableService.value)) {
enabledServices.push(availableService);
} else {
disabledServices.push(availableService);
}
}
});
} else {
disabledServices = availableServices;
}
availableServices.forEach((service) => {
if (!enabledServices.includes(service)) {
disabledServices.push(service);
}
});
}

return {
Expand All @@ -52,15 +52,30 @@ function getCategoryStatus(context) {
}

function runServiceAction(context, service, action, args) {
if (service !== null && typeof service === 'object') {
service = service.value;
}

context.exeInfo = context.amplify.getProjectDetails();
if (context.exeInfo.amplifyMeta) {
context.exeInfo.categoryMeta = context.exeInfo.amplifyMeta[constants.CategoryName];
if (context.exeInfo.categoryMeta) {
context.exeInfo.serviceMeta = context.exeInfo.categoryMeta[service];
}
}
const serviceModule = require(path.join(__dirname, `${service}/index.js`));
return serviceModule[action](context, args);

if (service === 'AmplifyConsole') {
switch (action) {
case 'enable': return addAmplifyHosting(context);
case 'publish': return publishAmplifyHosting(context);
case 'configure': return configureAmplifyHosting(context);
case 'console': return consoleAmplifyHosting(context);
default: context.print.error('Action not supported');
}
} else {
const serviceModule = require(path.join(__dirname, `${service}/index.js`));
return serviceModule[action](context, args);
}
}

async function migrate(context) {
Expand Down
5 changes: 5 additions & 0 deletions packages/amplify-category-hosting/lib/supported-services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = {
AmplifyConsole: {
provider: 'NONE',
description: 'Hosting with Amplify Console (Managed Hosting, easy custom domain setup)',
},
S3AndCloudFront: {
provider: 'awscloudformation',
description: 'Amazon CloudFront and S3',
},
};
1 change: 1 addition & 0 deletions packages/amplify-category-hosting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"chalk": "^2.4.2",
"amplify-hosting-service": "1.0.0",
"fs-extra": "^8.1.0",
"inquirer": "^6.5.1",
"mime-types": "^2.1.24",
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"amplify-category-auth": "1.13.0",
"amplify-category-function": "1.13.0",
"amplify-category-hosting": "1.8.0",
"amplify-hosting-service": "1.0.0",
"amplify-category-interactions": "1.12.0",
"amplify-category-notifications": "1.10.0",
"amplify-category-predictions": "1.6.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ora = require('ora');
const pathManager = require('./path-manager');
const { removeEnvFromCloud } = require('./remove-env-from-cloud');
const removeAmplifyHosting = require('amplify-hosting-service').remove;

async function deleteProject(context) {
if (await context.amplify.confirmPrompt.run('Are you sure you want to continue? (This would delete all the environments of the project from the cloud and wipe out all the local amplify resource files)')) {
Expand All @@ -12,6 +13,8 @@ async function deleteProject(context) {
const spinner = ora('Deleting resources from the cloud. This may take a few minutes...');
spinner.start();
await Promise.all(removeEnvPromises);
// Remove Amplify Console stack
await removeAmplifyHosting(context);
spinner.succeed('Project deleted in the cloud');
// Remove amplify dir
context.filesystem.remove(pathManager.getAmplifyDirPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
updateBackendConfigAfterResourceRemove,
} = require('./update-backend-config');
const { removeResourceParameters } = require('./envResourceParams');
const removeAmplifyHosting = require('amplify-hosting-service').remove;


async function forceRemoveResource(context, category, name, dir) {
Expand Down Expand Up @@ -52,6 +53,10 @@ function removeResource(context, category) {
return inquirer.prompt(question)
.then((answer) => {
const resourceName = answer.resource;

if (category === 'hosting' && resourceName === 'AmplifyConsole') {
return removeAmplifyHosting(context);
}
const resourceDir = path.normalize(path.join(
pathManager.getBackendDirPath(),
category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function getResourcesToBeCreated(
if ((!amplifyMeta[categoryName][resource].lastPushTimeStamp ||
!currentamplifyMeta[categoryName] ||
!currentamplifyMeta[categoryName][resource]) &&
categoryName !== 'providers') {
categoryName !== 'providers' && !amplifyMeta[categoryName][resource].status) {
amplifyMeta[categoryName][resource].resourceName = resource;
amplifyMeta[categoryName][resource].category = categoryName;
resources.push(amplifyMeta[categoryName][resource]);
Expand Down Expand Up @@ -174,7 +174,8 @@ function getResourcesToBeDeleted(
Object.keys((currentamplifyMeta)).forEach((categoryName) => {
const categoryItem = currentamplifyMeta[categoryName];
Object.keys((categoryItem)).forEach((resource) => {
if (!amplifyMeta[categoryName] || !amplifyMeta[categoryName][resource]) {
if ((!amplifyMeta[categoryName] || !amplifyMeta[categoryName][resource])
&& (!currentamplifyMeta[categoryName][resource].status)) {
currentamplifyMeta[categoryName][resource].resourceName = resource;
currentamplifyMeta[categoryName][resource].category = categoryName;

Expand Down Expand Up @@ -214,7 +215,8 @@ async function getResourcesToBeUpdated(
await asyncForEach(Object.keys((categoryItem)), async (resource) => {
if (currentamplifyMeta[categoryName]) {
if (currentamplifyMeta[categoryName][resource] !== undefined &&
amplifyMeta[categoryName][resource] !== undefined) {
amplifyMeta[categoryName][resource] !== undefined
&& !currentamplifyMeta[categoryName][resource].status) {
const backendModified = await isBackendDirModifiedSinceLastPush(
resource,
categoryName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getProjectConfig } = require('./get-project-config');
const { getResourceStatus } = require('./resource-status');
const { getProviderPlugins } = require('./get-provider-plugins');
const showAmplifyHostingLinks = require('amplify-hosting-service').status;

async function showHelpfulProviderLinks(context) {
const { providers } = getProjectConfig();
Expand All @@ -11,6 +12,15 @@ async function showHelpfulProviderLinks(context) {
allResources,
} = await getResourceStatus();

// Show Amplify Console URLs
try {
if (allResources.findIndex(resource => resource.category === 'hosting' && resource.service === 'AmplifyConsole') !== -1) {
await showAmplifyHostingLinks(context);
}
} catch (e) {
console.log(e.stack);
}

providers.forEach((providerName) => {
const pluginModule = require(providerPlugins[providerName]);
providerPromises.push(pluginModule.showHelpfulLinks(context, allResources));
Expand Down
Loading