Skip to content

Crawl up directories to find config, to support monorepos #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require("path");
var conf = require("./config");
var webpack = require("webpack");
var merge = require("webpack-merge");
var findUp = require('find-up');

// custom babel target for each node version
function getBabelTarget(envConfig) {
Expand All @@ -14,10 +15,12 @@ function getBabelTarget(envConfig) {
}

function webpackConfig(dir, additionalConfig) {
var config = conf.load();
var allConfig = conf.load();
var config = allConfig.config;
var rootPath = allConfig.rootPath;
var envConfig = config.build.environment || config.build.Environment || {};
var babelOpts = { cacheDirectory: true };
if (!fs.existsSync(path.join(process.cwd(), ".babelrc"))) {
if (!findUp.sync(".babelrc")) {
babelOpts.presets = [
["@babel/preset-env", { targets: { node: getBabelTarget(envConfig) } }]
];
Expand All @@ -30,7 +33,7 @@ function webpackConfig(dir, additionalConfig) {
}

var functionsDir = config.build.functions || config.build.Functions;
var functionsPath = path.join(process.cwd(), functionsDir);
var functionsPath = path.join(rootPath, functionsDir);
var dirPath = path.join(process.cwd(), dir);

if (dirPath === functionsPath) {
Expand Down
9 changes: 7 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
var toml = require("toml");
var fs = require("fs");
var path = require("path");
var findUp = require('find-up');
var path = require('path');

exports.load = function() {
var configPath = path.join(process.cwd(), "netlify.toml");
var configPath = findUp.sync("netlify.toml");
if (!fs.existsSync(configPath)) {
console.error(
"No netlify.toml found. This is needed to configure the function settings"
);
process.exit(1);
}

return toml.parse(fs.readFileSync(configPath));
return {
config: toml.parse(fs.readFileSync(configPath)),
rootPath: path.join(configPath, '..'),
};
};
13 changes: 9 additions & 4 deletions lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function createHandler(dir, static) {
var func = cleanPath.split("/").filter(function(e) {
return e;
})[0];
var module = path.join(process.cwd(), dir, func);
var module = path.join(dir, func);
if(static) {
delete require.cache[require.resolve(module)]
}
Expand Down Expand Up @@ -82,9 +82,14 @@ function createHandler(dir, static) {
}

exports.listen = function(port, static) {
var config = conf.load();
var allConfig = conf.load();
var config = allConfig.config;
var rootPath = allConfig.rootPath;

var app = express();
var dir = config.build.functions || config.build.Functions;
var relativeDir = config.build.functions || config.build.Functions;
var dir = path.join(rootPath, relativeDir);

app.use(bodyParser.raw({limit: "6mb"}));
app.use(bodyParser.text({limit: "6mb", type: "*/*"}));
app.use(expressLogging(console, {
Expand All @@ -107,7 +112,7 @@ exports.listen = function(port, static) {

return {
clearCache: function(chunk) {
var module = path.join(process.cwd(), dir, chunk);
var module = path.join(dir, chunk);
delete require.cache[require.resolve(module)];
}
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"commander": "^2.17.1",
"express": "^4.16.3",
"express-logging": "^1.1.1",
"find-up": "^3.0.0",
"toml": "^2.3.3",
"webpack": "^4.17.1",
"webpack-merge": "^4.1.4"
Expand Down
29 changes: 29 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,12 @@ find-up@^2.1.0:
dependencies:
locate-path "^2.0.0"

find-up@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
dependencies:
locate-path "^3.0.0"

flush-write-stream@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.0.3.tgz#c5d586ef38af6097650b49bc41b55fabb19f35bd"
Expand Down Expand Up @@ -2079,6 +2085,13 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"

locate-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
dependencies:
p-locate "^3.0.0"
path-exists "^3.0.0"

lodash.debounce@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
Expand Down Expand Up @@ -2477,12 +2490,28 @@ p-limit@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc"

p-limit@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.1.0.tgz#1d5a0d20fb12707c758a655f6bbc4386b5930d68"
dependencies:
p-try "^2.0.0"

p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
dependencies:
p-limit "^1.1.0"

p-locate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4"
dependencies:
p-limit "^2.0.0"

p-try@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.0.0.tgz#85080bb87c64688fa47996fe8f7dfbe8211760b1"

pako@~0.2.0:
version "0.2.9"
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
Expand Down