Skip to content

Commit

Permalink
feat: headless Init and configure (#371)
Browse files Browse the repository at this point in the history
* init headless

* add headless for javascript frontend

* setup headless for android frontend

* move publish ignore to a file inside hosting folder

* move publish ignore out of amplifyrc file
  • Loading branch information
UnleashedMind authored and kaustavghosh06 committed Nov 6, 2018
1 parent 7dd7259 commit 8780400
Show file tree
Hide file tree
Showing 50 changed files with 1,139 additions and 679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function serviceWalkthrough(
const { inputs } = serviceMetadata;
const { amplify } = context;
const { parseInputs } = require(`${__dirname}/../question-factories/core-questions.js`);
const projectType = Object.keys(amplify.getProjectConfig().frontendHandler)[0];
const projectType = amplify.getProjectConfig().frontend;


let coreAnswers = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"operator": "includes"
},
{
"key": "frontendHandler",
"key": "frontend",
"value": "ios",
"operator": "configMatch"
}
Expand All @@ -160,7 +160,7 @@
"operator": "includes"
},
{
"key": "frontendHandler",
"key": "frontend",
"value": "android",
"operator": "configMatch"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function invalidate(context) {
}

async function getCloudFrontClient(context) {
const { projectConfig } = context.exeInfo;
const provider = require(projectConfig.providers[providerName]);
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = require(providerPlugins[providerName]);
const aws = await provider.getConfiguredAWSClient(context);
return new aws.CloudFront();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ const inquirer = require('inquirer');
const minimatch = require('minimatch');
const fs = require('fs-extra');

const { PublishIgnoreRCLabel } = require('../../constants');
const PublishIgnoreFileName = 'amplifyPublishIgnore.json';

async function configure(context) {
const amplifyRCFilePath = context.amplify.pathManager.getAmplifyRcFilePath();
let amplifyRC;
let publishIgnore;
const publishIgnoreFilePath = getPublishIgnoreFilePath(context);
let publishIgnore = [];

if (fs.existsSync(amplifyRCFilePath)) {
amplifyRC = JSON.parse(fs.readFileSync(amplifyRCFilePath, 'utf8'));
publishIgnore = amplifyRC[PublishIgnoreRCLabel];
if (fs.existsSync(publishIgnoreFilePath)) {
try {
publishIgnore = require(publishIgnoreFilePath);
} catch (e) {
publishIgnore = [];
}
}

amplifyRC = amplifyRC || {};
publishIgnore = publishIgnore || [];

publishIgnore = publishIgnore
.map(ignore => ignore.trim())
.filter(ignore => ignore.length > 0)
Expand All @@ -27,12 +26,20 @@ async function configure(context) {
context.print.info('Use glob patterns as in the .gitignore file.');

publishIgnore = await configurePublishIgnore(context, publishIgnore);
amplifyRC[PublishIgnoreRCLabel] = publishIgnore;

const jsonString = JSON.stringify(amplifyRC, null, 4);
fs.writeFileSync(amplifyRCFilePath, jsonString, 'utf8');
const jsonString = JSON.stringify(publishIgnore, null, 4);
fs.writeFileSync(publishIgnoreFilePath, jsonString, 'utf8');
}

function getPublishIgnoreFilePath(context) {
const projectPath = context.amplify.pathManager.searchProjectRootPath();
if (projectPath || fs.existsSync(projectPath)) {
return path.join(projectPath, PublishIgnoreFileName);
}
throw new Error('You are not working inside a valid amplify project.\nUse \'amplify init\' in the root of your app directory to initialize your project with Amplify');
}


async function configurePublishIgnore(context, publishIgnore) {
const DONE = "I'm done.";
const configActions = ['list', 'add', 'remove', 'remove all', DONE];
Expand Down Expand Up @@ -115,13 +122,18 @@ async function removeIgnore(context, publishIgnore) {
}

function getIgnore(context) {
let result;
const amplifyRCFilePath = context.amplify.pathManager.getAmplifyRcFilePath();
if (fs.existsSync(amplifyRCFilePath)) {
const amplifyRC = JSON.parse(fs.readFileSync(amplifyRCFilePath, 'utf8'));
result = amplifyRC[PublishIgnoreRCLabel];
let publishIgnore;
const publishIgnoreFilePath = getPublishIgnoreFilePath(context);
if (fs.existsSync(publishIgnoreFilePath)) {
try {
publishIgnore = require(publishIgnoreFilePath);
} catch (e) {
publishIgnore = [];
}
} else {
publishIgnore = [];
}
return result;
return publishIgnore;
}

function isIgnored(filePath, publishIgnore, ignoreRoot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async function run(context, distributionDirPath) {
}

async function getS3Client(context) {
const { projectConfig } = context.exeInfo;
const provider = require(projectConfig.providers[providerName]);
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = require(providerPlugins[providerName]);
const aws = await provider.getConfiguredAWSClient(context);
return new aws.S3();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-category-hosting/lib/category-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function getAvailableServices(context) {
const availableServices = [];
const projectConfig = context.amplify.getProjectConfig();
Object.keys(supportedServices).forEach((service) => {
if (Object.keys(projectConfig.providers).includes(supportedServices[service].provider)) {
if (projectConfig.providers.includes(supportedServices[service].provider)) {
availableServices.push(service);
}
});
Expand Down
1 change: 0 additions & 1 deletion packages/amplify-category-hosting/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = {
CategoryName: 'hosting',
PublishIgnoreRCLabel: 'publish-ignore',
};
4 changes: 2 additions & 2 deletions packages/amplify-category-notifications/lib/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ async function checkAuth(context) {
}

async function getIamClient(context) {
const { projectConfig } = context.exeInfo;
const provider = require(projectConfig.providers[providerName]);
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = require(providerPlugins[providerName]);
const aws = await provider.getConfiguredAWSClient(context);
return new aws.IAM();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ function console(context) {
}

async function getPinpointClient(context) {
const { projectConfig } = context.exeInfo;
const provider = require(projectConfig.providers[providerName]);
const providerPlugins = context.amplify.getProviderPlugins(context);
const provider = require(providerPlugins[providerName]);
const aws = await provider.getConfiguredAWSClient(context);
aws.config.update({ region: 'us-east-1' });
return new aws.Pinpoint();
Expand Down
10 changes: 9 additions & 1 deletion packages/amplify-cli/src/commands/configure.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const analyzeProject = require('../lib/config-steps/c0-analyzeProject');
const configFrontendHandler = require('../lib/config-steps/c1-configFrontendHandler');
const configFrontendHandler = require('../lib/config-steps/c1-configFrontend');
const configProviders = require('../lib/config-steps/c2-configProviders');
const configureNewUser = require('../lib/configure-new-user');
const onFailure = require('../lib/config-steps/c9-onFailure');
const onSuccess = require('../lib/config-steps/c9-onSuccess');
const { normalizeInputParams } = require('../lib/input-params-manager');

module.exports = {
name: 'configure',
Expand All @@ -13,6 +14,7 @@ module.exports = {
}

if (context.parameters.first === 'project') {
constructExeInfo(context);
return analyzeProject.run(context)
.then(configFrontendHandler.run)
.then(configProviders.run)
Expand All @@ -22,3 +24,9 @@ module.exports = {
},
};

function constructExeInfo(context) {
context.exeInfo = {
inputParams: normalizeInputParams(context),
};
}

9 changes: 8 additions & 1 deletion packages/amplify-cli/src/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const analyzeProject = require('../lib/init-steps/s0-analyzeProject');
const initFrontendHandler = require('../lib/init-steps/s1-initFrontendHandler');
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');

module.exports = {
name: 'init',
run: async (context) => {
constructExeInfo(context);
analyzeProject.run(context)
.then(initFrontendHandler.run)
.then(initProviders.run)
Expand All @@ -15,3 +17,8 @@ module.exports = {
},
};

function constructExeInfo(context) {
context.exeInfo = {
inputParams: normalizeInputParams(context),
};
}
7 changes: 4 additions & 3 deletions packages/amplify-cli/src/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ module.exports = {

await context.amplify.pushResources(context);

const frontendHandler =
require(Object.values(context.exeInfo.projectConfig.frontendHandler)[0]);
frontendHandler.publish(context);
const frontendPlugins = context.amplify.getFrontendPlugins(context);
const frontendHandlerModule =
require(frontendPlugins[context.exeInfo.projectConfig.frontend]);
frontendHandlerModule.publish(context);
},
};
8 changes: 5 additions & 3 deletions packages/amplify-cli/src/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module.exports = {
run: async (context) => {
context.exeInfo = context.amplify.getProjectDetails();
await context.amplify.pushResources(context);
const frontendHandler =
require(Object.values(context.exeInfo.projectConfig.frontendHandler)[0]);
frontendHandler.run(context);

const frontendPlugins = context.amplify.getFrontendPlugins(context);
const frontendHandlerModule =
require(frontendPlugins[context.exeInfo.projectConfig.frontend]);
frontendHandlerModule.run(context);
},
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ora = require('ora');
const { getProjectConfig } = require('./get-project-config');
const { getCategoryPlugins } = require('./get-category-plugins');
const { getProviderPlugins } = require('./get-provider-plugins');
const pathManager = require('./path-manager');

const spinner = ora('Deleting resources from the cloud. This may take a few minutes...');
Expand All @@ -10,10 +11,11 @@ async function deleteProject(context) {
.then((answer) => {
if (answer) {
const { providers } = getProjectConfig();
const providerPlugins = getProviderPlugins(context);
const providerPromises = [];

Object.keys(providers).forEach((providerName) => {
const pluginModule = require(providers[providerName]);
providers.forEach((providerName) => {
const pluginModule = require(providerPlugins[providerName]);
providerPromises.push(pluginModule.deleteProject(context));
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
const inquirer = require('inquirer');

async function editorSelection(defaultEditor) {
const editors = [
{
name: 'Sublime Text',
value: 'sublime',
},
{
name: 'Visual Studio Code',
value: 'code',
},
{
name: 'Atom Editor',
value: 'atom',
},
{
name: 'IDEA 14 CE',
value: 'idea14ce',
},
{
name: 'Vim (via Terminal, Mac OS only)',
value: 'vim',
},
{
name: 'Emacs (via Terminal, Mac OS only)',
value: 'emacs',
},
{
name: 'None',
value: 'none',
},

];

const editors = [
{
name: 'Sublime Text',
value: 'sublime',
},
{
name: 'Visual Studio Code',
value: 'code',
},
{
name: 'Atom Editor',
value: 'atom',
},
{
name: 'IDEA 14 CE',
value: 'idea14ce',
},
{
name: 'Vim (via Terminal, Mac OS only)',
value: 'vim',
},
{
name: 'Emacs (via Terminal, Mac OS only)',
value: 'emacs',
},
{
name: 'None',
value: 'none',
},
];

async function editorSelection(defaultEditor) {
defaultEditor = editors.findIndex(editor => editor.value === defaultEditor) > -1 ?
defaultEditor : undefined;

Expand All @@ -50,6 +48,17 @@ async function editorSelection(defaultEditor) {
return editorSelected;
}

function normalizeEditorCode(editorCode) {
if (editorCode) {
editorCode = editorCode.toLowerCase();
editorCode = editors.findIndex(editor => editor.value === editorCode) > -1 ?
editorCode : undefined;
}
return editorCode;
}

module.exports = {
editors,
normalizeEditorCode,
editorSelection,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { getProjectConfig } = require('./get-project-config');
const { getProviderPlugins } = require('./get-provider-plugins');

async function executeProviderUtils(context, providerName, utilName, options) {
const { providers } = getProjectConfig();
const pluginModule = require(providers[providerName]);
const providerPlugins = getProviderPlugins(context);
const pluginModule = require(providerPlugins[providerName]);
return await pluginModule.providerUtils[utilName](context, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { getResourceOutputs } = require('./get-resource-outputs');
function onCategoryOutputsChange(context) {
const projectConfigFilePath = pathManager.getProjectConfigFilePath();
const projectConfig = JSON.parse(fs.readFileSync(projectConfigFilePath));
if (projectConfig.frontendHandler) {
const frontendHandler = require(Object.values(projectConfig.frontendHandler)[0]);
frontendHandler.createFrontendConfigs(context, getResourceOutputs());
if (projectConfig.frontend) {
const frontendPlugins = context.amplify.getFrontendPlugins(context);
const frontendHandlerModule =
require(frontendPlugins[projectConfig.frontend]);
frontendHandlerModule.createFrontendConfigs(context, getResourceOutputs());
}
}

Expand Down

0 comments on commit 8780400

Please sign in to comment.