Skip to content

Commit 9bd22e6

Browse files
author
sw-yx
committed
add install command
1 parent 9f9464b commit 9bd22e6

File tree

4 files changed

+238
-10
lines changed

4 files changed

+238
-10
lines changed

bin/cmd.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@ var pkg = JSON.parse(
1212
);
1313
var build = require("../lib/build");
1414
var serve = require("../lib/serve");
15+
var install = require("../lib/install");
1516

1617
program.version(pkg.version);
1718

1819
const stringBooleanToBoolean = val => {
19-
console.log({val});
20-
if (typeof val !== 'string' && (val !== 'true' || val !== 'false')) {
20+
console.log({ val });
21+
if (typeof val !== "string" && (val !== "true" || val !== "false")) {
2122
throw Error(`Incorrect string value: ${val}`);
2223
}
2324

24-
return val === 'true';
25+
return val === "true";
2526
};
2627

2728
program
2829
.option("-c --config <webpack-config>", "additional webpack configuration")
2930
.option("-p --port <port>", "port to serve from (default: 9000)")
30-
.option("-b --babelrc <babelrc>", "use .babelrc in root (default: true)", stringBooleanToBoolean)
31+
.option(
32+
"-b --babelrc <babelrc>",
33+
"use .babelrc in root (default: true)",
34+
stringBooleanToBoolean
35+
)
3136
.option(
3237
"-t --timeout <timeout>",
3338
"function invocation timeout in seconds (default: 10)"
@@ -47,13 +52,13 @@ program
4752
static,
4853
Number(program.timeout) || 10
4954
);
50-
}
55+
};
5156
if (static) {
5257
startServer();
5358
return; // early terminate, don't build
54-
};
55-
const { config: userWebpackConfig, babelrc: useBabelrc = true} = program;
56-
build.watch(cmd, { userWebpackConfig, useBabelrc}, function(err, stats) {
59+
}
60+
const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
61+
build.watch(cmd, { userWebpackConfig, useBabelrc }, function(err, stats) {
5762
if (err) {
5863
console.error(err);
5964
return;
@@ -75,9 +80,9 @@ program
7580
.action(function(cmd, options) {
7681
console.log("netlify-lambda: Building functions");
7782

78-
const { config: userWebpackConfig, babelrc: useBabelrc = true} = program;
83+
const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
7984
build
80-
.run(cmd, { userWebpackConfig, useBabelrc})
85+
.run(cmd, { userWebpackConfig, useBabelrc })
8186
.then(function(stats) {
8287
console.log(stats.toString({ color: true }));
8388
})
@@ -87,6 +92,17 @@ program
8792
});
8893
});
8994

95+
program
96+
.command("install [dir]")
97+
.description("install functions")
98+
.action(function(cmd, options) {
99+
console.log("netlify-lambda: installing function dependencies");
100+
install.run(cmd).catch(function(err) {
101+
console.error(err);
102+
process.exit(1);
103+
});
104+
});
105+
90106
// error on unknown commands
91107
// ref: https://github.com/tj/commander.js#custom-event-listeners
92108
program.on("command:*", function() {

lib/install.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const path = require("path");
2+
const fs = require("fs");
3+
const globby = require("globby");
4+
const cp = require("child_process");
5+
var conf = require("./config");
6+
7+
function installDeps(functionDir, cb) {
8+
cp.exec("npm i", { cwd: functionDir }, cb);
9+
}
10+
11+
exports.run = async function(dir) {
12+
let directory;
13+
if (dir) {
14+
var dirPath = path.join(process.cwd(), dir);
15+
directory = dirPath;
16+
} else {
17+
var config = conf.load();
18+
const functionsDir = config.build.functions || config.build.Functions;
19+
if (!functionsDir) {
20+
console.log("Error: no functions dir detected.");
21+
}
22+
const functionsPath = path.join(process.cwd(), functionsDir);
23+
directory = functionsPath;
24+
}
25+
26+
const findJSFiles = ["*/package.json", "!node_modules", "!**/node_modules"];
27+
const foldersWithDeps = await globby(findJSFiles, { cwd: directory });
28+
29+
const folders = foldersWithDeps
30+
.map(fnFolder => {
31+
return fnFolder.substring(0, fnFolder.indexOf("package.json"));
32+
})
33+
.map(folder => {
34+
installDeps(path.join(directory, folder), () => {
35+
console.log(`${folder} dependencies installed`);
36+
});
37+
});
38+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"devDependencies": {
4646
"auto-changelog": "^1.13.0",
4747
"gh-release": "^3.5.0",
48+
"globby": "^10.0.1",
4849
"jest": "^23.6.0"
4950
}
5051
}

0 commit comments

Comments
 (0)