just-kit 0.8.0 → 0.8.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.
Files changed (3) hide show
  1. package/just-kit.js +79 -6
  2. package/package.json +3 -3
  3. package/test.js +0 -22
package/just-kit.js CHANGED
@@ -1,9 +1,82 @@
1
- /**
2
- * just-kit – small collection of lightweight JavaScript utilities
3
- *
4
- * @module just-kit
5
- */
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { dirname, join } from 'path';
4
+ import { fileURLToPath } from 'url';
5
+
6
+ export function _dir(file) {
7
+ const err = new Error();
8
+ const stack = err.stack;
9
+ if (!stack) return process.cwd();
10
+
11
+ // Usually line 2 or 3 is the caller (depending on engine)
12
+ const callerLine = stack.split('\n')[2]?.trim() || '';
13
+
14
+ const match = callerLine.match(/(?:file:\/{2,3})?(.+?)(?::\d+:\d+)?$/);
15
+ if (!match) return process.cwd();
16
+
17
+ const filePath = match[1];
18
+
19
+ try {
20
+ const dir = dirname(fileURLToPath(filePath.startsWith('file:') ? filePath : `file://${filePath}`));
21
+ if (file)
22
+ return join(dir,file);
23
+ return dir;
24
+ } catch {
25
+ return process.cwd();
26
+ }
27
+ }
28
+
29
+ export async function loadModules(dirPath, BaseClass, extension = '.js') {
30
+ if (typeof BaseClass !== 'function' || !BaseClass.prototype) {
31
+ throw new Error('BaseClass parameter must be a valid class constructor');
32
+ }
33
+
34
+ const absoluteDir = path.isAbsolute(dirPath)
35
+ ? dirPath
36
+ : path.resolve(process.cwd(), dirPath);
37
+
38
+ let dirents;
39
+ try {
40
+ dirents = await fs.readdir(absoluteDir, { withFileTypes: true });
41
+ } catch (err) {
42
+ throw new Error(`Cannot read directory "${dirPath}": ${err.message}`);
43
+ }
44
+
45
+ /** @type {Function[]} */
46
+ const classes = [];
47
+
48
+ const files = dirents.filter(
49
+ entry => entry.isFile() && entry.name.endsWith(extension)
50
+ );
51
+
52
+ for (const file of files) {
53
+ const fullPath = path.join(absoluteDir, file.name);
54
+ const fileUrl = new URL(`file://${fullPath}`);
55
+
56
+ try {
57
+ const module = await import(fileUrl.href);
58
+ const Exported = module.default;
59
+
60
+ if (!Exported) continue;
61
+
62
+ if (typeof Exported !== 'function' || !Exported.prototype) continue;
63
+
64
+ // Check inheritance: is Exported instanceof BaseClass? (works for classes)
65
+ if (Exported === BaseClass || Exported.prototype instanceof BaseClass) {
66
+ classes.push(Exported);
67
+ }
68
+ // Alternative stricter check (sometimes preferred):
69
+ // if (Object.isPrototypeOf.call(BaseClass.prototype, Exported.prototype)) { ... }
70
+
71
+ } catch (err) {
72
+ console.error(`Failed to load ${file.name}: ${err.message}`);
73
+ }
74
+ }
75
+
76
+ return classes;
77
+ }
78
+
6
79
 
7
80
  export { default as Next } from './just-next.js';
8
81
  export { default as JustPool } from './just-pool.js';
9
- export { default as Prompt } from './just-prompt.js';
82
+ export { default as Prompt } from './just-prompt.js';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "just-kit",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "simple nodejs utility",
5
5
  "keywords": [
6
6
  "just",
7
7
  "just-kit",
8
8
  "prompt",
9
- "next",
10
- "pool"
9
+ "next",
10
+ "pool"
11
11
  ],
12
12
  "homepage": "https://github.com/just-node/just-kit#readme",
13
13
  "bugs": {
package/test.js DELETED
@@ -1,22 +0,0 @@
1
- import { Next, JustPool, Prompt } from './just-kit.js';
2
-
3
- // 1. Cycling list
4
- const playlist = new Next(['song1.mp3', 'song2.mp3', 'song3.mp3'], 'random');
5
- console.log(playlist.next()); // → random song
6
- console.log(playlist.peek()); // → another (or same) random song
7
-
8
- // 2. Lazy pool
9
- const dbPool = new JustPool((dbName) => "hello: "+dbName);
10
- const db1 = dbPool.get('users'); // created once
11
- const db2 = dbPool.get('users'); // same instance
12
-
13
-
14
-
15
-
16
- Prompt.app(
17
- 'Type your message (type "exit" to quit): ',
18
- 'exit',
19
- async (input) => {
20
- console.log(`You said: ${input}`);
21
- }
22
- ).start();