@deessejs/collections 0.0.42 → 0.0.46

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.
@@ -2,4 +2,3 @@ import { Config } from "./types";
2
2
  export declare const defineConfig: (config: Config) => import("drizzle-orm/node-postgres").NodePgDatabase<Record<string, never>> & {
3
3
  $client: import("pg").Pool;
4
4
  };
5
- export declare const waitConfig: () => Promise<Config>;
@@ -1,18 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.waitConfig = exports.defineConfig = void 0;
3
+ exports.defineConfig = void 0;
4
+ // src/core/index.ts (ou là où est défini defineConfig)
4
5
  const node_postgres_1 = require("drizzle-orm/node-postgres");
5
- let _config;
6
6
  const defineConfig = (config) => {
7
- _config = config;
8
7
  const db = (0, node_postgres_1.drizzle)(config.databaseUrl);
8
+ // Astuce : on attache la config brute à l'objet retourné.
9
+ // Cela permet au worker de lire "db._config.collections" plus tard.
10
+ // On le type en "any" ou on étend le type de retour pour éviter les erreurs TS.
11
+ db._config = config;
9
12
  return db;
10
13
  };
11
14
  exports.defineConfig = defineConfig;
12
- const waitConfig = async () => {
13
- while (!_config) {
14
- await new Promise((r) => setTimeout(r, 50));
15
- }
16
- return _config;
17
- };
18
- exports.waitConfig = waitConfig;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.withCollections = void 0;
7
+ // src/next/index.ts (votre withCollections)
7
8
  const child_process_1 = require("child_process");
8
9
  const constants_1 = require("next/constants");
9
10
  const path_1 = __importDefault(require("path"));
@@ -11,14 +12,32 @@ const withCollections = (phase, config) => {
11
12
  const isDev = phase === constants_1.PHASE_DEVELOPMENT_SERVER;
12
13
  if (isDev && !global.__collections_worker_started) {
13
14
  global.__collections_worker_started = true;
14
- const workerPath = path_1.default.join(__dirname, "../worker/index.js");
15
- console.log("[withCollections] Spawning background worker:", workerPath);
15
+ // Attention : Assurez-vous que ce chemin pointe vers le fichier JS compilé de votre lib
16
+ // Si vous êtes en monorepo/dev local sur la lib, pointez vers le TS avec ts-node ou bun
17
+ const workerPath = path_1.default.resolve(__dirname, "../worker/index.js");
18
+ console.log("[Deesse] Spawning background worker:", workerPath);
16
19
  (0, child_process_1.spawn)("node", [workerPath], {
17
20
  stdio: "inherit",
21
+ env: {
22
+ ...process.env,
23
+ // Variables d'env utiles pour le worker
24
+ NODE_ENV: "development",
25
+ }
18
26
  });
19
27
  }
28
+ // On ajoute un alias webpack pour que l'utilisateur puisse importer
29
+ // le schéma généré facilement s'il en a besoin,
30
+ // ou pour que Drizzle trouve le fichier généré.
20
31
  return {
21
32
  ...config,
33
+ webpack: (webpackConfig, options) => {
34
+ if (config.webpack) {
35
+ webpackConfig = config.webpack(webpackConfig, options);
36
+ }
37
+ // Exemple : alias pour accéder au dossier shadow facilement
38
+ webpackConfig.resolve.alias["@deesse/schema"] = path_1.default.join(process.cwd(), ".deesse/shadow/schema");
39
+ return webpackConfig;
40
+ }
22
41
  };
23
42
  };
24
43
  exports.withCollections = withCollections;
@@ -1,2 +1 @@
1
- import { Collection } from "../collections/types";
2
- export declare const loadCollections: () => Promise<Collection[]>;
1
+ export {};
@@ -1,72 +1,54 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
4
  };
38
5
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.loadCollections = void 0;
40
- const fs_1 = __importDefault(require("fs"));
6
+ // src/worker/index.ts
7
+ const jiti_1 = require("jiti");
41
8
  const path_1 = __importDefault(require("path"));
