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

Init configure dx changes #6907

Merged
merged 8 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 72 additions & 1 deletion packages/amplify-cli/src/config-steps/c0-analyzeProject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as path from 'path';
import inquirer, { InputQuestion } from 'inquirer';
import inquirer, { ListQuestion, InputQuestion } from 'inquirer';
import { normalizeEditor, editorSelection } from '../extensions/amplify-helpers/editor-selection';
import { isProjectNameValid, normalizeProjectName } from '../extensions/amplify-helpers/project-name-validation';
import { getEnvInfo } from '../extensions/amplify-helpers/get-env-info';
import { displayConfigurationDefaults } from '../init-steps/s0-analyzeProject';
import { getFrontendPlugins } from '../extensions/amplify-helpers/get-frontend-plugins';
import { isContainersEnabled } from '../execution-manager';
import { stateManager } from 'amplify-cli-core';

export async function analyzeProject(context) {
Expand All @@ -15,12 +18,80 @@ export async function analyzeProject(context) {
const projectPath = process.cwd();
Object.assign(context.exeInfo.localEnvInfo, { projectPath });

let { projectName } = context.exeInfo.projectConfig;
const { defaultEditor, envName } = context.exeInfo.localEnvInfo;

context.print.info('');
await displayConfigurationDefaults(context, projectName, envName, defaultEditor);

const frontendPlugins = getFrontendPlugins(context);
let frontend = context.exeInfo.projectConfig.frontend;
if (!frontend) {
frontend = 'javascript';
}
const frontendModule = require(frontendPlugins[frontend]);
await frontendModule.displayFrontendDefaults(context, projectPath);
context.print.info('');

const envAwsInfo = stateManager.getLocalAWSInfo();
if (typeof envAwsInfo?.[envName] === 'object') {
const awsInfo = envAwsInfo[envName];
if (awsInfo.useProfile && awsInfo.profileName) {
await displayProfileSetting(context, awsInfo['profileName']);
context.print.info('');
}
}

await displayContainersInfo(context);
context.print.info('');

const configurationSetting = await getConfigurationSetting();
if (configurationSetting !== 'project') {
context.exeInfo.inputParams.yes = true;
if (configurationSetting === 'containers') {
context.exeInfo.inputParams.containerSetting = true;
} else if (configurationSetting === 'profile') {
context.exeInfo.inputParams.profileSetting = true;
}
} else {
context.exeInfo.inputParams.containerSetting = true;
context.exeInfo.inputParams.profileSetting = true;
}

await configureProjectName(context);
await configureEditor(context);

return context;
}

function displayProfileSetting(context, profileName) {
context.print.info('AWS Profile setting');
context.print.info(`| Selected profile: ${profileName}`);
}

function displayContainersInfo(context) {
context.print.info('Advanced: Container-based deployments');
const containerDeploymentStatus = isContainersEnabled(context) ? 'Yes' : 'No';
context.print.info(`| Leverage container-based deployments: ${containerDeploymentStatus}`);
}

async function getConfigurationSetting() {
const configureSettingQuestion: ListQuestion = {
type: 'list',
name: 'configurationSetting',
message: 'Which setting do you want to configure?',
choices: [
{ name: 'Project information', value: 'project' },
{ name: 'AWS Profile setting', value: 'profile' },
{ name: 'Advanced: Container-based deployments', value: 'containers' },
],
default: 'project',
};

const { configurationSetting } = await inquirer.prompt(configureSettingQuestion);
return configurationSetting;
}

async function configureProjectName(context) {
let { projectName } = context.exeInfo.projectConfig;
if (context.exeInfo.inputParams.amplify && context.exeInfo.inputParams.amplify.projectName) {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/src/execution-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function executeCommand(context: Context) {
}
}

function isContainersEnabled(context) {
export function isContainersEnabled(context) {
const projectConfig = context.amplify.getProjectConfig();
return projectConfig?.[projectConfig.frontend]?.config?.ServerlessContainers ?? false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as inquirer from 'inquirer';
import { JSONUtilities } from 'amplify-cli-core';
import { merge } from 'lodash';

const editors = [
export const editors = [
{
name: 'Visual Studio Code',
value: 'vscode',
Expand Down
80 changes: 72 additions & 8 deletions packages/amplify-cli/src/init-steps/s0-analyzeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as path from 'path';
import * as fs from 'fs-extra';
import * as inquirer from 'inquirer';
import { InvalidEnvironmentNameError, stateManager, exitOnNextTick, $TSContext } from 'amplify-cli-core';
import { normalizeEditor, editorSelection } from '../extensions/amplify-helpers/editor-selection';
import { normalizeEditor, editorSelection, editors } from '../extensions/amplify-helpers/editor-selection';
import { getFrontendPlugins } from '../extensions/amplify-helpers/get-frontend-plugins';
import { getSuitableFrontend } from './s1-initFrontend';
import { isProjectNameValid, normalizeProjectName } from '../extensions/amplify-helpers/project-name-validation';
import { amplifyCLIConstants } from '../extensions/amplify-helpers/constants';
import { print } from '../context-extensions';

export async function analyzeProjectHeadless(context: $TSContext) {
const projectPath = process.cwd();
Expand All @@ -23,13 +26,63 @@ export async function analyzeProjectHeadless(context: $TSContext) {
}
}

export function displayConfigurationDefaults(context, defaultProjectName, defaultEnv, defaultEditorName) {
print.info('Project information');
print.info(`| Name: ${defaultProjectName}`);
print.info(`| Environment: ${defaultEnv}`);
print.info(`| Default editor: ${defaultEditorName}`);
}

function setConfigurationDefaults(context, projectPath, defaultProjectName, defaultEnv, defaultEditor) {
setExeInfo(context, projectPath, defaultEditor, defaultEnv);
setProjectConfig(context, defaultProjectName);
context.exeInfo.inputParams.amplify = {};
context.exeInfo.inputParams.amplify.projectName = defaultProjectName;
context.exeInfo.inputParams.amplify.envName = defaultEnv;
context.exeInfo.inputParams.amplify.defaultEditor = defaultEditor;
}

async function displayAndSetDefaults(context, projectPath, projectName) {
const defaultProjectName = projectName;
const defaultEnv = getDefaultEnv(context);
let defaultEditor;
if (context?.exeInfo?.inputParams?.amplify?.defaultEditor) {
defaultEditor = normalizeEditor(context.exeInfo.inputParams.amplify.defaultEditor);
} else {
defaultEditor = editors.length > 0 ? editors[0].value : 'vscode';
}
const editorIndex = editors.findIndex(editorEntry => editorEntry.value === defaultEditor);
const defaultEditorName = editorIndex > -1 ? editors[editorIndex].name : 'Visual Studio Code';

print.success('The following configuration will be applied:');
print.info('');

await displayConfigurationDefaults(context, defaultProjectName, defaultEnv, defaultEditorName);

const frontendPlugins = getFrontendPlugins(context);
const defaultFrontend = getSuitableFrontend(frontendPlugins, projectPath);
const frontendModule = require(frontendPlugins[defaultFrontend]);

await frontendModule.displayFrontendDefaults(context, projectPath);
print.info('');

if (context.exeInfo.inputParams.yes || (await context.amplify.confirmPrompt('Initialize the project with the above configuration?'))) {
await setConfigurationDefaults(context, projectPath, defaultProjectName, defaultEnv, defaultEditorName);
await frontendModule.setFrontendDefaults(context, projectPath);
}
}

export async function analyzeProject(context): Promise<$TSContext> {
if (!context.parameters.options.app || !context.parameters.options.quickstart) {
context.print.warning('Note: It is recommended to run this command from the root of your app directory');
}
const projectPath = process.cwd();
context.exeInfo.isNewProject = isNewProject(context);
const projectName = await getProjectName(context);
if (context.parameters.command !== 'env') {
await displayAndSetDefaults(context, projectPath, projectName);
}

const envName = await getEnvName(context);

let defaultEditor = getDefaultEditor();
Expand Down Expand Up @@ -129,8 +182,25 @@ async function getEditor(context) {
}
/* End getEditor */

const isEnvNameValid = inputEnvName => {
return /^[a-z]{2,10}$/.test(inputEnvName);
};

const INVALID_ENV_NAME_MSG = 'Environment name must be between 2 and 10 characters, and lowercase only.';

function getDefaultEnv(context): string | undefined {
const defaultEnv = 'dev';
let defaultEnv = 'dev';

if (context?.exeInfo?.inputParams?.amplify?.envName) {
if (isEnvNameValid(context.exeInfo.inputParams.amplify.envName)) {
defaultEnv = context.exeInfo.inputParams.amplify.envName;
return defaultEnv;
}
context.print.error(INVALID_ENV_NAME_MSG);
context.usageData.emitError(new InvalidEnvironmentNameError(INVALID_ENV_NAME_MSG));
exitOnNextTick(1);
}

if (isNewProject(context) || !context.amplify.getAllEnvs().includes(defaultEnv)) {
return defaultEnv;
}
Expand All @@ -140,12 +210,6 @@ function getDefaultEnv(context): string | undefined {
async function getEnvName(context) {
let envName;

const isEnvNameValid = inputEnvName => {
return /^[a-z]{2,10}$/.test(inputEnvName);
};

const INVALID_ENV_NAME_MSG = 'Environment name must be between 2 and 10 characters, and lowercase only.';

if (context.exeInfo.inputParams.amplify && context.exeInfo.inputParams.amplify.envName) {
if (isEnvNameValid(context.exeInfo.inputParams.amplify.envName)) {
({ envName } = context.exeInfo.inputParams.amplify);
Expand Down
22 changes: 13 additions & 9 deletions packages/amplify-cli/src/init-steps/s1-initFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ export async function initFrontend(context) {
}

const frontendPlugins = getFrontendPlugins(context);
const suitableFrontend = getSuitableFrontend(frontendPlugins, context.exeInfo.localEnvInfo.projectPath);
const frontend = await getFrontendHandler(context, frontendPlugins, suitableFrontend);

context.exeInfo.projectConfig.frontend = frontend;
const frontendModule = require(frontendPlugins[frontend]);
await frontendModule.init(context);

return context;
}

export function getSuitableFrontend(frontendPlugins, projectPath) {
let suitableFrontend;
let fitToHandleScore = -1;

Object.keys(frontendPlugins).forEach(key => {
const { scanProject } = require(frontendPlugins[key]);
const newScore = scanProject(context.exeInfo.localEnvInfo.projectPath);
const newScore = scanProject(projectPath);
if (newScore > fitToHandleScore) {
fitToHandleScore = newScore;
suitableFrontend = key;
}
});

const frontend = await getFrontendHandler(context, frontendPlugins, suitableFrontend);

context.exeInfo.projectConfig.frontend = frontend;
const frontendModule = require(frontendPlugins[frontend]);
await frontendModule.init(context);

return context;
return suitableFrontend;
}

async function getFrontendHandler(context, frontendPlugins, suitableFrontend) {
Expand Down
10 changes: 10 additions & 0 deletions packages/amplify-e2e-core/src/init/initProjectHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function initJSProjectWithProfile(cwd: string, settings: Object): Promise
spawn(getCLIPath(), ['init'], { cwd, stripColors: true, env })
.wait('Enter a name for the project')
.sendLine(s.name)
.wait('Initialize the project with the above configuration?')
.sendLine('n')
.wait('Enter a name for the environment')
.sendLine(s.envName)
.wait('Choose your default editor:')
Expand Down Expand Up @@ -96,6 +98,8 @@ export function initAndroidProjectWithProfile(cwd: string, settings: Object): Pr
})
.wait('Enter a name for the project')
.sendLine(s.name)
.wait('Initialize the project with the above configuration?')
.sendLine('n')
.wait('Enter a name for the environment')
.sendLine(s.envName)
.wait('Choose your default editor:')
Expand Down Expand Up @@ -137,6 +141,8 @@ export function initIosProjectWithProfile(cwd: string, settings: Object): Promis
})
.wait('Enter a name for the project')
.sendLine(s.name)
.wait('Initialize the project with the above configuration?')
.sendLine('n')
.wait('Enter a name for the environment')
.sendLine(s.envName)
.wait('Choose your default editor:')
Expand Down Expand Up @@ -170,6 +176,8 @@ export function initFlutterProjectWithProfile(cwd: string, settings: Object): Pr
let chain = spawn(getCLIPath(), ['init'], { cwd, stripColors: true })
.wait('Enter a name for the project')
.sendLine(s.name)
.wait('Initialize the project with the above configuration?')
.sendLine('n')
.wait('Enter a name for the environment')
.sendLine(s.envName)
.wait('Choose your default editor:')
Expand Down Expand Up @@ -215,6 +223,8 @@ export function initProjectWithAccessKey(
})
.wait('Enter a name for the project')
.sendLine(s.name)
.wait('Initialize the project with the above configuration?')
.sendLine('n')
.wait('Enter a name for the environment')
.sendLine(s.envName)
.wait('Choose your default editor:')
Expand Down
10 changes: 10 additions & 0 deletions packages/amplify-frontend-android/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ function run(context) {
return context;
}

function displayFrontendDefaults(context) {
configManager.displayFrontendDefaults(context);
}

function setFrontendDefaults(context) {
configManager.setFrontendDefaults(context);
}

async function executeAmplifyCommand(context) {
let commandPath = path.normalize(path.join(__dirname, 'commands'));
if (context.input.command === 'help') {
Expand All @@ -65,6 +73,8 @@ module.exports = {
publish,
run,
createFrontendConfigs,
displayFrontendDefaults,
setFrontendDefaults,
executeAmplifyCommand,
handleAmplifyEvent,
deleteConfig: deleteAmplifyConfig,
Expand Down
17 changes: 16 additions & 1 deletion packages/amplify-frontend-android/lib/configuration-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ function normalizeInputParams(context) {
context.exeInfo.inputParams[constants.Label] = inputParams;
}

export function displayFrontendDefaults(context) {
context.print.info(`| App type: android`);
context.print.info(`| Res directory: ${constants.defaultResDir}`);
}

export function setFrontendDefaults(context) {
context.exeInfo.inputParams.amplify.frontend = constants.Label;

let inputParams = {};
context.exeInfo.inputParams[constants.Label] = inputParams;
inputParams.config = {};
inputParams.config.ResDir = constants.defaultResDir;
context.exeInfo.inputParams[constants.Label] = inputParams;
}

async function confirmConfiguration(context) {
if (!context.exeInfo.projectConfig[constants.Label]) {
context.exeInfo.projectConfig[constants.Label] = {};
Expand All @@ -50,7 +65,7 @@ async function confirmConfiguration(context) {
type: 'input',
name: 'ResDir',
message: 'Where is your Res directory: ',
default: config.ResDir || 'app/src/main/res',
default: config.ResDir || constants.defaultResDir,
},
];
const answers = await inquirer.prompt(configurationSettings);
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-frontend-android/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
Label: 'android',
defaultResDir: 'app/src/main/res',
ProjectScanBaseScore: 0,
ProjectScanMaxScore: 100,
awsConfigFilename: 'awsconfiguration.json',
Expand Down
10 changes: 10 additions & 0 deletions packages/amplify-frontend-flutter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ function run(context) {
return context;
}

function displayFrontendDefaults(context, projectPath) {
configManager.displayFrontendDefaults(context);
}

function setFrontendDefaults(context, projectPath) {
configManager.setFrontendDefaults(context);
}

async function executeAmplifyCommand(context) {
let commandPath = path.normalize(path.join(__dirname, 'commands'));
if (context.input.command === 'help') {
Expand Down Expand Up @@ -74,6 +82,8 @@ module.exports = {
configure,
publish,
run,
displayFrontendDefaults,
setFrontendDefaults,
createFrontendConfigs,
executeAmplifyCommand,
handleAmplifyEvent,
Expand Down
Loading