Skip to content
Merged
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
28 changes: 7 additions & 21 deletions packages/@netlify-build/src/build/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,22 @@ const getHookInstructions = function({
hook,
pluginsHooks,
netlifyConfig: {
build: { command: configCommand, lifecycle: configLifecycle }
build: {
lifecycle: { [hook]: lifecycleCommands }
}
},
redactedKeys
}) {
return [
// Support for old command. Add build.command to execution
configCommand && hook === 'build'
? {
name: `config.build.command`,
hook: 'build',
pluginConfig: {},
async method() {
await execCommand(configCommand, 'build.command', redactedKeys)
},
override: {}
}
: undefined,
// Merge in config lifecycle events first
configLifecycle && configLifecycle[hook]
lifecycleCommands
? {
name: `config.build.lifecycle.${hook}`,
hook,
pluginConfig: {},
async method() {
const commands = Array.isArray(configLifecycle[hook])
? configLifecycle[hook]
: configLifecycle[hook].split('\n')
// TODO pass in env vars if not available
// TODO return stdout?
await pMapSeries(commands, command => execCommand(command, `build.lifecycle.${hook}`, redactedKeys))
await pMapSeries(lifecycleCommands, command =>
execCommand(command, `build.lifecycle.${hook}`, redactedKeys)
)
},
override: {}
}
Expand Down
6 changes: 0 additions & 6 deletions packages/@netlify-build/src/build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ module.exports = async function build(options = {}) {
redactedKeys
})

if (netlifyConfig.build.lifecycle && netlifyConfig.build.command) {
throw new Error(
`build.command && build.lifecycle are both defined in config file. Please move build.command to build.lifecycle.build`
)
}

doDryRun({ buildInstructions, netlifyConfigPath, options })

logLifeCycleStart()
Expand Down
6 changes: 5 additions & 1 deletion packages/@netlify-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const configorama = require('configorama')
const pathExists = require('path-exists')

const getConfigPath = require('./path')
const { validateConfig } = require('./validate')
const { normalizeConfig } = require('./normalize')

async function netlifyConfig(configFile, options) {
const configPath = resolve(cwd(), configFile)
Expand Down Expand Up @@ -35,7 +37,9 @@ async function netlifyConfig(configFile, options) {
]
})

const configA = Object.assign({ build: {} }, config)
validateConfig(config)

const configA = normalizeConfig(config)
return configA
}

Expand Down
40 changes: 40 additions & 0 deletions packages/@netlify-config/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const mapObj = require('map-obj')
const omit = require('omit.js')

// Normalize configuration object
const normalizeConfig = function(config) {
const configA = Object.assign({ build: {} }, config)
const configB = normalizeLifecycles(configA)
return configB
}

const normalizeLifecycles = function(config) {
const {
build,
build: { command, lifecycle = {} }
} = config
const buildA = omit(build, ['command'])
const lifecycleA = normalizeCommand(lifecycle, command)

const lifecycleB = mapObj(lifecycleA, normalizeLifecycle)

return Object.assign({}, config, {
build: Object.assign({}, buildA, { lifecycle: lifecycleB })
})
}

// `build.lifecycle.build` was previously called `build.command`
const normalizeCommand = function(lifecycle, command) {
if (command === undefined) {
return lifecycle
}

return { ...lifecycle, build: command }
}

const normalizeLifecycle = function(hook, value) {
const valueA = typeof value === 'string' ? value.trim().split('\n') : value
return [hook, valueA]
}

module.exports = { normalizeConfig }
40 changes: 34 additions & 6 deletions packages/@netlify-config/package-lock.json

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

2 changes: 2 additions & 0 deletions packages/@netlify-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"dependencies": {
"configorama": "0.3.4",
"find-up": "^4.1.0",
"map-obj": "^4.1.0",
"minimist": "^1.2.0",
"omit.js": "^1.0.2",
"path-exists": "^4.0.0"
},
"devDependencies": {
Expand Down
12 changes: 9 additions & 3 deletions packages/@netlify-config/tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test('Test TOML', async t => {
t.deepEqual(config, {
build: {
publish: 'dist',
command: 'npm run build',
lifecycle: {
build: ['npm run build']
},
functions: 'functions'
}
})
Expand All @@ -22,7 +24,9 @@ test('Test YAML', async t => {
t.deepEqual(config, {
build: {
publish: 'dist',
command: 'npm run build',
lifecycle: {
build: ['npm run build']
},
functions: 'functions'
}
})
Expand All @@ -34,7 +38,9 @@ test('Test JSON', async t => {
t.deepEqual(config, {
build: {
publish: 'dist',
command: 'npm run build',
lifecycle: {
build: ['npm run build']
},
functions: 'functions'
}
})
Expand Down
14 changes: 14 additions & 0 deletions packages/@netlify-config/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Validate the configuration object
const validateConfig = function(config) {
validateOldCommand(config)
}

const validateOldCommand = function({ build: { command, lifecycle } = {} }) {
if (command !== undefined && lifecycle !== undefined) {
throw new Error(
`'build.command' and 'build.lifecycle' are both defined in the configuration. Please rename 'build.command' to 'build.lifecycle.build'`
)
}
}

module.exports = { validateConfig }