diff --git a/.attw.json b/.attw.json new file mode 100644 index 0000000..de5330c --- /dev/null +++ b/.attw.json @@ -0,0 +1,3 @@ +{ + "ignoreRules": ["no-resolution"] +} diff --git a/package.json b/package.json index 49f48cc..5a40ec6 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,20 @@ "types": "./artifacts/dist/esm/index.d.ts", "default": "./artifacts/dist/esm/index.js" } + }, + "./declare": { + "import": { + "types": "./artifacts/dist/esm/declare.d.ts", + "default": "./artifacts/dist/esm/declare.js" + }, + "require": { + "types": "./artifacts/dist/cjs/declare.d.ts", + "default": "./artifacts/dist/cjs/declare.js" + }, + "default": { + "types": "./artifacts/dist/esm/declare.d.ts", + "default": "./artifacts/dist/esm/declare.js" + } } }, "sideEffects": false, diff --git a/src/environment.ts b/src/environment.ts index 60c49be..f55bd67 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -1,10 +1,3 @@ -import { EOL } from "os"; -import { - render as renderSpecification, - type MarkdownPrettyPrintType, -} from "./specification.js"; -import { render as renderSummary } from "./summary.js"; -import { Results, validate } from "./validation.js"; import { createVariableComposite, type VariableComposite, @@ -12,38 +5,7 @@ import { } from "./variable-composite.js"; import { Variable, VariableSpec, createVariable } from "./variable.js"; -let state: State = createInitialState(); - -export type InitializeOptions = { - readonly onInvalid?: OnInvalid; - readonly markdownPrettyPrint?: MarkdownPrettyPrintType; -}; - -export async function initialize( - options: InitializeOptions = {}, -): Promise { - if (process.env.AUSTENITE_SPEC === "true") { - const { markdownPrettyPrint = "prettier" } = options; - console.log( - await renderSpecification(markdownPrettyPrint, variablesByName()), - ); - - // eslint-disable-next-line n/no-process-exit - process.exit(0); - } else { - const { onInvalid = defaultOnInvalid } = options; - const [isValid, results] = validate(variablesByName(), state.composites); - - if (!isValid) { - onInvalid({ - results, - defaultHandler() { - defaultOnInvalid({ results }); - }, - }); - } - } -} +export let state: State = createInitialState(); export function registerVariable(spec: VariableSpec): Variable { const variable = createVariable(spec); @@ -88,28 +50,10 @@ function createInitialState(): State { }; } -function defaultOnInvalid({ results }: { results: Results }): never { - console.error( - ["Environment Variables:", "", renderSummary(results)].join(EOL), - ); - - // eslint-disable-next-line n/no-process-exit - process.exit(1); - - return undefined as never; -} - -function variablesByName(): Variable[] { +export function variablesByName(): Variable[] { return Object.values(state.variables).sort(compareVariableNames); } function compareVariableNames(a: Variable, b: Variable) { return a.spec.name.localeCompare(b.spec.name); } - -export type OnInvalid = (args: OnInvalidArgs) => void; - -type OnInvalidArgs = { - readonly results: Results; - readonly defaultHandler: () => never; -}; diff --git a/src/index.ts b/src/index.ts index ab83812..c6c19b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { initialize } from "./environment.js"; -export type { OnInvalid } from "./environment.js"; +export { initialize } from "./initialize.js"; +export type { OnInvalid } from "./initialize.js"; diff --git a/src/initialize.ts b/src/initialize.ts new file mode 100644 index 0000000..203f8c0 --- /dev/null +++ b/src/initialize.ts @@ -0,0 +1,57 @@ +import { EOL } from "os"; +import { state, variablesByName } from "./environment.js"; +import { + render as renderSpecification, + type MarkdownPrettyPrintType, +} from "./specification.js"; +import { render as renderSummary } from "./summary.js"; +import { validate, type Results } from "./validation.js"; + +export async function initialize( + options: InitializeOptions = {}, +): Promise { + if (process.env.AUSTENITE_SPEC === "true") { + const { markdownPrettyPrint = "prettier" } = options; + console.log( + await renderSpecification(markdownPrettyPrint, variablesByName()), + ); + + // eslint-disable-next-line n/no-process-exit + process.exit(0); + } else { + const { onInvalid = defaultOnInvalid } = options; + const [isValid, results] = validate(variablesByName(), state.composites); + + if (!isValid) { + onInvalid({ + results, + defaultHandler() { + defaultOnInvalid({ results }); + }, + }); + } + } +} + +export function defaultOnInvalid({ results }: { results: Results }): never { + console.error( + ["Environment Variables:", "", renderSummary(results)].join(EOL), + ); + + // eslint-disable-next-line n/no-process-exit + process.exit(1); + + return undefined as never; +} + +export type InitializeOptions = { + readonly onInvalid?: OnInvalid; + readonly markdownPrettyPrint?: MarkdownPrettyPrintType; +}; + +export type OnInvalid = (args: OnInvalidArgs) => void; + +type OnInvalidArgs = { + readonly results: Results; + readonly defaultHandler: () => never; +};