1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var program = require('commander');
|
8 | var fs = require('fs');
|
9 | var path = require('path');
|
10 | var pkg = JSON.parse(
|
11 | fs.readFileSync(path.join(__dirname, '..', 'package.json')),
|
12 | );
|
13 | var build = require('../lib/build');
|
14 | var serve = require('../lib/serve');
|
15 | var install = require('../lib/install');
|
16 |
|
17 | program.version(pkg.version);
|
18 |
|
19 | const 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 |
|
28 | program
|
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 |
|
42 | program
|
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 = function () {
|
50 | server = serve.listen(
|
51 | program.port || 9000,
|
52 | static,
|
53 | Number(program.timeout) || 10,
|
54 | );
|
55 | };
|
56 | if (static) {
|
57 | startServer();
|
58 | return;
|
59 | }
|
60 | const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
|
61 | build.watch(cmd, { userWebpackConfig, useBabelrc }, function (err, stats) {
|
62 | if (err) {
|
63 | console.error(err);
|
64 | return;
|
65 | }
|
66 | console.log(stats.toString(stats.compilation.options.stats));
|
67 | if (!server) {
|
68 | startServer();
|
69 | }
|
70 | stats.compilation.chunks.forEach(function (chunk) {
|
71 | server.clearCache(chunk.name || chunk.id.toString());
|
72 | });
|
73 | });
|
74 | });
|
75 |
|
76 | program
|
77 | .command('build <dir>')
|
78 | .description('build functions')
|
79 | .action(function (cmd) {
|
80 | console.log('netlify-lambda: Building functions');
|
81 |
|
82 | const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
|
83 | build
|
84 | .run(cmd, { userWebpackConfig, useBabelrc })
|
85 | .then(function (stats) {
|
86 | console.log(stats.toString(stats.compilation.options.stats));
|
87 | })
|
88 | .catch(function (err) {
|
89 | console.error(err);
|
90 | process.exit(1);
|
91 | });
|
92 | });
|
93 |
|
94 | program
|
95 | .command('install [dir]')
|
96 | .description('install functions')
|
97 | .action(function (cmd) {
|
98 | console.log('netlify-lambda: installing function dependencies');
|
99 | install.run(cmd).catch(function (err) {
|
100 | console.error(err);
|
101 | process.exit(1);
|
102 | });
|
103 | });
|
104 |
|
105 |
|
106 |
|
107 | program.on('command:*', function () {
|
108 | console.error(
|
109 | 'Invalid command: %s\nSee --help for a list of available commands.',
|
110 | program.args.join(' '),
|
111 | );
|
112 | process.exit(1);
|
113 | });
|
114 |
|
115 | program.parse(process.argv);
|
116 |
|
117 |
|
118 |
|
119 | var NO_COMMAND_SPECIFIED = program.args.length === 0;
|
120 |
|
121 | if (NO_COMMAND_SPECIFIED) {
|
122 |
|
123 | program.help();
|
124 | }
|