UNPKG

1.4 kBJavaScriptView Raw
1'use strict'
2
3const { resolve } = require('path')
4
5const findUp = require('find-up')
6const pLocate = require('p-locate')
7const pathExists = require('path-exists')
8
9// Configuration location can be:
10// - a local path with the --config CLI flag
11// - a `netlify.*` file in the `repositoryRoot/{base}`
12// - a `netlify.*` file in the `repositoryRoot`
13// - a `netlify.*` file in the current directory or any parent
14const getConfigPath = async function ({ configOpt, cwd, repositoryRoot, configBase }) {
15 const configPath = await pLocate(
16 [
17 searchConfigOpt(cwd, configOpt),
18 searchBaseConfigFile(repositoryRoot, configBase),
19 searchConfigFile(repositoryRoot),
20 findUp(FILENAME, { cwd }),
21 ],
22 Boolean,
23 )
24 return configPath
25}
26
27// --config CLI flag
28const searchConfigOpt = function (cwd, configOpt) {
29 if (configOpt === undefined) {
30 return
31 }
32
33 return resolve(cwd, configOpt)
34}
35
36// Look for `repositoryRoot/{base}/netlify.*`
37const searchBaseConfigFile = function (repositoryRoot, configBase) {
38 if (configBase === undefined) {
39 return
40 }
41
42 return searchConfigFile(configBase)
43}
44
45// Look for several file extensions for `netlify.*`
46const searchConfigFile = async function (cwd) {
47 const path = resolve(cwd, FILENAME)
48 if (!(await pathExists(path))) {
49 return
50 }
51 return path
52}
53
54const FILENAME = 'netlify.toml'
55
56module.exports = { getConfigPath }