Skip to content
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

Update Config parsing to work with netlify build #616

Merged
merged 13 commits into from
Dec 6, 2019
32 changes: 28 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion src/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -21,14 +26,23 @@ 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()

// 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
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('netlify-cli-logo')
} = require('../../utils/logo')

class ExecCommand extends Command {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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')
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('netlify-cli-logo')
} = require('../../utils/logo')

class FunctionsBuildCommand extends Command {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
NETLIFYDEVLOG,
NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('netlify-cli-logo')
} = require('../../utils/logo')

const templatesDir = path.resolve(__dirname, '../../functions-templates')

Expand Down
2 changes: 1 addition & 1 deletion src/detectors/utils/jsdetect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 19 additions & 7 deletions src/utils/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,16 +25,28 @@ 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
let configPath
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why suppress this error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every site needs netlify config file. They might have build settings set in the UI

The @netlify/build config package was throwing hard errors when config was missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be updated in build? @ehmicky did you update how config handles this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this try/catch should not be needed anymore. Missing configuration files do not throw anymore.

throw err
}
}
// Get site id & build state
const state = new StateConfig(projectRoot)

Expand Down
10 changes: 5 additions & 5 deletions src/utils/detect-functions-builder.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
36 changes: 18 additions & 18 deletions src/utils/detect-server.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
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')
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions src/utils/get-config-path.js

This file was deleted.

8 changes: 6 additions & 2 deletions src/utils/init/config-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down
2 changes: 1 addition & 1 deletion src/utils/live-tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('netlify-cli-logo')
} = require('./logo')

async function createTunnel(siteId, netlifyApiToken, log) {
await installTunnelClient(log)
Expand Down
8 changes: 8 additions & 0 deletions src/utils/logo.js
Original file line number Diff line number Diff line change
@@ -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("◈")}`
}
27 changes: 0 additions & 27 deletions src/utils/read-config.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/serve-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('netlify-cli-logo')
} = require('./logo')
const { getFunctions } = require('./get-functions')

const defaultPort = 34567
Expand Down