Skip to content

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

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

Merged
merged 2 commits into from
Jan 30, 2020
Merged
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
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