@netlify/config
Advanced tools
Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "@netlify/config", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Netlify config module", | ||
@@ -34,5 +34,5 @@ "main": "src/main.js", | ||
"js-yaml": "^3.13.1", | ||
"locate-path": "^5.0.0", | ||
"map-obj": "^4.1.0", | ||
"omit.js": "^1.0.2", | ||
"p-filter": "^2.1.0", | ||
"path-exists": "^4.0.0", | ||
@@ -43,3 +43,5 @@ "toml": "^3.0.0", | ||
"devDependencies": { | ||
"ava": "^2.4.0" | ||
"ava": "^2.4.0", | ||
"cpy": "^8.1.0", | ||
"has-ansi": "^4.0.0" | ||
}, | ||
@@ -46,0 +48,0 @@ "engines": { |
@@ -23,3 +23,3 @@ const yargs = require('yargs') | ||
string: true, | ||
describe: `Path to a configuration file containing default values. | ||
describe: `JSON configuration object containing default values. | ||
Each configuration default value is used unless overriden through the main configuration file. | ||
@@ -56,2 +56,10 @@ Default: none.`, | ||
}, | ||
baseRelDir: { | ||
boolean: true, | ||
describe: `Feature flag meant for backward compatibility. | ||
When enabled, if the 'build.base' configuration property is defined, it is used | ||
to try to retrieve a second configuration file and discard the first one. | ||
Default: true`, | ||
hidden: true, | ||
}, | ||
} | ||
@@ -58,0 +66,0 @@ |
@@ -34,4 +34,3 @@ #!/usr/bin/env node | ||
console.error(error.message) | ||
exit(1) | ||
return | ||
return exit(1) | ||
} | ||
@@ -38,0 +37,0 @@ |
@@ -20,16 +20,18 @@ require('./utils/polyfills') | ||
const resolveConfig = async function({ cachedConfig, ...opts } = {}) { | ||
// Performance optimization when @netlify/config caller has already previously | ||
// called it and cached the result. | ||
// This is used by the buildbot which: | ||
// - first calls @netlify/config since it needs configuration property | ||
// - later calls @netlify/build, which runs @netlify/config under the hood | ||
if (cachedConfig !== undefined) { | ||
return getCachedConfig(cachedConfig) | ||
return getConfig(cachedConfig, 'cached') | ||
} | ||
const { | ||
config: configOpt, | ||
defaultConfig: defaultConfigPath, | ||
cwd, | ||
context, | ||
repositoryRoot, | ||
branch, | ||
} = await normalizeOpts(opts) | ||
const { config: configOpt, defaultConfig, cwd, context, repositoryRoot, branch, baseRelDir } = await normalizeOpts( | ||
opts, | ||
) | ||
const defaultConfig = await getDefaultConfig(defaultConfigPath) | ||
// Retrieve default configuration file. It has less priority and it also does | ||
// not get normalized, merged with contexts, etc. | ||
const defaultConfigA = getConfig(defaultConfig, 'default') | ||
@@ -42,3 +44,4 @@ const { configPath, config } = await loadConfig({ | ||
branch, | ||
defaultConfig, | ||
defaultConfig: defaultConfigA, | ||
baseRelDir, | ||
}) | ||
@@ -52,23 +55,13 @@ | ||
// Performance optimization when @netlify/config caller has already previously | ||
// called it and cached the result. | ||
// This is used by the buildbot which: | ||
// - first calls @netlify/config since it needs configuration property | ||
// - later calls @netlify/build, which runs @netlify/config under the hood | ||
const getCachedConfig = function(cachedConfig) { | ||
try { | ||
return JSON.parse(cachedConfig) | ||
} catch (error) { | ||
throwError('When resolving cached configuration', error) | ||
// Load a configuration file passed as a JSON object. | ||
// The logic is much simpler: it does not normalize nor validate it. | ||
const getConfig = function(config, name) { | ||
if (config === undefined) { | ||
return {} | ||
} | ||
} | ||
// Retrieve default configuration file. It has less priority and it also does | ||
// not get normalized, merged with contexts, etc. | ||
const getDefaultConfig = async function(configPath) { | ||
try { | ||
return await parseConfig(configPath) | ||
return JSON.parse(config) | ||
} catch (error) { | ||
error.message = `When resolving default configuration ${configPath}:\n${error.message}` | ||
throw error | ||
throwError(`When resolving ${name} configuration`, error) | ||
} | ||
@@ -88,2 +81,3 @@ } | ||
defaultConfig: { build: { base: defaultBase } = {} }, | ||
baseRelDir, | ||
}) { | ||
@@ -98,4 +92,8 @@ const { | ||
// No second pass needed since there is no `build.base` | ||
if (base === undefined || base === defaultBase) { | ||
// No second pass needed if: | ||
// - there is no `build.base` | ||
// - `build.base` is the same as the `Base directory` UI setting (already used in the first round) | ||
// - `baseRelDir` feature flag is not used. This feature flag was introduced to ensure | ||
// backward compatibility. | ||
if (!baseRelDir || base === undefined || base === defaultBase) { | ||
return { configPath, config } | ||
@@ -134,4 +132,3 @@ } | ||
} catch (error) { | ||
const configMessage = configPath === undefined ? '' : ` file ${configPath}` | ||
error.message = `When resolving config${configMessage}:\n${error.message}` | ||
error.message = `When resolving config file ${configPath}:\n${error.message}` | ||
throw error | ||
@@ -138,0 +135,0 @@ } |
@@ -17,3 +17,3 @@ const { | ||
if (BRANCH !== undefined) { | ||
if (BRANCH) { | ||
return BRANCH | ||
@@ -20,0 +20,0 @@ } |
@@ -6,2 +6,5 @@ const { | ||
const pathExists = require('path-exists') | ||
const { throwError } = require('../error') | ||
const { removeFalsy } = require('../utils/remove_falsy') | ||
@@ -24,2 +27,3 @@ | ||
const optsE = removeFalsy(optsD) | ||
await checkPaths(optsE) | ||
return optsE | ||
@@ -31,4 +35,19 @@ } | ||
context: CONTEXT || 'production', | ||
baseRelDir: true, | ||
} | ||
// Verify that options point to existing paths | ||
const checkPaths = async function(opts) { | ||
await Promise.all(PATH_NAMES.map(pathName => checkPath(opts, pathName))) | ||
} | ||
const PATH_NAMES = ['cwd', 'repositoryRoot'] | ||
const checkPath = async function(opts, pathName) { | ||
const path = opts[pathName] | ||
if (!(await pathExists(path))) { | ||
throwError(`Option '${pathName}' points to a non-existing file`) | ||
} | ||
} | ||
module.exports = { normalizeOpts } |
@@ -1,6 +0,9 @@ | ||
const { resolve } = require('path') | ||
const { resolve, basename } = require('path') | ||
const findUp = require('find-up') | ||
const locatePath = require('locate-path') | ||
const pathExists = require('path-exists') | ||
const pFilter = require('p-filter') | ||
const { throwError } = require('./error') | ||
// Configuration location can be: | ||
@@ -34,5 +37,11 @@ // - a local path with the --config CLI flag | ||
const searchConfigFile = async function(cwd) { | ||
const filenames = FILENAMES.map(filename => resolve(cwd, filename)) | ||
const configPath = await locatePath(filenames) | ||
return configPath | ||
const paths = FILENAMES.map(filename => resolve(cwd, filename)) | ||
const pathsA = await pFilter(paths, pathExists) | ||
if (pathsA.length > 1) { | ||
const filenames = pathsA.map(path => basename(path)).join(', ') | ||
throwError(`Should use only one configuration file (among ${filenames}) in:\n${cwd}`) | ||
} | ||
return pathsA[0] | ||
} | ||
@@ -39,0 +48,0 @@ |
@@ -9,5 +9,5 @@ const filterObj = require('filter-obj') | ||
const isDefined = function(key, value) { | ||
return Boolean(value) | ||
return value !== undefined && value !== '' | ||
} | ||
module.exports = { removeFalsy } |
41138
1098
3
+ Addedp-filter@^2.1.0
+ Addedp-filter@2.1.0(transitive)
+ Addedp-map@2.1.0(transitive)
- Removedlocate-path@^5.0.0