Skip to content

Commit

Permalink
add install command
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Aug 6, 2019
1 parent 9f9464b commit 9bd22e6
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 10 deletions.
36 changes: 26 additions & 10 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ var pkg = JSON.parse(
);
var build = require("../lib/build");
var serve = require("../lib/serve");
var install = require("../lib/install");

program.version(pkg.version);

const stringBooleanToBoolean = val => {
console.log({val});
if (typeof val !== 'string' && (val !== 'true' || val !== 'false')) {
console.log({ val });
if (typeof val !== "string" && (val !== "true" || val !== "false")) {
throw Error(`Incorrect string value: ${val}`);
}

return val === 'true';
return val === "true";
};

program
.option("-c --config <webpack-config>", "additional webpack configuration")
.option("-p --port <port>", "port to serve from (default: 9000)")
.option("-b --babelrc <babelrc>", "use .babelrc in root (default: true)", stringBooleanToBoolean)
.option(
"-b --babelrc <babelrc>",
"use .babelrc in root (default: true)",
stringBooleanToBoolean
)
.option(
"-t --timeout <timeout>",
"function invocation timeout in seconds (default: 10)"
Expand All @@ -47,13 +52,13 @@ program
static,
Number(program.timeout) || 10
);
}
};
if (static) {
startServer();
return; // early terminate, don't build
};
const { config: userWebpackConfig, babelrc: useBabelrc = true} = program;
build.watch(cmd, { userWebpackConfig, useBabelrc}, function(err, stats) {
}
const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
build.watch(cmd, { userWebpackConfig, useBabelrc }, function(err, stats) {
if (err) {
console.error(err);
return;
Expand All @@ -75,9 +80,9 @@ program
.action(function(cmd, options) {
console.log("netlify-lambda: Building functions");

const { config: userWebpackConfig, babelrc: useBabelrc = true} = program;
const { config: userWebpackConfig, babelrc: useBabelrc = true } = program;
build
.run(cmd, { userWebpackConfig, useBabelrc})
.run(cmd, { userWebpackConfig, useBabelrc })
.then(function(stats) {
console.log(stats.toString({ color: true }));
})
Expand All @@ -87,6 +92,17 @@ program
});
});

program
.command("install [dir]")
.description("install functions")
.action(function(cmd, options) {
console.log("netlify-lambda: installing function dependencies");
install.run(cmd).catch(function(err) {
console.error(err);
process.exit(1);
});
});

// error on unknown commands
// ref: https://github.com/tj/commander.js#custom-event-listeners
program.on("command:*", function() {
Expand Down
38 changes: 38 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require("path");
const fs = require("fs");
const globby = require("globby");
const cp = require("child_process");
var conf = require("./config");

function installDeps(functionDir, cb) {
cp.exec("npm i", { cwd: functionDir }, cb);
}

exports.run = async function(dir) {
let directory;
if (dir) {
var dirPath = path.join(process.cwd(), dir);
directory = dirPath;
} else {
var config = conf.load();
const functionsDir = config.build.functions || config.build.Functions;
if (!functionsDir) {
console.log("Error: no functions dir detected.");
}
const functionsPath = path.join(process.cwd(), functionsDir);
directory = functionsPath;
}

const findJSFiles = ["*/package.json", "!node_modules", "!**/node_modules"];
const foldersWithDeps = await globby(findJSFiles, { cwd: directory });

const folders = foldersWithDeps
.map(fnFolder => {
return fnFolder.substring(0, fnFolder.indexOf("package.json"));
})
.map(folder => {
installDeps(path.join(directory, folder), () => {
console.log(`${folder} dependencies installed`);
});
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"auto-changelog": "^1.13.0",
"gh-release": "^3.5.0",
"globby": "^10.0.1",
"jest": "^23.6.0"
}
}
Loading

0 comments on commit 9bd22e6

Please sign in to comment.