Skip to content
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
6 changes: 3 additions & 3 deletions packages/amplify-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { rewireDeprecatedCommands } from './rewireDeprecatedCommands';
EventEmitter.defaultMaxListeners = 1000;

// entry from commandline
export async function run(): Promise<number> {
export async function run() {
try {
let pluginPlatform = await getPluginPlatform();
let input = getCommandLineInput(pluginPlatform);
Expand Down Expand Up @@ -68,12 +68,12 @@ export async function run(): Promise<number> {
if (e.stack) {
print.info(e.stack);
}
return 1;
process.exit(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we bubble up return code and handle process exit at the top level? is this the best solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually the top most level, the amplify in the bin directly calls this run method.

}
}

// entry from library call
export async function execute(input: Input) {
export async function execute(input: Input): Promise<number> {
try {
let pluginPlatform = await getPluginPlatform();
let verificationResult = verifyInput(pluginPlatform, input);
Expand Down
10 changes: 7 additions & 3 deletions packages/amplify-console-hosting/utils/build-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function zipFile(sourceDir, destFilePath) {
resolve(zipFilePath);
});

archive.on('error', (err) => {
archive.on('error', err => {
reject(err);
});
archive.pipe(output);
Expand All @@ -29,14 +29,18 @@ function zipFile(sourceDir, destFilePath) {
}

function run(command, projectDirectory) {
if (!command) {
throw new Error('Missing build command');
}

return new Promise((resolve, reject) => {
let args = command.split(/\s+/);
const cmd = args[0];
args = args.slice(1);
const execution = spawn(cmd, args, { cwd: projectDirectory, env: process.env, stdio: 'inherit' });

let rejectFlag = false;
execution.on('exit', (code) => {
execution.on('exit', code => {
if (code === 0) {
resolve();
} else if (!rejectFlag) {
Expand All @@ -45,7 +49,7 @@ function run(command, projectDirectory) {
}
});

execution.on('error', (err) => {
execution.on('error', err => {
console.log(chalk.red('command execution teminated with error'));
if (!rejectFlag) {
rejectFlag = true;
Expand Down
53 changes: 51 additions & 2 deletions packages/amplify-e2e-core/src/categories/hosting.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { nspawn as spawn, getCLIPath } from '../../src';
import fs from 'fs-extra';
import path from 'path';
import { nspawn as spawn, getCLIPath, createNewProjectDir, KEY_DOWN_ARROW, readJsonFile } from '../../src';
import { spawnSync } from 'child_process';

export function addHosting(cwd: string) {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['add', 'hosting'], { cwd, stripColors: true })
.wait('Select the plugin module to execute')
.sendLine('j')
.send(KEY_DOWN_ARROW)
.sendCarriageReturn()
.wait('Select the environment setup:')
.sendCarriageReturn()
Expand All @@ -24,6 +27,33 @@ export function addHosting(cwd: string) {
});
}

export function amplifyPushWithUpdate(cwd: string) {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['push'], { cwd, stripColors: true })
.wait('Are you sure you want to continue?')
.sendCarriageReturn()
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}

export function amplifyPublishWithoutUpdate(cwd: string) {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['publish'], { cwd, stripColors: true }).run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}

export function removeHosting(cwd: string) {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['remove', 'hosting'], { cwd, stripColors: true })
Expand All @@ -41,3 +71,22 @@ export function removeHosting(cwd: string) {
});
});
}

export async function createReactTestProject(): Promise<string> {
const projRoot = await createNewProjectDir('hosting');
const projectName = path.basename(projRoot);
const projectDir = path.dirname(projRoot);

spawnSync('npx', ['create-react-app', projectName], { cwd: projectDir });

return projRoot;
}

export function resetBuildCommand(projectDir: string, newBuildCommand: string): string {
const projectConfigFilePath = path.join(projectDir, 'amplify', '.config', 'project-config.json');
const projectConfig = readJsonFile(projectConfigFilePath);
const currentBuildCommand = projectConfig.javascript.config.BuildCommand;
projectConfig.javascript.config.BuildCommand = newBuildCommand;
fs.writeFileSync(projectConfigFilePath, JSON.stringify(projectConfig, null, 4));
return currentBuildCommand;
}
2 changes: 1 addition & 1 deletion packages/amplify-e2e-core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from './appsync';
export * from './nexpect';
export * from './pinpoint';
export * from './projectMeta';
export * from './read-json-file';
export * from './readJsonFile';
export * from './request';
export * from './retrier';
export * from './sdk-calls';
Expand Down
53 changes: 44 additions & 9 deletions packages/amplify-e2e-tests/src/__tests__/hosting.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
import {
amplifyPublishWithoutUpdate,
createReactTestProject,
resetBuildCommand,
} from 'amplify-e2e-core';

import { initJSProjectWithProfile, deleteProject } from 'amplify-e2e-core';
import { addHosting, removeHosting, amplifyPushWithoutCodegen } from 'amplify-e2e-core';
import { createNewProjectDir, deleteProjectDir, getProjectMeta } from 'amplify-e2e-core';
import { deleteProjectDir, getProjectMeta } from 'amplify-e2e-core';
import * as fs from 'fs-extra';
import * as path from 'path';

describe('amplify add hosting', () => {
let projRoot: string;
beforeEach(async () => {
projRoot = await createNewProjectDir('hosting');

beforeAll(async () => {
projRoot = await createReactTestProject();
await initJSProjectWithProfile(projRoot, {});
await addHosting(projRoot);
await amplifyPushWithoutCodegen(projRoot);
});

afterEach(async () => {
afterAll(async () => {
await removeHosting(projRoot);
await amplifyPushWithoutCodegen(projRoot);
await deleteProject(projRoot, true);
deleteProjectDir(projRoot);
});

it('add hosting', async () => {
await initJSProjectWithProfile(projRoot, {});
await addHosting(projRoot);
await amplifyPushWithoutCodegen(projRoot);
beforeEach(async () => {});

afterEach(async () => {});

it('add hosting and push creates correct amplify artifacts', async () => {
expect(fs.existsSync(path.join(projRoot, 'amplify', 'backend', 'hosting', 'S3AndCloudFront'))).toBe(true);
const projectMeta = getProjectMeta(projRoot);
expect(projectMeta.hosting).toBeDefined();
expect(projectMeta.hosting.S3AndCloudFront).toBeDefined();
});
});

it('publish', async () => {
let error;
try {
await amplifyPublishWithoutUpdate(projRoot);
} catch (err) {
error = err;
}
expect(error).not.toBeDefined();
});

it('publish throws error if build command is missing', async () => {
const currentBuildCommand = resetBuildCommand(projRoot, '');
let error;
try {
await amplifyPublishWithoutUpdate(projRoot);
} catch (err) {
error = err;
}
expect(error).toBeDefined();
expect(error.message).toEqual('Process exited with non zero exit code 1');
resetBuildCommand(projRoot, currentBuildCommand);
});

})
5 changes: 5 additions & 0 deletions packages/amplify-frontend-javascript/lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ function run(context) {
return new Promise((resolve, reject) => {
const { projectConfig } = context.exeInfo;
const buildCommand = projectConfig[constants.Label].config.BuildCommand;

if (!buildCommand) {
throw new Error('Missing build command');
}

let args = buildCommand.split(/\s+/);
const command = normalizeCommand(args[0]);
args = args.slice(1);
Expand Down
5 changes: 5 additions & 0 deletions packages/amplify-frontend-javascript/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ function run(context) {
return new Promise((resolve, reject) => {
const { projectConfig } = context.exeInfo;
const startCommand = projectConfig[constants.Label].config.StartCommand;

if (!startCommand) {
throw new Error('Missing start command');
}

let args = startCommand.split(/\s+/);
const command = args[0];
args = args.slice(1);
Expand Down