Skip to content

Commit

Permalink
Add keyword.normalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent 9fad6c7 commit 0a7cd18
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/config/normalize/lib/keywords/list/cwd.js
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,
}
17 changes: 16 additions & 1 deletion src/config/normalize/lib/keywords/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { shouldSkipKeyword, shouldSkipMain } from './skip.js'
// - `name` `{string}` (required): property name in rules
// - `aliases` `{string[]}`: alias property names
// - `main` `function`: main function
// - `normalize` `(definition) => definition`: normalize and validate the
// `definition`
// - `test` `(value, info) => boolean`: skip the keyword when returning `false`
// - `hasInput` `boolean` (default: false): pass `input` to `main()`
// as a second argument
Expand Down Expand Up @@ -76,6 +78,7 @@ const applyKeyword = async function ({
hasInput = false,
undefinedInput = false,
undefinedDefinition = false,
normalize,
main,
},
state,
Expand All @@ -99,8 +102,9 @@ const applyKeyword = async function ({
return state
}

const definitionB = await normalizeDefinition(definitionA, normalize, info)
const returnValue = await callFunc({
func: main.bind(undefined, definitionA),
func: main.bind(undefined, definitionB),
input,
info,
hasInput,
Expand All @@ -123,3 +127,14 @@ const getDefinition = function (rule, name, aliases) {
const aliasA = aliases.find((alias) => rule[alias] !== undefined)
return aliasA === undefined ? undefined : rule[aliasA]
}

// Apply `keyword.normalize()`
const normalizeDefinition = async function (definition, normalize, info) {
return normalize === undefined
? definition
: await callFunc({
func: () => normalize(definition),
info,
hasInput: false,
})
}

0 comments on commit 0a7cd18

Please sign in to comment.