UNPKG

1 kBJavaScriptView Raw
1'use strict'
2
3const { isDirectory } = require('path-type')
4
5const { throwUserError } = require('./error')
6
7// Retrieve the build directory used to resolve most paths.
8// This is (in priority order):
9// - `build.base`
10// - `--repositoryRoot`
11// - the current directory (default value of `--repositoryRoot`)
12const getBuildDir = async function (repositoryRoot, base) {
13 const buildDir = base === undefined ? repositoryRoot : base
14 await checkBuildDir(buildDir, repositoryRoot)
15 return buildDir
16}
17
18// The build directory is used as the current directory of build commands and
19// build plugins. Therefore it must exist.
20// We already check `repositoryRoot` earlier in the code, so only need to check
21// `buildDir` when it is the base directory instead.
22const checkBuildDir = async function (buildDir, repositoryRoot) {
23 if (buildDir === repositoryRoot || (await isDirectory(buildDir))) {
24 return
25 }
26
27 throwUserError(`Base directory does not exist: ${buildDir}`)
28}
29
30module.exports = { getBuildDir }