dotenv-gad 1.2.0 → 1.2.1
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.
- package/dist/cli/commands/utils.js +19 -12
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, unlinkSync } from "fs";
|
|
2
|
-
import { dirname, join } from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
1
|
+
import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs";
|
|
2
|
+
import { dirname, join, resolve } from "path";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
4
4
|
import { transformSync } from "esbuild";
|
|
5
5
|
import Chalk from "chalk";
|
|
6
6
|
import inquirer from "inquirer";
|
|
@@ -12,8 +12,13 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
12
12
|
* @throws If the schema file is malformed or cannot be loaded.
|
|
13
13
|
*/
|
|
14
14
|
export async function loadSchema(schemaPath) {
|
|
15
|
+
const absPath = resolve(schemaPath);
|
|
16
|
+
const importModule = async (filePath) => {
|
|
17
|
+
const fileUrl = pathToFileURL(filePath).href;
|
|
18
|
+
return (await import(`${fileUrl}?t=${Date.now()}`)).default;
|
|
19
|
+
};
|
|
15
20
|
const loadTsModule = async (tsFilePath) => {
|
|
16
|
-
const tempFile = join(__dirname,
|
|
21
|
+
const tempFile = join(__dirname, `../../temp-schema-${Date.now()}.mjs`);
|
|
17
22
|
try {
|
|
18
23
|
const tsCode = readFileSync(tsFilePath, "utf-8");
|
|
19
24
|
const { code } = transformSync(tsCode, {
|
|
@@ -22,21 +27,23 @@ export async function loadSchema(schemaPath) {
|
|
|
22
27
|
target: "esnext",
|
|
23
28
|
});
|
|
24
29
|
writeFileSync(tempFile, code);
|
|
25
|
-
return
|
|
30
|
+
return await importModule(tempFile);
|
|
26
31
|
}
|
|
27
32
|
finally {
|
|
28
|
-
|
|
33
|
+
if (existsSync(tempFile)) {
|
|
34
|
+
unlinkSync(tempFile);
|
|
35
|
+
}
|
|
29
36
|
}
|
|
30
37
|
};
|
|
31
38
|
try {
|
|
32
|
-
if (
|
|
33
|
-
return await loadTsModule(
|
|
39
|
+
if (absPath.endsWith(".ts")) {
|
|
40
|
+
return await loadTsModule(absPath);
|
|
34
41
|
}
|
|
35
|
-
else if (
|
|
36
|
-
return
|
|
42
|
+
else if (absPath.endsWith(".js")) {
|
|
43
|
+
return await importModule(absPath);
|
|
37
44
|
}
|
|
38
|
-
else if (
|
|
39
|
-
return JSON.parse(readFileSync(
|
|
45
|
+
else if (absPath.endsWith(".json")) {
|
|
46
|
+
return JSON.parse(readFileSync(absPath, "utf-8"));
|
|
40
47
|
}
|
|
41
48
|
throw new Error(`Unsupported schema format. Use .ts, .js or .json`);
|
|
42
49
|
}
|