42
- const generate_1 = require("../drizzle/generate");
43
- // Dossier des collections
44
- const collectionsDir = path_1.default.join(process.cwd(), "src", "collections");
45
- console.log(`[worker] Loading collections from ${collectionsDir}`);
46
- // Fonction pour importer toutes les collections
47
- const loadCollections = async () => {
48
- const files = fs_1.default
49
- .readdirSync(collectionsDir)
50
- .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
51
- const collections = [];
52
- for (const file of files) {
53
- const fullPath = path_1.default.join(collectionsDir, file);
54
- // Utilisation de import dynamique
55
- const module = await Promise.resolve(`${fullPath}`).then(s => __importStar(require(s)));
56
- // On suppose que la collection exporte toujours une const (ex: Posts)
57
- for (const exported of Object.values(module)) {
58
- const col = exported;
59
- if (col?.slug && col?.fields) {
60
- collections.push(col);
61
- }
9
+ const chokidar_1 = __importDefault(require("chokidar"));
10
+ const generate_1 = require("../drizzle/generate"); // Votre fonction de génération existante
11
+ const PROJECT_ROOT = process.cwd();
12
+ const CONFIG_PATH = path_1.default.join(PROJECT_ROOT, "src", "api", "index.ts");
13
+ const COLLECTIONS_DIR = path_1.default.join(PROJECT_ROOT, "src", "collections");
14
+ // Initialiser Jiti pour lire du TS directement
15
+ const jiti = (0, jiti_1.createJiti)(PROJECT_ROOT, {
16
+ interopDefault: true,
17
+ requireCache: false, // Important pour recharger quand le fichier change
18
+ });
19
+ async function runGenerator() {
20
+ try {
21
+ console.log("[Deesse] 🛠️ Generating schema...");
22
+ // 1. On vide le cache de jiti pour être sûr d'avoir la dernière version
23
+ // jiti gère le cache automatiquement mais on peut forcer si besoin selon la version
24
+ // 2. On charge le fichier de config utilisateur
25
+ // Jiti va compiler le TS à la volée.
26
+ const mod = await jiti.import(CONFIG_PATH);
27
+ // 3. On récupère la config
28
+ // Note : Si l'utilisateur fait "export const db = ...", mod.db sera l'instance
29
+ // Si l'utilisateur fait "export default ...", ce sera mod.default
30
+ const dbInstance = mod.db || mod.default;
31
+ if (!dbInstance || !dbInstance._config) {
32
+ throw new Error("[Deesse] Could not find exported 'db' or '_config' in src/api/index.ts");
62
33
  }
34
+ const collections = dbInstance._config.collections;
35
+ // 4. On appelle votre fonction existante
36
+ (0, generate_1.generateShadowSchema)(collections);
37
+ console.log("[Deesse] ✅ Schema updated");
63
38
  }
64
- return collections;
65
- };
66
- exports.loadCollections = loadCollections;
67
- // Exemple d'utilisation
68
- (async () => {
69
- const collections = await (0, exports.loadCollections)();
70
- (0, generate_1.generateShadowSchema)(collections);
71
- console.log(`[worker] Shadow schema generated for ${collections.length} collections.`);
72
- })();
39
+ catch (error) {
40
+ console.error("[Deesse] ❌ Error generating schema:", error);
41
+ }
42
+ }
43
+ // Lancement initial
44
+ runGenerator();
45
+ // Mode Watch
46
+ console.log(`[Deesse] 👀 Watching for changes in ${COLLECTIONS_DIR}...`);
47
+ const watcher = chokidar_1.default.watch([CONFIG_PATH, COLLECTIONS_DIR], {
48
+ ignoreInitial: true,
49
+ ignored: /(^|[\/\\])\../, // Ignorer les fichiers cachés (dotfiles)
50
+ });
51
+ watcher.on("all", (event, path) => {
52
+ // On debounce légèrement pour éviter de générer 2 fois si l'éditeur sauvegarde en plusieurs étapes
53
+ runGenerator();
54
+ });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@deessejs/collections",
3
- "version": "0.0.42",
3
+ "version": "0.0.46",
4
4
  "description": "",
5
- "types": "dist/index.d.ts",
6
5
  "main": "dist/index.js",
7
- "module": "dist/esm/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/index.esm.js",
8
8
  "exports": {
9
9
  ".": {
10
- "import": "./dist/esm/index.js",
10
+ "import": "./dist/index.esm.js",
11
11
  "require": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts"
13
13
  }
@@ -18,6 +18,7 @@
18
18
  "LICENSE"
19
19
  ],
20
20
  "scripts": {
21
+ "build": "tsc",
21
22
  "build:watch": "tsc --watch",
22
23
  "prepublishOnly": "npm run build",
23
24
  "lint": "eslint src --ext .ts",
@@ -26,10 +27,7 @@
26
27
  "test:ui": "vitest --ui",
27
28
  "test:run": "vitest run",
28
29
  "test:coverage": "vitest run --coverage",
29
- "test:watch": "vitest watch",
30
- "build:cjs": "tsc --project tsconfig.json",
31
- "build:esm": "tsc --project tsconfig.esm.json",
32
- "build": "npm run build:cjs && npm run build:esm"
30
+ "test:watch": "vitest watch"
33
31
  },
34
32
  "repository": {
35
33
  "type": "git",
@@ -68,6 +66,7 @@
68
66
  "chokidar": "^5.0.0",
69
67
  "dotenv": "^17.2.3",
70
68
  "drizzle-orm": "^0.45.0",
69
+ "jiti": "^2.6.1",
71
70
  "pg": "^8.16.3"
72
71
  }
73
72
  }