@chidchanun/bcp 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {
4
+ pathToFileURL,
5
+ } from "node:url";
6
+
7
+ import {
8
+ assertEnvironmentSchema,
9
+ type BcpEnvironmentSchema,
10
+ } from "./environment-schema.js";
11
+
12
+ const ENVIRONMENT_SCHEMA_FILES = [
13
+ "bcp.environment.ts",
14
+ "bcp.environment.mts",
15
+ "bcp.environment.js",
16
+ "bcp.environment.mjs",
17
+ ] as const;
18
+
19
+ export interface LoadedBcpEnvironmentSchema {
20
+ file: string | null;
21
+ schema: BcpEnvironmentSchema;
22
+ }
23
+
24
+ export function getEnvironmentSchemaFileNames(): string[] {
25
+ return [
26
+ ...ENVIRONMENT_SCHEMA_FILES,
27
+ ];
28
+ }
29
+
30
+ export async function loadBcpEnvironmentSchema(
31
+ rootDirectory: string
32
+ ): Promise<LoadedBcpEnvironmentSchema> {
33
+ const matches =
34
+ ENVIRONMENT_SCHEMA_FILES
35
+ .map(
36
+ (fileName) =>
37
+ path.join(
38
+ rootDirectory,
39
+ fileName
40
+ )
41
+ )
42
+ .filter(
43
+ (filePath) =>
44
+ fs.existsSync(
45
+ filePath
46
+ )
47
+ );
48
+
49
+ if (
50
+ matches.length > 1
51
+ ) {
52
+ throw new Error(
53
+ `BCP Framework: multiple environment schema files found: ${matches.map((file) => path.basename(file)).join(", ")}. Keep only one bcp.environment file.`
54
+ );
55
+ }
56
+
57
+ if (
58
+ matches.length === 0
59
+ ) {
60
+ return {
61
+ file: null,
62
+ schema: {},
63
+ };
64
+ }
65
+
66
+ const file =
67
+ matches[0];
68
+ const url =
69
+ pathToFileURL(
70
+ file
71
+ );
72
+
73
+ url.searchParams.set(
74
+ "bcp-environment",
75
+ `${Date.now()}-${Math.random()}`
76
+ );
77
+
78
+ const module =
79
+ await import(
80
+ url.href
81
+ );
82
+ const schema =
83
+ module.default;
84
+
85
+ if (
86
+ schema === undefined
87
+ ) {
88
+ throw new Error(
89
+ `BCP Framework: ${path.basename(file)} must export a default environment schema.`
90
+ );
91
+ }
92
+
93
+ assertEnvironmentSchema(
94
+ schema
95
+ );
96
+
97
+ return {
98
+ file,
99
+ schema,
100
+ };
101
+ }