Skip to content

Commit

Permalink
feat: add --dry-run option to load user's function but not start a se…
Browse files Browse the repository at this point in the history
…rver (#118)

* feat: add --dry-run option to load user's function but not start a server

* Change the --dry-run log message.
  • Loading branch information
hdp617 authored Jan 30, 2020
1 parent f2544de commit 9e148c2
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const FLAG = {
TARGET: 'target',
SIGNATURE_TYPE: 'signature-type', // dash
SOURCE: 'source',
DRY_RUN: 'dry-run',
};

// Supported environment variables
Expand All @@ -51,14 +52,15 @@ const ENV = {
TARGET: 'FUNCTION_TARGET',
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
SOURCE: 'FUNCTION_SOURCE',
DRY_RUN: 'DRY_RUN',
};

enum NodeEnv {
PRODUCTION = 'production',
}

const argv = minimist(process.argv, {
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE, FLAG.DRY_RUN],
});

const CODE_LOCATION = resolve(
Expand All @@ -77,6 +79,7 @@ if (SIGNATURE_TYPE === undefined) {
console.error(`Function signature type must be one of 'http' or 'event'.`);
process.exit(1);
}
const DRY_RUN = argv[FLAG.DRY_RUN] || process.env[ENV.DRY_RUN] || false;

// CLI Help Flag
if (process.argv[2] === '-h' || process.argv[2] === '--help') {
Expand All @@ -97,6 +100,14 @@ if (!USER_FUNCTION) {

const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!);
const ERROR_HANDLER = new ErrorHandler(SERVER);

if (DRY_RUN) {
console.log(`Function: ${TARGET}`);
console.log(`URL: http://localhost:${PORT}/`);
console.log('Dry run successful, shutting down.');
process.exit(0);
}

SERVER.listen(PORT, () => {
ERROR_HANDLER.register();
if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) {
Expand Down

0 comments on commit 9e148c2

Please sign in to comment.