@scriptdb/pmr 1.0.0
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/README.md +181 -0
- package/dist/index.js +12 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# @scriptdb/pmr
|
|
2
|
+
|
|
3
|
+
Plugins Module Resolver for the script database, providing dynamic plugin loading and resolution capabilities.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dynamic plugin loading**: Load plugins from directories at runtime
|
|
8
|
+
- **Module resolution**: Automatically resolve and import plugin modules
|
|
9
|
+
- **Plugin discovery**: Discover plugins in specified directories
|
|
10
|
+
- **Type-safe**: Full TypeScript support with type definitions
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add @scriptdb/pmr
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { PluginModuleResolver } from '@scriptdb/pmr';
|
|
22
|
+
|
|
23
|
+
// Resolve plugins from a directory
|
|
24
|
+
const plugins = await PluginModuleResolver('./plugins');
|
|
25
|
+
|
|
26
|
+
// Use the loaded plugins
|
|
27
|
+
console.log(plugins);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API Reference
|
|
31
|
+
|
|
32
|
+
### PluginModuleResolver(basePath)
|
|
33
|
+
|
|
34
|
+
Resolves and loads plugins from the specified directory.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
await PluginModuleResolver(basePath: string): Promise<any>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- `basePath` (string): The base directory path where plugins are located
|
|
41
|
+
|
|
42
|
+
Returns a promise that resolves with the loaded plugins.
|
|
43
|
+
|
|
44
|
+
## Plugin Structure
|
|
45
|
+
|
|
46
|
+
To be compatible with the Plugin Module Resolver, your plugin directory should follow this structure:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
plugins/
|
|
50
|
+
├── index.ts # Main plugin entry point
|
|
51
|
+
└── plugin-files... # Additional plugin files
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `index.ts` file should export a `plugins()` function:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// plugins/index.ts
|
|
58
|
+
export async function plugins() {
|
|
59
|
+
return {
|
|
60
|
+
// Plugin exports
|
|
61
|
+
hello: () => console.log('Hello from plugin!'),
|
|
62
|
+
add: (a: number, b: number) => a + b
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Examples
|
|
68
|
+
|
|
69
|
+
### Basic Plugin Loading
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { PluginModuleResolver } from '@scriptdb/pmr';
|
|
73
|
+
|
|
74
|
+
// Load plugins from the ./my-plugins directory
|
|
75
|
+
const plugins = await PluginModuleResolver('./my-plugins');
|
|
76
|
+
|
|
77
|
+
// Use plugin functions
|
|
78
|
+
if (plugins.hello) {
|
|
79
|
+
plugins.hello(); // "Hello from plugin!"
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (plugins.add) {
|
|
83
|
+
const sum = plugins.add(5, 3); // 8
|
|
84
|
+
console.log(sum);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Plugin with Dependencies
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// plugins/calculator/index.ts
|
|
92
|
+
export async function plugins() {
|
|
93
|
+
return {
|
|
94
|
+
add: (a: number, b: number) => a + b,
|
|
95
|
+
subtract: (a: number, b: number) => a - b,
|
|
96
|
+
multiply: (a: number, b: number) => a * b,
|
|
97
|
+
divide: (a: number, b: number) => a / b
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// main.ts
|
|
102
|
+
import { PluginModuleResolver } from '@scriptdb/pmr';
|
|
103
|
+
|
|
104
|
+
const calc = await PluginModuleResolver('./plugins/calculator');
|
|
105
|
+
|
|
106
|
+
console.log(calc.add(5, 3)); // 8
|
|
107
|
+
console.log(calc.subtract(5, 3)); // 2
|
|
108
|
+
console.log(calc.multiply(5, 3)); // 15
|
|
109
|
+
console.log(calc.divide(15, 3)); // 5
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Advanced Plugin with Configuration
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// plugins/database/index.ts
|
|
116
|
+
interface DatabaseConfig {
|
|
117
|
+
host: string;
|
|
118
|
+
port: number;
|
|
119
|
+
name: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let config: DatabaseConfig | null = null;
|
|
123
|
+
|
|
124
|
+
export async function plugins() {
|
|
125
|
+
return {
|
|
126
|
+
configure: (options: DatabaseConfig) => {
|
|
127
|
+
config = options;
|
|
128
|
+
},
|
|
129
|
+
connect: () => {
|
|
130
|
+
if (!config) {
|
|
131
|
+
throw new Error('Database not configured. Call configure() first.');
|
|
132
|
+
}
|
|
133
|
+
// Database connection logic here
|
|
134
|
+
return `Connected to ${config.host}:${config.port}/${config.name}`;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// main.ts
|
|
140
|
+
import { PluginModuleResolver } from '@scriptdb/pmr';
|
|
141
|
+
|
|
142
|
+
const db = await PluginModuleResolver('./plugins/database');
|
|
143
|
+
|
|
144
|
+
db.configure({
|
|
145
|
+
host: 'localhost',
|
|
146
|
+
port: 5432,
|
|
147
|
+
name: 'mydb'
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
console.log(db.connect()); // "Connected to localhost:5432/mydb"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
154
|
+
|
|
155
|
+
The Plugin Module Resolver will throw an error if:
|
|
156
|
+
|
|
157
|
+
- The specified directory doesn't exist
|
|
158
|
+
- The index.ts file is not found
|
|
159
|
+
- The plugins() function is not exported
|
|
160
|
+
- There are syntax errors in the plugin code
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { PluginModuleResolver } from '@scriptdb/pmr';
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
const plugins = await PluginModuleResolver('./non-existent-directory');
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error('Failed to load plugins:', error.message);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Security Considerations
|
|
173
|
+
|
|
174
|
+
- Only load plugins from trusted sources
|
|
175
|
+
- Validate plugin functionality before use
|
|
176
|
+
- Consider implementing a sandboxed environment for untrusted plugins
|
|
177
|
+
- Limit plugin access to sensitive resources
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
async function PluginModuleResolver(baseBase) {
|
|
4
|
+
const DIR = baseBase;
|
|
5
|
+
const res = await import(path.resolve(path.join(DIR, "index.ts")));
|
|
6
|
+
return await res.plugins();
|
|
7
|
+
}
|
|
8
|
+
var src_default = PluginModuleResolver;
|
|
9
|
+
export {
|
|
10
|
+
src_default as default,
|
|
11
|
+
PluginModuleResolver
|
|
12
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scriptdb/pmr",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Plugins module resolver for script database",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "bun --watch src/index.ts",
|
|
20
|
+
"build": "bun build src/index.ts --outdir dist --target node --format esm --splitting",
|
|
21
|
+
"build:cjs": "bun build src/index.ts --outdir dist --target node --format cjs --outfile dist/index.js",
|
|
22
|
+
"build:types": "tsc --emitDeclarationOnly --project tsconfig.build.json",
|
|
23
|
+
"build:all": "bun run build && bun run build:cjs && bun run build:types",
|
|
24
|
+
"test": "bun test",
|
|
25
|
+
"lint": "bun run lint:src",
|
|
26
|
+
"lint:src": "eslint src --ext .ts,.tsx",
|
|
27
|
+
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"clean": "rm -rf dist"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/bun": "^1.3.2",
|
|
33
|
+
"@types/node": "^20.0.0",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
35
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
36
|
+
"bun-types": "latest",
|
|
37
|
+
"eslint": "^8.0.0",
|
|
38
|
+
"typescript": "^5.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|