UNPKG

3.36 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 = 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; // early terminate, don't build
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
76program
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
94program
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// error on unknown commands
106// ref: https://github.com/tj/commander.js#custom-event-listeners
107program.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
115program.parse(process.argv);
116
117// check if no command line args are provided
118// ref: https://github.com/tj/commander.js/issues/7#issuecomment-32448653
119var NO_COMMAND_SPECIFIED = program.args.length === 0;
120
121if (NO_COMMAND_SPECIFIED) {
122 // user did not supply args, so show --help
123 program.help();
124}