From 4012c9d66fe37af570e43e3007ec527d373e5fd1 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Mon, 18 Nov 2019 14:52:32 -0800 Subject: [PATCH 01/12] chore: update config dep --- package-lock.json | 32 ++++++++++++++++++++++++++++---- package.json | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 70b3e9b00a3..b6fc0155014 100644 --- a/package-lock.json +++ b/package-lock.json @@ -695,18 +695,37 @@ } }, "@netlify/config": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/@netlify/config/-/config-0.1.1.tgz", - "integrity": "sha512-LMywGz02K9K/NmKyDFN7AlevNzpGAjj5xF1q3Bw7AYQ9HVgvtCktHQGBQJkQKvbj8F+igJBgStOBouwJoP4EHA==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/@netlify/config/-/config-0.1.7.tgz", + "integrity": "sha512-qg3pKzzyxYe6F2hExp7Akv9wJoGLw5zZ9bMv9duAzUkVqyAI/Gcv1ymYi4kkP0cop/92lJ70MzBkfHuexgpvWw==", "requires": { + "chalk": "^2.4.2", "configorama": "^0.3.6", + "deepmerge": "^4.2.2", + "dot-prop": "^5.2.0", "find-up": "^4.1.0", + "indent-string": "^4.0.0", + "is-plain-obj": "^2.0.0", + "js-yaml": "^3.13.1", + "make-dir": "^3.0.0", "map-obj": "^4.1.0", - "minimist": "^1.2.0", "path-exists": "^4.0.0", "resolve": "^1.12.0" }, "dependencies": { + "deepmerge": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", + "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==" + }, + "dot-prop": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.2.0.tgz", + "integrity": "sha512-uEUyaDKoSQ1M4Oq8l45hSE26SnTxL6snNnqvK/VWx5wJhmff5z0FUVJDKDanor/6w3kzE3i7XZOk+7wC0EXr1A==", + "requires": { + "is-obj": "^2.0.0" + } + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -716,6 +735,11 @@ "path-exists": "^4.0.0" } }, + "indent-string": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-4.0.0.tgz", + "integrity": "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==" + }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", diff --git a/package.json b/package.json index ad92f4bb053..b0dac27643a 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "dependencies": { "@iarna/toml": "^2.2.3", "@netlify/build": "^0.1.7", - "@netlify/config": "^0.1.1", + "@netlify/config": "^0.1.7", "@netlify/zip-it-and-ship-it": "^0.3.1", "@oclif/command": "^1.5.18", "@oclif/config": "^1.13.2", From 6bd8a44c9c7ae08832ca1099f0820f9ea80eba09 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Mon, 18 Nov 2019 14:54:41 -0800 Subject: [PATCH 02/12] refactor: delete old config parsing --- src/utils/get-config-path.js | 22 ---------------------- src/utils/read-config.js | 27 --------------------------- 2 files changed, 49 deletions(-) delete mode 100644 src/utils/get-config-path.js delete mode 100644 src/utils/read-config.js diff --git a/src/utils/get-config-path.js b/src/utils/get-config-path.js deleted file mode 100644 index da23ac86a62..00000000000 --- a/src/utils/get-config-path.js +++ /dev/null @@ -1,22 +0,0 @@ -const fs = require('fs') -const path = require('path') - -module.exports = function getConfigPath(root) { - // TODO support more formats - const tomlPath = path.join(root, 'netlify.toml') - - if (fileExistsSync(tomlPath)) { - return tomlPath - } - - return undefined -} - -function fileExistsSync(filePath) { - try { - const stats = fs.lstatSync(filePath) - return stats.isFile() - } catch (e) { - return false - } -} diff --git a/src/utils/read-config.js b/src/utils/read-config.js deleted file mode 100644 index e38bf4effac..00000000000 --- a/src/utils/read-config.js +++ /dev/null @@ -1,27 +0,0 @@ -const fs = require('fs') -const TOML = require('@iarna/toml') -const permissionError = "You don't have access to this file." - -module.exports = function getConfigData(configPath) { - // No config value. probably not in folder - if (!configPath) { - return {} - } - - try { - // TODO support more formats - return TOML.parse(fs.readFileSync(configPath, 'utf8')) - } catch (err) { - // Don't create if it doesn't exist - if (err.code === 'ENOENT') { - return {} - } - - // Improve the message of permission errors - if (err.code === 'EACCES') { - err.message = `${err.message}\n${permissionError}\n` - } - - throw err - } -} From f28376138eb57557d04a1c3dab01d06527bcf910 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Mon, 18 Nov 2019 14:54:56 -0800 Subject: [PATCH 03/12] chore: code formatting --- src/utils/init/config-github.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils/init/config-github.js b/src/utils/init/config-github.js index 42c34e3a776..3f59ea3ff41 100644 --- a/src/utils/init/config-github.js +++ b/src/utils/init/config-github.js @@ -49,8 +49,12 @@ async function configGithub(ctx, site, repo) { let defaultBuildCmd, defaultBuildDir = '.' const { build } = ctx.netlify.config // read from netlify toml - if (build && build.command) defaultBuildCmd = build.command - if (build && build.publish) defaultBuildDir = build.publish + if (build && build.command) { + defaultBuildCmd = build.command + } + if (build && build.publish) { + defaultBuildDir = build.publish + } if (build && build.functions) console.log('Netlify functions folder is ' + chalk.yellow(build.functions)) const { buildCmd, buildDir } = await inquirer.prompt([ { From 6e89477b9a407d5e8eeee94b2255a76834b86f4a Mon Sep 17 00:00:00 2001 From: DavidWells Date: Mon, 18 Nov 2019 14:55:10 -0800 Subject: [PATCH 04/12] refactor: read config with @netlify/config --- src/utils/command.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/utils/command.js b/src/utils/command.js index 0e9312c9b80..65f147ecb6e 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -9,8 +9,8 @@ const StateConfig = require('./state-config') const globalConfig = require('./global-config') const findRoot = require('./find-root') const chalkInstance = require('./chalk') -const readConfig = require('./read-config') -const getConfigPath = require('./get-config-path') +const resolveConfig = require('@netlify/config') +const getConfigPath = require('@netlify/config').getConfigPath const argv = require('minimist')(process.argv.slice(2)) const { NETLIFY_AUTH_TOKEN, NETLIFY_API_URL } = process.env @@ -25,16 +25,19 @@ class BaseCommand extends Command { } // Initialize context async init(_projectRoot) { - const projectRoot = findRoot(_projectRoot || process.cwd()) // if calling programmatically, can use a supplied root, else in normal CLI context it just uses process.cwd() + const cwd = argv.cwd || process.cwd() + const projectRoot = findRoot(_projectRoot || cwd) // if calling programmatically, can use a supplied root, else in normal CLI context it just uses process.cwd() // Grab netlify API token const authViaFlag = getAuthArg(argv) const [token] = this.getConfigToken(authViaFlag) - // Get site config from netlify.toml - const configPath = getConfigPath(projectRoot) - // TODO: https://github.com/request/caseless to handle key casing issues - const config = readConfig(configPath) + // Read new netlify.toml/yml/json + const configPath = await getConfigPath(argv.config, cwd) + const config = await resolveConfig(configPath, { + cwd: cwd, + context: argv.context + }) // Get site id & build state const state = new StateConfig(projectRoot) @@ -137,7 +140,7 @@ class BaseCommand extends Command { Object.assign( {}, { - context: this + context: 'foo' }, opts ) From d2f3b2b8e9c3098dc27bf3c13ef156d04e4b389c Mon Sep 17 00:00:00 2001 From: DavidWells Date: Mon, 18 Nov 2019 15:05:47 -0800 Subject: [PATCH 05/12] chore: restore this --- src/utils/command.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/command.js b/src/utils/command.js index 65f147ecb6e..856f85ed7cc 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -140,7 +140,7 @@ class BaseCommand extends Command { Object.assign( {}, { - context: 'foo' + context: this }, opts ) From 52646b9f9e4b4c413a47b0ccea9a4913cf39a9f0 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Tue, 19 Nov 2019 14:33:18 -0800 Subject: [PATCH 06/12] refactor: remove cli logo dep --- package.json | 1 - src/commands/dev/exec.js | 2 +- src/commands/dev/index.js | 2 +- src/commands/functions/build.js | 2 +- src/commands/functions/create.js | 2 +- src/detectors/utils/jsdetect.js | 2 +- src/utils/detect-server.js | 2 +- src/utils/dev.js | 2 +- src/utils/live-tunnel.js | 2 +- src/utils/logo.js | 8 ++++++++ src/utils/rules-proxy.js | 2 +- src/utils/serve-functions.js | 2 +- 12 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 src/utils/logo.js diff --git a/package.json b/package.json index b0dac27643a..333d8629e92 100644 --- a/package.json +++ b/package.json @@ -116,7 +116,6 @@ "make-dir": "^3.0.0", "minimist": "^1.2.0", "netlify": "^2.4.8", - "netlify-cli-logo": "^1.0.0", "netlify-redirector": "^0.1.0", "node-fetch": "^2.6.0", "npm-packlist": "^1.4.4", diff --git a/src/commands/dev/exec.js b/src/commands/dev/exec.js index 788fd838eb6..cc33754a01f 100644 --- a/src/commands/dev/exec.js +++ b/src/commands/dev/exec.js @@ -5,7 +5,7 @@ const { NETLIFYDEVLOG, // NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('../../utils/logo') class ExecCommand extends Command { async run() { diff --git a/src/commands/dev/index.js b/src/commands/dev/index.js index 0c120cd081f..4a5189b1831 100644 --- a/src/commands/dev/index.js +++ b/src/commands/dev/index.js @@ -24,7 +24,7 @@ const { NETLIFYDEVLOG, NETLIFYDEVWARN // NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('../../utils/logo') const boxen = require('boxen') const { createTunnel, connectTunnel } = require('../../utils/live-tunnel') const createRewriter = require('../../utils/rules-proxy') diff --git a/src/commands/functions/build.js b/src/commands/functions/build.js index 6e2a5a076d8..82c1e0ec102 100644 --- a/src/commands/functions/build.js +++ b/src/commands/functions/build.js @@ -7,7 +7,7 @@ const { NETLIFYDEVLOG, // NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('../../utils/logo') class FunctionsBuildCommand extends Command { async run() { diff --git a/src/commands/functions/create.js b/src/commands/functions/create.js index 187b88bb5ff..b88b18ca932 100644 --- a/src/commands/functions/create.js +++ b/src/commands/functions/create.js @@ -16,7 +16,7 @@ const { NETLIFYDEVLOG, NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('../../utils/logo') const templatesDir = path.resolve(__dirname, '../../functions-templates') diff --git a/src/detectors/utils/jsdetect.js b/src/detectors/utils/jsdetect.js index 4875d755e77..e1430f0c0d9 100644 --- a/src/detectors/utils/jsdetect.js +++ b/src/detectors/utils/jsdetect.js @@ -7,7 +7,7 @@ const { existsSync, readFileSync } = require('fs') let pkgJSON = null let yarnExists = false let warnedAboutEmptyScript = false -const { NETLIFYDEVWARN } = require('netlify-cli-logo') +const { NETLIFYDEVWARN } = require('../../utils/logo') /** hold package.json in a singleton so we dont do expensive parsing repeatedly */ function getPkgJSON() { diff --git a/src/utils/detect-server.js b/src/utils/detect-server.js index a1b38729895..e164c4b9125 100644 --- a/src/utils/detect-server.js +++ b/src/utils/detect-server.js @@ -1,6 +1,6 @@ const path = require('path') const chalk = require('chalk') -const { NETLIFYDEVLOG } = require('netlify-cli-logo') +const { NETLIFYDEVLOG } = require('./logo') const inquirer = require('inquirer') const fuzzy = require('fuzzy') const fs = require('fs') diff --git a/src/utils/dev.js b/src/utils/dev.js index 4751b54abbf..eb8c4f60004 100644 --- a/src/utils/dev.js +++ b/src/utils/dev.js @@ -8,7 +8,7 @@ const { NETLIFYDEVLOG, // NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('./logo') /** * inject environment variables from netlify addons and buildbot * into your local dev process.env diff --git a/src/utils/live-tunnel.js b/src/utils/live-tunnel.js index b0150340390..a3d20582611 100644 --- a/src/utils/live-tunnel.js +++ b/src/utils/live-tunnel.js @@ -9,7 +9,7 @@ const { NETLIFYDEVLOG, // NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('./logo') async function createTunnel(siteId, netlifyApiToken, log) { await installTunnelClient(log) diff --git a/src/utils/logo.js b/src/utils/logo.js new file mode 100644 index 00000000000..b12ddc9e308 --- /dev/null +++ b/src/utils/logo.js @@ -0,0 +1,8 @@ +const chalk = require("chalk") + +module.exports = { + NETLIFYDEV: `${chalk.greenBright("◈")} ${chalk.rgb(40, 180, 170)("Netlify Dev")} ${chalk.greenBright("◈")}`, + NETLIFYDEVLOG: `${chalk.greenBright("◈")}`, + NETLIFYDEVWARN: `${chalk.yellowBright("◈")}`, + NETLIFYDEVERR: `${chalk.redBright("◈")}` +} diff --git a/src/utils/rules-proxy.js b/src/utils/rules-proxy.js index a12b583e8c5..11535340232 100644 --- a/src/utils/rules-proxy.js +++ b/src/utils/rules-proxy.js @@ -5,7 +5,7 @@ const redirector = require('netlify-redirector') const chokidar = require('chokidar') const cookie = require('cookie') const redirectParser = require('./redirect-parser') -const { NETLIFYDEVWARN } = require('netlify-cli-logo') +const { NETLIFYDEVWARN } = require('./logo') function parseFile(parser, name, data) { const result = parser(data) diff --git a/src/utils/serve-functions.js b/src/utils/serve-functions.js index c98a4b1877a..1531edeab5f 100644 --- a/src/utils/serve-functions.js +++ b/src/utils/serve-functions.js @@ -9,7 +9,7 @@ const { NETLIFYDEVLOG, // NETLIFYDEVWARN, NETLIFYDEVERR -} = require('netlify-cli-logo') +} = require('./logo') const { getFunctions } = require('./get-functions') const defaultPort = 34567 From 18f1b306c6ad101cc8126c7174a1f99ccf15ef32 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Tue, 19 Nov 2019 14:33:57 -0800 Subject: [PATCH 07/12] fix: dont run detector sync lookup for every command --- src/utils/detect-server.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/utils/detect-server.js b/src/utils/detect-server.js index e164c4b9125..61736bbb3f4 100644 --- a/src/utils/detect-server.js +++ b/src/utils/detect-server.js @@ -4,25 +4,25 @@ const { NETLIFYDEVLOG } = require('./logo') const inquirer = require('inquirer') const fuzzy = require('fuzzy') const fs = require('fs') -const detectors = fs - .readdirSync(path.join(__dirname, '..', 'detectors')) - .filter(x => x.endsWith('.js')) // only accept .js detector files - .map(det => { - try { - return require(path.join(__dirname, '..', `detectors/${det}`)) - } catch (err) { - console.error( - `failed to load detector: ${chalk.yellow( - det - )}, this is likely a bug in the detector, please file an issue in netlify-dev-plugin`, - err - ) - return null - } - }) - .filter(Boolean) module.exports.serverSettings = async devConfig => { + const detectors = fs + .readdirSync(path.join(__dirname, '..', 'detectors')) + .filter(x => x.endsWith('.js')) // only accept .js detector files + .map(det => { + try { + return require(path.join(__dirname, '..', `detectors/${det}`)) + } catch (err) { + console.error( + `failed to load detector: ${chalk.yellow( + det + )}, this is likely a bug in the detector, please file an issue in netlify-dev-plugin`, + err + ) + return null + } + }) + .filter(Boolean) let settingsArr = [] let settings = null for (const i in detectors) { From 1a1c7f8a03918d8ca323b4cafa6b9b13f7fa132b Mon Sep 17 00:00:00 2001 From: DavidWells Date: Tue, 19 Nov 2019 14:35:19 -0800 Subject: [PATCH 08/12] fix: dont run sync FS reads for everything --- src/utils/detect-functions-builder.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/detect-functions-builder.js b/src/utils/detect-functions-builder.js index 6adfc968ecd..3460096bcc8 100644 --- a/src/utils/detect-functions-builder.js +++ b/src/utils/detect-functions-builder.js @@ -1,11 +1,11 @@ const path = require('path') -const detectors = require('fs') - .readdirSync(path.join(__dirname, '..', 'function-builder-detectors')) - .filter(x => x.endsWith('.js')) // only accept .js detector files - .map(det => require(path.join(__dirname, '..', `function-builder-detectors/${det}`))) - module.exports.detectFunctionsBuilder = function() { + const detectors = require('fs') + .readdirSync(path.join(__dirname, '..', 'function-builder-detectors')) + .filter(x => x.endsWith('.js')) // only accept .js detector files + .map(det => require(path.join(__dirname, '..', `function-builder-detectors/${det}`))) + for (const i in detectors) { const settings = detectors[i]() if (settings) { From 8a7a26ce0e7ff12f64700a4ec40830cb75a504a5 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Tue, 19 Nov 2019 14:51:30 -0800 Subject: [PATCH 09/12] refactor: suppress config not found errors in CLI --- src/utils/command.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/utils/command.js b/src/utils/command.js index 856f85ed7cc..7b89fb3d760 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -33,11 +33,20 @@ class BaseCommand extends Command { const [token] = this.getConfigToken(authViaFlag) // Read new netlify.toml/yml/json - const configPath = await getConfigPath(argv.config, cwd) - const config = await resolveConfig(configPath, { - cwd: cwd, - context: argv.context - }) + let configPath = projectRoot + let config = {} + try { + configPath = await getConfigPath(argv.config, cwd) + config = await resolveConfig(configPath, { + cwd: cwd, + context: argv.context + }) + } catch (err) { + // Suppress config not found error for CLI. @TODO Revisit + if (err.message.indexOf('No netlify configuration file was found') === -1) { + throw err + } + } // Get site id & build state const state = new StateConfig(projectRoot) From 442fb512f67c377a8b30c40d52896745b63c0d8b Mon Sep 17 00:00:00 2001 From: DavidWells Date: Tue, 19 Nov 2019 15:12:34 -0800 Subject: [PATCH 10/12] refactor: default config path undef --- src/utils/command.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/command.js b/src/utils/command.js index 7b89fb3d760..9a86fadced5 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -33,7 +33,7 @@ class BaseCommand extends Command { const [token] = this.getConfigToken(authViaFlag) // Read new netlify.toml/yml/json - let configPath = projectRoot + let configPath let config = {} try { configPath = await getConfigPath(argv.config, cwd) From eb3f96f588d15fe4db1d654cacace1f0c11b4110 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Thu, 5 Dec 2019 18:01:05 -0800 Subject: [PATCH 11/12] refactor: add siteId --- src/commands/build/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/commands/build/index.js b/src/commands/build/index.js index 82119ea2db1..c0234c80497 100644 --- a/src/commands/build/index.js +++ b/src/commands/build/index.js @@ -21,6 +21,7 @@ class BuildCommand extends Command { // Retrieve Netlify Build options async getOptions() { + const { site } = this.netlify const { raw } = this.parse(BuildCommand) const { dry = false } = parseRawFlags(raw) const [token] = this.getConfigToken() @@ -28,7 +29,15 @@ class BuildCommand extends Command { // Try current directory first, then site root const config = (await getConfigPath()) || (await getConfigPath(undefined, this.netlify.site.root)) - return { config, token, dry } + let options = { + config, + token, + dry + } + if (site.id) { + options.siteId = site.id + } + return options } } From b19136105e7864fa956c9b37210521356f5e0ff6 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Thu, 5 Dec 2019 18:05:29 -0800 Subject: [PATCH 12/12] chore: add todo --- src/commands/build/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/commands/build/index.js b/src/commands/build/index.js index c0234c80497..bb06eefb4d2 100644 --- a/src/commands/build/index.js +++ b/src/commands/build/index.js @@ -7,6 +7,11 @@ const { parseRawFlags } = require('../../utils/parse-raw-flags') class BuildCommand extends Command { // Run Netlify Build async run() { + /* + @TODO remove this.getOptions() & use the parsed config from Command. + this.netlify.config contains resolved config via @netlify/config + @netlify/build currently takes a path to config and resolves config values again + */ const options = await this.getOptions() await this.config.runHook('analytics', {