Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Nov 5, 2024
1 parent 1275835 commit 4d05c55
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .attw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignoreRules": ["no-resolution"]
}
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 2 additions & 58 deletions src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,11 @@
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,
type VariableCompositeSpec,
} 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<void> {
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<T>(spec: VariableSpec<T>): Variable<T> {
const variable = createVariable(spec);
Expand Down Expand Up @@ -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<unknown>[] {
export function variablesByName(): Variable<unknown>[] {
return Object.values(state.variables).sort(compareVariableNames);
}

function compareVariableNames(a: Variable<unknown>, b: Variable<unknown>) {
return a.spec.name.localeCompare(b.spec.name);
}

export type OnInvalid = (args: OnInvalidArgs) => void;

type OnInvalidArgs = {
readonly results: Results;
readonly defaultHandler: () => never;
};
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
57 changes: 57 additions & 0 deletions src/initialize.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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;
};

0 comments on commit 4d05c55

Please sign in to comment.