Skip to content

Commit

Permalink
Split exports for wider runtime support
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Nov 5, 2024
1 parent 91ad426 commit 49808fa
Show file tree
Hide file tree
Showing 36 changed files with 134 additions and 98 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"
}
},
"./node": {
"import": {
"types": "./artifacts/dist/esm/node.d.ts",
"default": "./artifacts/dist/esm/node.js"
},
"require": {
"types": "./artifacts/dist/cjs/node.d.ts",
"default": "./artifacts/dist/cjs/node.js"
},
"default": {
"types": "./artifacts/dist/esm/node.d.ts",
"default": "./artifacts/dist/esm/node.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;
};
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export { networkPortNumber } from "./declaration/network-port-number.js";
export { number } from "./declaration/number.js";
export { string } from "./declaration/string.js";
export { url } from "./declaration/url.js";
export { initialize } from "./environment.js";
export type { OnInvalid } from "./environment.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;
};
2 changes: 2 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { initialize } from "./initialize.js";
export type { OnInvalid } from "./initialize.js";
2 changes: 1 addition & 1 deletion test/fixture/example/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initialize } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import {
cdnUrl,
earthAtomCount,
Expand Down
3 changes: 2 additions & 1 deletion test/suite/constraint/length.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type {
DeclarationExampleOptions,
Example,
} from "../../../src/example.js";
import { binary, initialize, string } from "../../../src/index.js";
import { binary, string } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { toString } from "../../../src/schema.js";
import { noop } from "../../helpers.js";

Expand Down
2 changes: 1 addition & 1 deletion test/suite/constraint/range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import type {
import {
bigInteger,
duration,
initialize,
integer,
networkPortNumber,
number,
} from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const { Duration } = Temporal;
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/big-integer.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { bigInteger, initialize } from "../../../src/index.js";
import { bigInteger } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Big integer declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/big-integer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/big-integer.js";
import { bigInteger, initialize } from "../../../src/index.js";
import { bigInteger } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const validValueTable = [
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/binary.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Buffer } from "buffer";
import { describe, expectTypeOf, it } from "vitest";
import { binary, initialize } from "../../../src/index.js";
import { binary } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Binary declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/binary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Buffer } from "buffer";
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/binary.js";
import { binary, initialize } from "../../../src/index.js";
import { binary } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const validValueTable = [
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/boolean.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { boolean, initialize } from "../../../src/index.js";
import { boolean } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Boolean declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/boolean.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/boolean.js";
import { boolean, initialize } from "../../../src/index.js";
import { boolean } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Boolean declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/duration.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Temporal } from "@js-temporal/polyfill";
import { describe, expectTypeOf, it } from "vitest";
import { duration, initialize } from "../../../src/index.js";
import { duration } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const { Duration } = Temporal;
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/duration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Temporal } from "@js-temporal/polyfill";
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/duration.js";
import { duration, initialize } from "../../../src/index.js";
import { duration } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const { Duration } = Temporal;
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/enumeration.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { enumeration, initialize } from "../../../src/index.js";
import { enumeration } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Enumeration declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/enumeration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/enumeration.js";
import { enumeration, initialize } from "../../../src/index.js";
import { enumeration } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Enumeration declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/integer.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { initialize, integer } from "../../../src/index.js";
import { integer } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Integer declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/integer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/integer.js";
import { initialize, integer } from "../../../src/index.js";
import { integer } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const validValueTable = [
Expand Down
7 changes: 2 additions & 5 deletions test/suite/declaration/kubernetes-address.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import {
KubernetesAddress,
initialize,
kubernetesAddress,
} from "../../../src/index.js";
import { KubernetesAddress, kubernetesAddress } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Kubernetes address declarations", () => {
Expand Down
7 changes: 2 additions & 5 deletions test/suite/declaration/kubernetes-address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/kubernetes-address.js";
import {
KubernetesAddress,
initialize,
kubernetesAddress,
} from "../../../src/index.js";
import { KubernetesAddress, kubernetesAddress } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { quote } from "../../../src/shell.js";
import { noop } from "../../helpers.js";

Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/network-port-number.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { initialize, networkPortNumber } from "../../../src/index.js";
import { networkPortNumber } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Network port number declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/network-port-number.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/network-port-number.js";
import { initialize, networkPortNumber } from "../../../src/index.js";
import { networkPortNumber } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const validValueTable = [
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/number.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { initialize, number } from "../../../src/index.js";
import { number } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("Number declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/number.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/number.js";
import { initialize, number } from "../../../src/index.js";
import { number } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

const validValueTable = [
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/string.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { initialize, string } from "../../../src/index.js";
import { string } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("String declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it } from "vitest";
import { Declaration } from "../../../src/declaration.js";
import { Options } from "../../../src/declaration/string.js";
import { initialize, string } from "../../../src/index.js";
import { string } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("String declarations", () => {
Expand Down
3 changes: 2 additions & 1 deletion test/suite/declaration/url.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expectTypeOf, it } from "vitest";
import { initialize, url } from "../../../src/index.js";
import { url } from "../../../src/index.js";
import { initialize } from "../../../src/node.js";
import { noop } from "../../helpers.js";

describe("URL declarations", () => {
Expand Down
Loading

0 comments on commit 49808fa

Please sign in to comment.