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 = 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;
|
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 |
|
80 | program
|
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 |
|
98 | program
|
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 |
|
110 |
|
111 | program.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 |
|
119 | program.parse(process.argv);
|
120 |
|
121 |
|
122 |
|
123 | var NO_COMMAND_SPECIFIED = program.args.length === 0;
|
124 |
|
125 | if (NO_COMMAND_SPECIFIED) {
|
126 |
|
127 | program.help();
|
128 | }
|