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.
@@ -1,13 +1,13 @@
1
1
  // loaders/config.ts
2
- import { pathToFileURL } from "url";
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 fs from "fs";
6
- /** find nearest package.json and read its "type" (defaults to "commonjs") */
7
- function getNearestPkgType(fromPath) {
6
+ import { pathToFileURL } from "url";
7
+ function nearestPkgType(fromPath) {
8
8
  try {
9
9
  let dir = dirname(fromPath);
10
- while (true) {
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
- // simple singleton cache to avoid double-loading the same file concurrently
26
- const configCache = new Map();
25
+ const cache = new Map();
27
26
  async function loadConfigUniversal(absPath) {
28
- const key = absPath;
29
- if (configCache.has(key))
30
- return configCache.get(key);
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 = getNearestPkgType(absPath);
31
+ const projType = nearestPkgType(absPath);
34
32
  const asUrl = pathToFileURL(absPath).href;
35
33
  const req = createRequire(import.meta.url);
36
- // Explicit extensions win:
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
- // .js is ambiguous: respect package.json "type"
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
- else {
52
- // CJS project: load via require
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
- configCache.set(key, promise);
58
- try {
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); // works for CJS or ESM
69
+ return await loadConfigUniversal(resolve(process.cwd(), configPath));
68
70
  }
69
71
  //# sourceMappingURL=config.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-laravel-migrate",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Generate laravel migrations and/or models using prisma files",
5
5
  "bin": {
6
6
  "prisma-laravel-migrations": "./dist/cli/migrator.index.js",