prisma-laravel-migrate 0.2.2 → 0.2.3
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/utils/config.js +33 -31
- package/package.json +1 -1
package/dist/utils/config.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// loaders/config.ts
|
|
2
|
-
import
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { readFile } from "fs/promises";
|
|
3
4
|
import { createRequire } from "module";
|
|
4
5
|
import { extname, dirname, resolve } from "path";
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
function getNearestPkgType(fromPath) {
|
|
6
|
+
import { pathToFileURL } from "url";
|
|
7
|
+
function nearestPkgType(fromPath) {
|
|
8
8
|
try {
|
|
9
9
|
let dir = dirname(fromPath);
|
|
10
|
-
|
|
10
|
+
for (;;) {
|
|
11
11
|
const pj = resolve(dir, "package.json");
|
|
12
12
|
if (fs.existsSync(pj)) {
|
|
13
13
|
const type = JSON.parse(fs.readFileSync(pj, "utf8")).type;
|
|
@@ -22,48 +22,50 @@ function getNearestPkgType(fromPath) {
|
|
|
22
22
|
catch { }
|
|
23
23
|
return "commonjs";
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
const configCache = new Map();
|
|
25
|
+
const cache = new Map();
|
|
27
26
|
async function loadConfigUniversal(absPath) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const promise = (async () => {
|
|
27
|
+
if (cache.has(absPath))
|
|
28
|
+
return cache.get(absPath);
|
|
29
|
+
const p = (async () => {
|
|
32
30
|
const ext = extname(absPath).toLowerCase();
|
|
33
|
-
const projType =
|
|
31
|
+
const projType = nearestPkgType(absPath);
|
|
34
32
|
const asUrl = pathToFileURL(absPath).href;
|
|
35
33
|
const req = createRequire(import.meta.url);
|
|
36
|
-
// Explicit extensions
|
|
37
|
-
if (ext === ".mjs") {
|
|
38
|
-
const mod = await import(asUrl);
|
|
39
|
-
return mod.default ?? mod;
|
|
40
|
-
}
|
|
34
|
+
// Explicit extensions
|
|
41
35
|
if (ext === ".cjs") {
|
|
42
36
|
const mod = req(absPath);
|
|
43
37
|
return mod.default ?? mod;
|
|
44
38
|
}
|
|
45
|
-
|
|
46
|
-
if (projType === "module") {
|
|
47
|
-
// ESM project: load as ESM
|
|
39
|
+
if (ext === ".mjs") {
|
|
48
40
|
const mod = await import(asUrl);
|
|
49
41
|
return mod.default ?? mod;
|
|
50
42
|
}
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
// .js — ambiguous; read file to decide calmly
|
|
44
|
+
const code = await readFile(absPath, "utf8");
|
|
45
|
+
const looksCJS = /\bmodule\.exports\b|\bexports\s*=/.test(code);
|
|
46
|
+
if (projType === "commonjs" && looksCJS) {
|
|
47
|
+
// CJS project, CJS code → require
|
|
53
48
|
const mod = req(absPath);
|
|
54
49
|
return mod.default ?? mod;
|
|
55
50
|
}
|
|
51
|
+
if (projType === "module" && looksCJS) {
|
|
52
|
+
// ESM project but CJS code → wrap on the fly via data URL
|
|
53
|
+
const wrapped = `const module = { exports: {} }; const exports = module.exports;\n` +
|
|
54
|
+
code +
|
|
55
|
+
`\nexport default module.exports;`;
|
|
56
|
+
const dataUrl = "data:text/javascript;base64," +
|
|
57
|
+
Buffer.from(wrapped, "utf8").toString("base64");
|
|
58
|
+
const mod = await import(dataUrl);
|
|
59
|
+
return mod.default ?? mod;
|
|
60
|
+
}
|
|
61
|
+
// Otherwise: treat as ESM
|
|
62
|
+
const mod = await import(asUrl);
|
|
63
|
+
return mod.default ?? mod;
|
|
56
64
|
})();
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const cfg = await promise;
|
|
60
|
-
return cfg;
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
// keep it cached for this process; remove if you prefer one-shot
|
|
64
|
-
}
|
|
65
|
+
cache.set(absPath, p);
|
|
66
|
+
return p;
|
|
65
67
|
}
|
|
66
68
|
export async function loadConfig(configPath) {
|
|
67
|
-
return await loadConfigUniversal(configPath);
|
|
69
|
+
return await loadConfigUniversal(resolve(process.cwd(), configPath));
|
|
68
70
|
}
|
|
69
71
|
//# sourceMappingURL=config.js.map
|