@scriptdb/storage 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 +375 -0
- package/dist/index.js +5440 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# @scriptdb/storage
|
|
2
|
+
|
|
3
|
+
Storage module for the script database, providing a Git-based file system with full version control capabilities.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Git-based storage**: Complete version control for all files
|
|
8
|
+
- **File operations**: Add, update, delete, and retrieve files
|
|
9
|
+
- **Repository management**: Initialize, configure, and manage Git repositories
|
|
10
|
+
- **Remote operations**: Push, pull, fetch, and clone repositories
|
|
11
|
+
- **Configuration management**: Full Git configuration support
|
|
12
|
+
- **Cross-platform compatibility**: Works on Windows, macOS, and Linux
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bun add @scriptdb/storage
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Storage } from '@scriptdb/storage';
|
|
24
|
+
|
|
25
|
+
// Initialize storage
|
|
26
|
+
const storage = new Storage('./my-repo');
|
|
27
|
+
await storage.initialize();
|
|
28
|
+
|
|
29
|
+
// Add a file
|
|
30
|
+
await storage.addFile('hello.txt', 'Hello, World!');
|
|
31
|
+
|
|
32
|
+
// Commit changes
|
|
33
|
+
await storage.commit('Initial commit');
|
|
34
|
+
|
|
35
|
+
// Get file content
|
|
36
|
+
const file = await storage.getFile('hello.txt');
|
|
37
|
+
console.log(file.content); // "Hello, World!"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API Reference
|
|
41
|
+
|
|
42
|
+
### Constructor
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
new Storage(repoPath: string)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Creates a new storage instance with the specified repository path.
|
|
49
|
+
|
|
50
|
+
### Methods
|
|
51
|
+
|
|
52
|
+
#### initialize(repoPath?)
|
|
53
|
+
|
|
54
|
+
Initializes a Git repository at the specified path.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
await storage.initialize(repoPath?: string): Promise<SimpleGit>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### File Operations
|
|
61
|
+
|
|
62
|
+
##### addFile(filePath, content)
|
|
63
|
+
|
|
64
|
+
Adds a new file to the repository.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
await storage.addFile(filePath: string, content: string): Promise<{ success: boolean; message: string; path?: string }>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
##### updateFile(filePath, content)
|
|
71
|
+
|
|
72
|
+
Updates an existing file.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
await storage.updateFile(filePath: string, content: string): Promise<{ success: boolean; message: string; path?: string }>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
##### getFile(filePath)
|
|
79
|
+
|
|
80
|
+
Retrieves file content.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
await storage.getFile(filePath: string): Promise<{ success: boolean; content?: string; path?: string; message?: string }>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
##### deleteFile(filePath)
|
|
87
|
+
|
|
88
|
+
Deletes a file from the repository.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
await storage.deleteFile(filePath: string): Promise<{ success: boolean; message: string }>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Repository Operations
|
|
95
|
+
|
|
96
|
+
##### commit(message)
|
|
97
|
+
|
|
98
|
+
Commits staged changes.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
await storage.commit(message: string): Promise<{ success: boolean; message: string }>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
##### getStatus()
|
|
105
|
+
|
|
106
|
+
Gets the current repository status.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
await storage.getStatus(): Promise<RepoStatus>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
##### getHistory(filePath)
|
|
113
|
+
|
|
114
|
+
Gets commit history for a file.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
await storage.getHistory(filePath: string): Promise<CommitHistory[]>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
##### listFiles()
|
|
121
|
+
|
|
122
|
+
Lists all files in the repository.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
await storage.listFiles(): Promise<string[]>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Git Configuration
|
|
129
|
+
|
|
130
|
+
##### setConfig(config, scope?)
|
|
131
|
+
|
|
132
|
+
Sets Git configuration values.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
await storage.setConfig(config: GitConfig, scope?: 'local' | 'global' | 'system'): Promise<{ success: boolean; message: string; changes?: any[] }>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
##### getConfig(key, scope?)
|
|
139
|
+
|
|
140
|
+
Gets a specific Git configuration value.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
await storage.getConfig(key: string, scope?: 'local' | 'global' | 'system'): Promise<{ success: boolean; key?: string; value?: string; scope?: string; message?: string }>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
##### listConfig(scope?)
|
|
147
|
+
|
|
148
|
+
Lists all Git configuration values.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
await storage.listConfig(scope?: 'local' | 'global' | 'system'): Promise<{ success: boolean; scope: string; config?: any; message?: string }>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
##### setupBasicConfig(userName?, userEmail?, additionalConfig?)
|
|
155
|
+
|
|
156
|
+
Sets up basic Git configuration.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
await storage.setupBasicConfig(userName?: string, userEmail?: string, additionalConfig?: Record<string, string>): Promise<{ success: boolean; message: string }>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
#### Remote Operations
|
|
163
|
+
|
|
164
|
+
##### addRemote(name, url)
|
|
165
|
+
|
|
166
|
+
Adds a remote repository.
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
await storage.addRemote(name: string, url: string): Promise<{ success: boolean; message: string; name?: string; url?: string }>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
##### removeRemote(name)
|
|
173
|
+
|
|
174
|
+
Removes a remote repository.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
await storage.removeRemote(name: string): Promise<{ success: boolean; message: string; name?: string }>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
##### listRemotes()
|
|
181
|
+
|
|
182
|
+
Lists all remote repositories.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
await storage.listRemotes(): Promise<{ success: boolean; remotes: RemoteInfo[]; message?: string }>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
##### push(remote?, branch?, options?)
|
|
189
|
+
|
|
190
|
+
Pushes changes to a remote repository.
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
await storage.push(remote?: string, branch?: string, options?: { force?: boolean; setUpstream?: boolean }): Promise<{ success: boolean; message: string; remote?: string; branch?: string }>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
##### pull(remote?, branch?, options?)
|
|
197
|
+
|
|
198
|
+
Pulls changes from a remote repository.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
await storage.pull(remote?: string, branch?: string, options?: { allowUnrelatedHistories?: boolean }): Promise<{ success: boolean; message: string; remote?: string; branch?: string }>
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
##### fetch(remote?, branch?, options?)
|
|
205
|
+
|
|
206
|
+
Fetches changes from a remote repository without merging.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
await storage.fetch(remote?: string, branch?: string, options?: { prune?: boolean }): Promise<{ success: boolean; message: string; remote?: string; branch?: string }>
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
##### clone(url, targetPath?, options?)
|
|
213
|
+
|
|
214
|
+
Clones a remote repository.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
await storage.clone(url: string, targetPath?: string, options?: { bare?: boolean; branch?: string; depth?: number }): Promise<{ success: boolean; message: string; url?: string; path?: string }>
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Types
|
|
221
|
+
|
|
222
|
+
### GitConfig
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
interface GitConfig {
|
|
226
|
+
userName?: string;
|
|
227
|
+
userEmail?: string;
|
|
228
|
+
options?: Record<string, string>;
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### RepoStatus
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
interface RepoStatus {
|
|
236
|
+
staged: string[];
|
|
237
|
+
unstaged: string[];
|
|
238
|
+
untracked: string[];
|
|
239
|
+
clean: boolean;
|
|
240
|
+
error?: string;
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### RemoteInfo
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
interface RemoteInfo {
|
|
248
|
+
name: string;
|
|
249
|
+
refs: string;
|
|
250
|
+
pushUrl: string;
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### CommitHistory
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
interface CommitHistory {
|
|
258
|
+
hash: string;
|
|
259
|
+
message: string;
|
|
260
|
+
date: string;
|
|
261
|
+
author: string;
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Examples
|
|
266
|
+
|
|
267
|
+
### Basic Usage
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
import { Storage } from '@scriptdb/storage';
|
|
271
|
+
|
|
272
|
+
// Create storage instance
|
|
273
|
+
const storage = new Storage('./my-project');
|
|
274
|
+
await storage.initialize();
|
|
275
|
+
|
|
276
|
+
// Configure user
|
|
277
|
+
await storage.setupBasicConfig('John Doe', 'john@example.com');
|
|
278
|
+
|
|
279
|
+
// Add files
|
|
280
|
+
await storage.addFile('README.md', '# My Project');
|
|
281
|
+
await storage.addFile('src/index.ts', 'console.log("Hello, World!");');
|
|
282
|
+
|
|
283
|
+
// Commit changes
|
|
284
|
+
await storage.commit('Initial commit');
|
|
285
|
+
|
|
286
|
+
// Update a file
|
|
287
|
+
await storage.updateFile('README.md', '# My Project\n\nA sample project.');
|
|
288
|
+
await storage.commit('Update README');
|
|
289
|
+
|
|
290
|
+
// Get file content
|
|
291
|
+
const readme = await storage.getFile('README.md');
|
|
292
|
+
console.log(readme.content);
|
|
293
|
+
|
|
294
|
+
// Get repository status
|
|
295
|
+
const status = await storage.getStatus();
|
|
296
|
+
console.log(status);
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Working with Remotes
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { Storage } from '@scriptdb/storage';
|
|
303
|
+
|
|
304
|
+
// Initialize storage
|
|
305
|
+
const storage = new Storage('./my-project');
|
|
306
|
+
await storage.initialize();
|
|
307
|
+
|
|
308
|
+
// Add remote
|
|
309
|
+
await storage.addRemote('origin', 'https://github.com/username/my-project.git');
|
|
310
|
+
|
|
311
|
+
// Push changes
|
|
312
|
+
await storage.push('origin', 'main');
|
|
313
|
+
|
|
314
|
+
// Pull changes
|
|
315
|
+
await storage.pull('origin', 'main');
|
|
316
|
+
|
|
317
|
+
// Fetch changes without merging
|
|
318
|
+
await storage.fetch('origin', 'main');
|
|
319
|
+
|
|
320
|
+
// Clone a repository
|
|
321
|
+
const newStorage = new Storage('./cloned-project');
|
|
322
|
+
await newStorage.clone('https://github.com/username/another-project.git');
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Configuration Management
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
import { Storage } from '@scriptdb/storage';
|
|
329
|
+
|
|
330
|
+
const storage = new Storage('./my-project');
|
|
331
|
+
await storage.initialize();
|
|
332
|
+
|
|
333
|
+
// Set user configuration
|
|
334
|
+
await storage.setConfig({
|
|
335
|
+
userName: 'Jane Doe',
|
|
336
|
+
userEmail: 'jane@example.com',
|
|
337
|
+
options: {
|
|
338
|
+
'core.autocrlf': 'false',
|
|
339
|
+
'pull.rebase': 'true'
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Get configuration
|
|
344
|
+
const userName = await storage.getConfig('user.name');
|
|
345
|
+
console.log(userName.value); // "Jane Doe"
|
|
346
|
+
|
|
347
|
+
// List all configuration
|
|
348
|
+
const config = await storage.listConfig();
|
|
349
|
+
console.log(config.config);
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Error Handling
|
|
353
|
+
|
|
354
|
+
All methods return a result object with a `success` property indicating if the operation was successful. If `success` is `false`, the `message` property contains information about the error.
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
const result = await storage.addFile('test.txt', 'content');
|
|
358
|
+
|
|
359
|
+
if (result.success) {
|
|
360
|
+
console.log(`File added at ${result.path}`);
|
|
361
|
+
} else {
|
|
362
|
+
console.error(`Error: ${result.message}`);
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Security Considerations
|
|
367
|
+
|
|
368
|
+
- Ensure proper file system permissions for the repository path
|
|
369
|
+
- Validate file paths and content to prevent directory traversal attacks
|
|
370
|
+
- Use secure authentication methods for remote operations
|
|
371
|
+
- Consider encrypting sensitive data before storing it
|
|
372
|
+
|
|
373
|
+
## License
|
|
374
|
+
|
|
375
|
+
MIT
|