UNPKG

3.43 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/**
4 * Module dependencies.
5 */
6
7var program = require('commander');
8var fs = require('fs');
9var path = require('path');
10var pkg = JSON.parse(
11 fs.readFileSync(path.join(__dirname, '..', 'package.json')),
12);
13var build = require('../lib/build');
14var serve = require('../lib/serve');
15var install = require('../lib/install');
16
17program.version(pkg.version);
18
19const stringBooleanToBoolean = (val) => {
20 console.log({ val });
21 if (typeof val !== 'string' && (val !== 'true' || val !== 'false')) {
22 throw Error(`Incorrect string value: ${val}`);
23 }
24
25 return val === 'true';
26};
27
28program
29 .option('-c --config <webpack-config>', 'additional webpack configuration')
30 .option('-p --port <port>', 'port to serve from (default: 9000)')
31 .option(
32 '-b --babelrc <babelrc>',
33 'use .babelrc in root (default: true)',
34 stringBooleanToBoolean,
35 )
36 .option(
37 '-t --timeout <timeout>',
38 'function invocation timeout in seconds (default: 10)',
39 )
40 .option('-s --static', 'serve pre-built lambda files');
41
42program
43 .command('serve <dir>')
44 .description('serve and watch functions')
45 .action(function (cmd) {
46 console.log('netlify-lambda: Starting server');
47 var static = Boolean(program.static);
48 var server;
49 var startServer = async function () {
50 server = await serve.listen(
51 program.port || 9000,
52 static,
53 Number(program.timeout) || 10,
54 );
55 };
56 if (static) {
57 startServer();
58 return; // early terminate, don't build
59 }
60 const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
61 build.watch(
62 cmd,
63 { userWebpackConfig, useBabelrc },
64 async function (err, stats) {
65 if (err) {
66 console.error(err);
67 return;
68 }
69 console.log(stats.toString(stats.compilation.options.stats));
70 if (!server) {
71 await startServer();
72 }
73 stats.compilation.chunks.forEach(function (chunk) {
74 server.clearCache(chunk.name || chunk.id.toString());
75 });
76 },
77 );
78 });
79
80program
81 .command('build <dir>')
82 .description('build functions')
83 .action(function (cmd) {
84 console.log('netlify-lambda: Building functions');
85
86 const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
87 build
88 .run(cmd, { userWebpackConfig, useBabelrc })
89 .then(function (stats) {
90 console.log(stats.toString(stats.compilation.options.stats));
91 })
92 .catch(function (err) {
93 console.error(err);
94 process.exit(1);
95 });
96 });
97
98program
99 .command('install [dir]')
100 .description('install functions')
101 .action(function (cmd) {
102 console.log('netlify-lambda: installing function dependencies');
103 install.run(cmd).catch(function (err) {
104 console.error(err);
105 process.exit(1);
106 });
107 });
108
109// error on unknown commands
110// ref: https://github.com/tj/commander.js#custom-event-listeners
111program.on('command:*', function () {
112 console.error(
113 'Invalid command: %s\nSee --help for a list of available commands.',
114 program.args.join(' '),
115 );
116 process.exit(1);
117});
118
119program.parse(process.argv);
120
121// check if no command line args are provided
122// ref: https://github.com/tj/commander.js/issues/7#issuecomment-32448653
123var NO_COMMAND_SPECIFIED = program.args.length === 0;
124
125if (NO_COMMAND_SPECIFIED) {
126 // user did not supply args, so show --help
127 program.help();
128}