-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,42 @@ | ||
import { stat } from 'fs/promises' | ||
import { resolve } from 'path' | ||
|
||
const main = async function (definition) { | ||
const normalize = async function (definition) { | ||
await validateCwd(definition) | ||
const cwd = resolve(definition) | ||
return { info: { cwd } } | ||
return resolve(definition) | ||
} | ||
|
||
// Errors in `cwd` are not user errors, i.e. should not start with `must` | ||
const validateCwd = async function (cwd) { | ||
if (typeof cwd !== 'string') { | ||
const validateCwd = async function (definition) { | ||
if (typeof definition !== 'string') { | ||
throw new TypeError('It must be a string.') | ||
} | ||
|
||
const cwdStat = await getStat(cwd) | ||
const cwdStat = await getStat(definition) | ||
|
||
if (!cwdStat.isDirectory()) { | ||
throw new Error('It must be a directory.') | ||
} | ||
} | ||
|
||
const getStat = async function (cwd) { | ||
const getStat = async function (definition) { | ||
try { | ||
return await stat(cwd) | ||
return await stat(definition) | ||
} catch (error) { | ||
throw error.code === 'ENOENT' | ||
? new Error('It must be an existing file') | ||
: error | ||
} | ||
} | ||
|
||
const main = function (definition) { | ||
return { info: { cwd: definition } } | ||
} | ||
|
||
// A `cwd[(info)]` rule can be specified to customize `info.cwd`. | ||
// eslint-disable-next-line import/no-default-export | ||
export default { | ||
name: 'cwd', | ||
undefinedInput: true, | ||
normalize, | ||
main, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters