elit 3.1.8 → 3.2.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/dist/cli.js +307 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/database.d.mts +10 -1
- package/dist/database.d.ts +9 -0
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +293 -2
- package/dist/database.mjs +291 -1
- package/dist/server.js +290 -1
- package/dist/server.mjs +290 -1
- package/package.json +1 -1
- package/src/config.ts +20 -6
- package/src/database.ts +364 -2
package/dist/database.js
CHANGED
|
@@ -32,6 +32,7 @@ var database_exports = {};
|
|
|
32
32
|
__export(database_exports, {
|
|
33
33
|
Database: () => Database,
|
|
34
34
|
database: () => database,
|
|
35
|
+
dbConsole: () => dbConsole,
|
|
35
36
|
default: () => database_default
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(database_exports);
|
|
@@ -331,8 +332,16 @@ var Database = class {
|
|
|
331
332
|
acc[type] = (...args) => logs.push({ type, args });
|
|
332
333
|
return acc;
|
|
333
334
|
}, {});
|
|
335
|
+
const systemBase = {
|
|
336
|
+
update,
|
|
337
|
+
remove,
|
|
338
|
+
rename,
|
|
339
|
+
read,
|
|
340
|
+
create,
|
|
341
|
+
save
|
|
342
|
+
};
|
|
334
343
|
this.register({
|
|
335
|
-
|
|
344
|
+
dbConsole: { ...customConsole, ...systemBase }
|
|
336
345
|
});
|
|
337
346
|
let stringCode;
|
|
338
347
|
if (typeof code === "function") {
|
|
@@ -427,14 +436,296 @@ var Database = class {
|
|
|
427
436
|
return await this.vmRun(code, options);
|
|
428
437
|
}
|
|
429
438
|
};
|
|
439
|
+
function create(dbName, code) {
|
|
440
|
+
const DIR = "databases";
|
|
441
|
+
const basePath = process.cwd();
|
|
442
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
443
|
+
const dbPath = import_node_path.default.join(baseDir, `${dbName}.ts`);
|
|
444
|
+
import_node_fs.default.appendFileSync(dbPath, code.toString(), "utf8");
|
|
445
|
+
}
|
|
446
|
+
function read(dbName) {
|
|
447
|
+
const DIR = "databases";
|
|
448
|
+
const basePath = process.cwd();
|
|
449
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
450
|
+
const dbPath = import_node_path.default.join(baseDir, `${dbName}.ts`);
|
|
451
|
+
if (!import_node_fs.default.existsSync(dbPath)) {
|
|
452
|
+
throw new Error(`Database '${dbName}' not found`);
|
|
453
|
+
}
|
|
454
|
+
return import_node_fs.default.readFileSync(dbPath, "utf8");
|
|
455
|
+
}
|
|
456
|
+
function remove(dbName, fnName) {
|
|
457
|
+
const DIR = "databases";
|
|
458
|
+
const basePath = process.cwd();
|
|
459
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
460
|
+
const dbPath = import_node_path.default.join(baseDir, `${dbName}.ts`);
|
|
461
|
+
if (!import_node_fs.default.existsSync(dbPath)) return false;
|
|
462
|
+
if (!fnName) {
|
|
463
|
+
const bak2 = `${dbPath}.bak`;
|
|
464
|
+
try {
|
|
465
|
+
import_node_fs.default.copyFileSync(dbPath, bak2);
|
|
466
|
+
} catch (e) {
|
|
467
|
+
}
|
|
468
|
+
try {
|
|
469
|
+
import_node_fs.default.unlinkSync(dbPath);
|
|
470
|
+
return "Removed successfully";
|
|
471
|
+
} catch (e) {
|
|
472
|
+
return "Removed failed";
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const bak = `${dbPath}.bak`;
|
|
476
|
+
try {
|
|
477
|
+
import_node_fs.default.copyFileSync(dbPath, bak);
|
|
478
|
+
} catch (e) {
|
|
479
|
+
}
|
|
480
|
+
let src = import_node_fs.default.readFileSync(dbPath, "utf8");
|
|
481
|
+
const escaped = fnName.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
482
|
+
const startRe = new RegExp(
|
|
483
|
+
`function\\s+${escaped}\\s*\\(|\\bclass\\s+${escaped}\\b|\\b(?:const|let|var)\\s+${escaped}\\s*=\\s*(?:function\\b|class\\b|\\(|\\{|\\[)`,
|
|
484
|
+
"m"
|
|
485
|
+
);
|
|
486
|
+
const startMatch = src.match(startRe);
|
|
487
|
+
if (startMatch) {
|
|
488
|
+
const startIdx = startMatch.index;
|
|
489
|
+
const len = src.length;
|
|
490
|
+
const idxCurly = src.indexOf("{", startIdx);
|
|
491
|
+
const idxBracket = src.indexOf("[", startIdx);
|
|
492
|
+
let braceOpen = -1;
|
|
493
|
+
if (idxCurly === -1) braceOpen = idxBracket;
|
|
494
|
+
else if (idxBracket === -1) braceOpen = idxCurly;
|
|
495
|
+
else braceOpen = Math.min(idxCurly, idxBracket);
|
|
496
|
+
if (braceOpen !== -1) {
|
|
497
|
+
const openingChar = src[braceOpen];
|
|
498
|
+
const closingChar = openingChar === "[" ? "]" : "}";
|
|
499
|
+
let i = braceOpen + 1;
|
|
500
|
+
let depth = 1;
|
|
501
|
+
while (i < len && depth > 0) {
|
|
502
|
+
const ch = src[i];
|
|
503
|
+
if (ch === openingChar) depth++;
|
|
504
|
+
else if (ch === closingChar) depth--;
|
|
505
|
+
i++;
|
|
506
|
+
}
|
|
507
|
+
let braceClose = i;
|
|
508
|
+
let endIdx = braceClose;
|
|
509
|
+
if (src.slice(braceClose, braceClose + 1) === ";")
|
|
510
|
+
endIdx = braceClose + 1;
|
|
511
|
+
const before = src.slice(0, startIdx);
|
|
512
|
+
const after = src.slice(endIdx);
|
|
513
|
+
src = before + after;
|
|
514
|
+
} else {
|
|
515
|
+
const semi = src.indexOf(";", startIdx);
|
|
516
|
+
let endIdx = semi !== -1 ? semi + 1 : src.indexOf("\n\n", startIdx);
|
|
517
|
+
if (endIdx === -1) endIdx = len;
|
|
518
|
+
src = src.slice(0, startIdx) + src.slice(endIdx);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const exportRe = new RegExp(
|
|
522
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
523
|
+
"g"
|
|
524
|
+
);
|
|
525
|
+
src = src.replace(exportRe, "");
|
|
526
|
+
src = src.replace(/\n{3,}/g, "\n\n");
|
|
527
|
+
import_node_fs.default.writeFileSync(dbPath, src, "utf8");
|
|
528
|
+
return `Removed ${fnName} from database ${dbName}.`;
|
|
529
|
+
}
|
|
530
|
+
function rename(oldName, newName) {
|
|
531
|
+
const DIR = "databases";
|
|
532
|
+
const basePath = process.cwd();
|
|
533
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
534
|
+
const oldPath = import_node_path.default.join(baseDir, `${oldName}.ts`);
|
|
535
|
+
const newPath = import_node_path.default.join(baseDir, `${newName}.ts`);
|
|
536
|
+
if (!import_node_fs.default.existsSync(oldPath)) {
|
|
537
|
+
return `Error: File '${oldName}.ts' does not exist in the database`;
|
|
538
|
+
}
|
|
539
|
+
if (import_node_fs.default.existsSync(newPath)) {
|
|
540
|
+
return `Error: File '${newName}.ts' already exists in the database`;
|
|
541
|
+
}
|
|
542
|
+
try {
|
|
543
|
+
import_node_fs.default.renameSync(oldPath, newPath);
|
|
544
|
+
return `Successfully renamed '${oldName}.ts' to '${newName}.ts'`;
|
|
545
|
+
} catch (error) {
|
|
546
|
+
return `Error renaming file: ${error instanceof Error ? error.message : String(error)}`;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
function save(dbName, code) {
|
|
550
|
+
const DIR = "databases";
|
|
551
|
+
const basePath = process.cwd();
|
|
552
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
553
|
+
const dbPath = import_node_path.default.join(baseDir, `${dbName}.ts`);
|
|
554
|
+
let fileContent = typeof code === "function" ? code.toString() : code;
|
|
555
|
+
import_node_fs.default.writeFileSync(dbPath, fileContent, "utf8");
|
|
556
|
+
}
|
|
557
|
+
function update(dbName, fnName, code) {
|
|
558
|
+
const DIR = "databases";
|
|
559
|
+
const basePath = process.cwd();
|
|
560
|
+
const baseDir = import_node_path.default.resolve(basePath, DIR);
|
|
561
|
+
const dbPath = import_node_path.default.join(baseDir, `${dbName}.ts`);
|
|
562
|
+
let src;
|
|
563
|
+
if (!import_node_fs.default.existsSync(dbPath)) {
|
|
564
|
+
try {
|
|
565
|
+
import_node_fs.default.writeFileSync(dbPath, "", "utf8");
|
|
566
|
+
return `Created new database file: ${dbPath}`;
|
|
567
|
+
} catch (e) {
|
|
568
|
+
return `Failed to create dbPath file: ${dbPath}`;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
src = import_node_fs.default.readFileSync(dbPath, "utf8");
|
|
572
|
+
const originalSrc = src;
|
|
573
|
+
const escaped = fnName.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
574
|
+
const startRe = new RegExp(
|
|
575
|
+
`function\\s+${escaped}\\s*\\(|\\bclass\\s+${escaped}\\b|\\b(?:const|let|var)\\s+${escaped}\\s*=\\s*(?:function\\b|class\\b|\\(|\\{|\\[)`,
|
|
576
|
+
"m"
|
|
577
|
+
);
|
|
578
|
+
const startMatch = src.match(startRe);
|
|
579
|
+
let declKind = null;
|
|
580
|
+
if (startMatch) {
|
|
581
|
+
let startIdx = startMatch.index;
|
|
582
|
+
const snippet = src.slice(startIdx, startIdx + 80);
|
|
583
|
+
if (/^function\b/.test(snippet)) declKind = "functionDecl";
|
|
584
|
+
else if (/^class\b/.test(snippet)) declKind = "classDecl";
|
|
585
|
+
else if (/^\b(?:const|let|var)\b/.test(snippet)) declKind = "varAssign";
|
|
586
|
+
}
|
|
587
|
+
let newCode;
|
|
588
|
+
if (typeof code === "function") {
|
|
589
|
+
const fnStr = code.toString();
|
|
590
|
+
if (declKind === "functionDecl") {
|
|
591
|
+
if (/^function\s+\w+/.test(fnStr)) newCode = fnStr;
|
|
592
|
+
else
|
|
593
|
+
newCode = `function ${fnName}${fnStr.replace(
|
|
594
|
+
/^function\s*\(/,
|
|
595
|
+
"("
|
|
596
|
+
)}`;
|
|
597
|
+
} else if (declKind === "classDecl") {
|
|
598
|
+
if (/^class\s+\w+/.test(fnStr)) newCode = fnStr;
|
|
599
|
+
else if (/^class\s*\{/.test(fnStr))
|
|
600
|
+
newCode = fnStr.replace(/^class\s*\{/, `class ${fnName} {`);
|
|
601
|
+
else newCode = `const ${fnName} = ${fnStr};`;
|
|
602
|
+
} else {
|
|
603
|
+
newCode = `const ${fnName} = ${fnStr};`;
|
|
604
|
+
}
|
|
605
|
+
} else {
|
|
606
|
+
newCode = `const ${fnName} = ${valueToCode(code, 0)};`;
|
|
607
|
+
}
|
|
608
|
+
if (startMatch) {
|
|
609
|
+
const startIdx = startMatch.index;
|
|
610
|
+
const idxCurly = src.indexOf("{", startIdx);
|
|
611
|
+
const idxBracket = src.indexOf("[", startIdx);
|
|
612
|
+
let braceOpen = -1;
|
|
613
|
+
if (idxCurly === -1) braceOpen = idxBracket;
|
|
614
|
+
else if (idxBracket === -1) braceOpen = idxCurly;
|
|
615
|
+
else braceOpen = Math.min(idxCurly, idxBracket);
|
|
616
|
+
if (braceOpen === -1) {
|
|
617
|
+
const exportRe = new RegExp(
|
|
618
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
619
|
+
"m"
|
|
620
|
+
);
|
|
621
|
+
if (exportRe.test(src)) {
|
|
622
|
+
src = src.replace(
|
|
623
|
+
exportRe,
|
|
624
|
+
`${newCode}
|
|
625
|
+
|
|
626
|
+
export const ${fnName}: any = ${fnName};`
|
|
627
|
+
);
|
|
628
|
+
} else {
|
|
629
|
+
src = src + `
|
|
630
|
+
|
|
631
|
+
${newCode}
|
|
632
|
+
|
|
633
|
+
export const ${fnName}: any = ${fnName};`;
|
|
634
|
+
}
|
|
635
|
+
} else {
|
|
636
|
+
const openingChar = src[braceOpen];
|
|
637
|
+
const closingChar = openingChar === "[" ? "]" : "}";
|
|
638
|
+
let i = braceOpen + 1;
|
|
639
|
+
let depth = 1;
|
|
640
|
+
const len = src.length;
|
|
641
|
+
while (i < len && depth > 0) {
|
|
642
|
+
const ch = src[i];
|
|
643
|
+
if (ch === openingChar) depth++;
|
|
644
|
+
else if (ch === closingChar) depth--;
|
|
645
|
+
i++;
|
|
646
|
+
}
|
|
647
|
+
let braceClose = i;
|
|
648
|
+
let endIdx = braceClose;
|
|
649
|
+
if (src.slice(braceClose, braceClose + 1) === ";")
|
|
650
|
+
endIdx = braceClose + 1;
|
|
651
|
+
const before = src.slice(0, startIdx);
|
|
652
|
+
const after = src.slice(endIdx);
|
|
653
|
+
src = before + newCode + after;
|
|
654
|
+
}
|
|
655
|
+
} else {
|
|
656
|
+
const exportRe = new RegExp(
|
|
657
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
658
|
+
"m"
|
|
659
|
+
);
|
|
660
|
+
if (exportRe.test(src)) {
|
|
661
|
+
src = src.replace(
|
|
662
|
+
exportRe,
|
|
663
|
+
`${newCode}
|
|
664
|
+
|
|
665
|
+
export const ${fnName}: any = ${fnName};`
|
|
666
|
+
);
|
|
667
|
+
} else {
|
|
668
|
+
src = src + `
|
|
669
|
+
|
|
670
|
+
${newCode}
|
|
671
|
+
|
|
672
|
+
export const ${fnName}: any = ${fnName};`;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
import_node_fs.default.writeFileSync(dbPath, src, "utf8");
|
|
676
|
+
if (src === originalSrc) {
|
|
677
|
+
return `Saved ${fnName} to database ${dbName}.`;
|
|
678
|
+
} else {
|
|
679
|
+
return `Updated ${dbName} with ${fnName}.`;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function valueToCode(val, depth = 0) {
|
|
683
|
+
const indentUnit = " ";
|
|
684
|
+
const indent = indentUnit.repeat(depth);
|
|
685
|
+
const indentInner = indentUnit.repeat(depth + 1);
|
|
686
|
+
if (val === null) return "null";
|
|
687
|
+
const t = typeof val;
|
|
688
|
+
if (t === "string") return JSON.stringify(val);
|
|
689
|
+
if (t === "number" || t === "boolean") return String(val);
|
|
690
|
+
if (t === "function") return val.toString();
|
|
691
|
+
if (Array.isArray(val)) {
|
|
692
|
+
if (val.length === 0) return "[]";
|
|
693
|
+
const items = val.map((v) => valueToCode(v, depth + 1));
|
|
694
|
+
return "[\n" + items.map((it) => indentInner + it).join(",\n") + "\n" + indent + "]";
|
|
695
|
+
}
|
|
696
|
+
if (t === "object") {
|
|
697
|
+
const keys = Object.keys(val);
|
|
698
|
+
if (keys.length === 0) return "{}";
|
|
699
|
+
const entries = keys.map((k) => {
|
|
700
|
+
const keyPart = isIdentifier(k) ? k : JSON.stringify(k);
|
|
701
|
+
const v = valueToCode(val[k], depth + 1);
|
|
702
|
+
return indentInner + keyPart + ": " + v;
|
|
703
|
+
});
|
|
704
|
+
return "{\n" + entries.join(",\n") + "\n" + indent + "}";
|
|
705
|
+
}
|
|
706
|
+
return String(val);
|
|
707
|
+
}
|
|
708
|
+
function isIdentifier(key) {
|
|
709
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key);
|
|
710
|
+
}
|
|
430
711
|
function database() {
|
|
431
712
|
return new Database({
|
|
432
713
|
dir: resolve(process.cwd(), "databases")
|
|
433
714
|
});
|
|
434
715
|
}
|
|
716
|
+
var dbConsole = {
|
|
717
|
+
create,
|
|
718
|
+
read,
|
|
719
|
+
remove,
|
|
720
|
+
rename,
|
|
721
|
+
save,
|
|
722
|
+
update,
|
|
723
|
+
...console
|
|
724
|
+
};
|
|
435
725
|
var database_default = database;
|
|
436
726
|
// Annotate the CommonJS export names for ESM import in node:
|
|
437
727
|
0 && (module.exports = {
|
|
438
728
|
Database,
|
|
439
|
-
database
|
|
729
|
+
database,
|
|
730
|
+
dbConsole
|
|
440
731
|
});
|
package/dist/database.mjs
CHANGED
|
@@ -297,8 +297,16 @@ var Database = class {
|
|
|
297
297
|
acc[type] = (...args) => logs.push({ type, args });
|
|
298
298
|
return acc;
|
|
299
299
|
}, {});
|
|
300
|
+
const systemBase = {
|
|
301
|
+
update,
|
|
302
|
+
remove,
|
|
303
|
+
rename,
|
|
304
|
+
read,
|
|
305
|
+
create,
|
|
306
|
+
save
|
|
307
|
+
};
|
|
300
308
|
this.register({
|
|
301
|
-
|
|
309
|
+
dbConsole: { ...customConsole, ...systemBase }
|
|
302
310
|
});
|
|
303
311
|
let stringCode;
|
|
304
312
|
if (typeof code === "function") {
|
|
@@ -393,14 +401,296 @@ var Database = class {
|
|
|
393
401
|
return await this.vmRun(code, options);
|
|
394
402
|
}
|
|
395
403
|
};
|
|
404
|
+
function create(dbName, code) {
|
|
405
|
+
const DIR = "databases";
|
|
406
|
+
const basePath = process.cwd();
|
|
407
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
408
|
+
const dbPath = path.join(baseDir, `${dbName}.ts`);
|
|
409
|
+
fs.appendFileSync(dbPath, code.toString(), "utf8");
|
|
410
|
+
}
|
|
411
|
+
function read(dbName) {
|
|
412
|
+
const DIR = "databases";
|
|
413
|
+
const basePath = process.cwd();
|
|
414
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
415
|
+
const dbPath = path.join(baseDir, `${dbName}.ts`);
|
|
416
|
+
if (!fs.existsSync(dbPath)) {
|
|
417
|
+
throw new Error(`Database '${dbName}' not found`);
|
|
418
|
+
}
|
|
419
|
+
return fs.readFileSync(dbPath, "utf8");
|
|
420
|
+
}
|
|
421
|
+
function remove(dbName, fnName) {
|
|
422
|
+
const DIR = "databases";
|
|
423
|
+
const basePath = process.cwd();
|
|
424
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
425
|
+
const dbPath = path.join(baseDir, `${dbName}.ts`);
|
|
426
|
+
if (!fs.existsSync(dbPath)) return false;
|
|
427
|
+
if (!fnName) {
|
|
428
|
+
const bak2 = `${dbPath}.bak`;
|
|
429
|
+
try {
|
|
430
|
+
fs.copyFileSync(dbPath, bak2);
|
|
431
|
+
} catch (e) {
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
fs.unlinkSync(dbPath);
|
|
435
|
+
return "Removed successfully";
|
|
436
|
+
} catch (e) {
|
|
437
|
+
return "Removed failed";
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
const bak = `${dbPath}.bak`;
|
|
441
|
+
try {
|
|
442
|
+
fs.copyFileSync(dbPath, bak);
|
|
443
|
+
} catch (e) {
|
|
444
|
+
}
|
|
445
|
+
let src = fs.readFileSync(dbPath, "utf8");
|
|
446
|
+
const escaped = fnName.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
447
|
+
const startRe = new RegExp(
|
|
448
|
+
`function\\s+${escaped}\\s*\\(|\\bclass\\s+${escaped}\\b|\\b(?:const|let|var)\\s+${escaped}\\s*=\\s*(?:function\\b|class\\b|\\(|\\{|\\[)`,
|
|
449
|
+
"m"
|
|
450
|
+
);
|
|
451
|
+
const startMatch = src.match(startRe);
|
|
452
|
+
if (startMatch) {
|
|
453
|
+
const startIdx = startMatch.index;
|
|
454
|
+
const len = src.length;
|
|
455
|
+
const idxCurly = src.indexOf("{", startIdx);
|
|
456
|
+
const idxBracket = src.indexOf("[", startIdx);
|
|
457
|
+
let braceOpen = -1;
|
|
458
|
+
if (idxCurly === -1) braceOpen = idxBracket;
|
|
459
|
+
else if (idxBracket === -1) braceOpen = idxCurly;
|
|
460
|
+
else braceOpen = Math.min(idxCurly, idxBracket);
|
|
461
|
+
if (braceOpen !== -1) {
|
|
462
|
+
const openingChar = src[braceOpen];
|
|
463
|
+
const closingChar = openingChar === "[" ? "]" : "}";
|
|
464
|
+
let i = braceOpen + 1;
|
|
465
|
+
let depth = 1;
|
|
466
|
+
while (i < len && depth > 0) {
|
|
467
|
+
const ch = src[i];
|
|
468
|
+
if (ch === openingChar) depth++;
|
|
469
|
+
else if (ch === closingChar) depth--;
|
|
470
|
+
i++;
|
|
471
|
+
}
|
|
472
|
+
let braceClose = i;
|
|
473
|
+
let endIdx = braceClose;
|
|
474
|
+
if (src.slice(braceClose, braceClose + 1) === ";")
|
|
475
|
+
endIdx = braceClose + 1;
|
|
476
|
+
const before = src.slice(0, startIdx);
|
|
477
|
+
const after = src.slice(endIdx);
|
|
478
|
+
src = before + after;
|
|
479
|
+
} else {
|
|
480
|
+
const semi = src.indexOf(";", startIdx);
|
|
481
|
+
let endIdx = semi !== -1 ? semi + 1 : src.indexOf("\n\n", startIdx);
|
|
482
|
+
if (endIdx === -1) endIdx = len;
|
|
483
|
+
src = src.slice(0, startIdx) + src.slice(endIdx);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const exportRe = new RegExp(
|
|
487
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
488
|
+
"g"
|
|
489
|
+
);
|
|
490
|
+
src = src.replace(exportRe, "");
|
|
491
|
+
src = src.replace(/\n{3,}/g, "\n\n");
|
|
492
|
+
fs.writeFileSync(dbPath, src, "utf8");
|
|
493
|
+
return `Removed ${fnName} from database ${dbName}.`;
|
|
494
|
+
}
|
|
495
|
+
function rename(oldName, newName) {
|
|
496
|
+
const DIR = "databases";
|
|
497
|
+
const basePath = process.cwd();
|
|
498
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
499
|
+
const oldPath = path.join(baseDir, `${oldName}.ts`);
|
|
500
|
+
const newPath = path.join(baseDir, `${newName}.ts`);
|
|
501
|
+
if (!fs.existsSync(oldPath)) {
|
|
502
|
+
return `Error: File '${oldName}.ts' does not exist in the database`;
|
|
503
|
+
}
|
|
504
|
+
if (fs.existsSync(newPath)) {
|
|
505
|
+
return `Error: File '${newName}.ts' already exists in the database`;
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
fs.renameSync(oldPath, newPath);
|
|
509
|
+
return `Successfully renamed '${oldName}.ts' to '${newName}.ts'`;
|
|
510
|
+
} catch (error) {
|
|
511
|
+
return `Error renaming file: ${error instanceof Error ? error.message : String(error)}`;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function save(dbName, code) {
|
|
515
|
+
const DIR = "databases";
|
|
516
|
+
const basePath = process.cwd();
|
|
517
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
518
|
+
const dbPath = path.join(baseDir, `${dbName}.ts`);
|
|
519
|
+
let fileContent = typeof code === "function" ? code.toString() : code;
|
|
520
|
+
fs.writeFileSync(dbPath, fileContent, "utf8");
|
|
521
|
+
}
|
|
522
|
+
function update(dbName, fnName, code) {
|
|
523
|
+
const DIR = "databases";
|
|
524
|
+
const basePath = process.cwd();
|
|
525
|
+
const baseDir = path.resolve(basePath, DIR);
|
|
526
|
+
const dbPath = path.join(baseDir, `${dbName}.ts`);
|
|
527
|
+
let src;
|
|
528
|
+
if (!fs.existsSync(dbPath)) {
|
|
529
|
+
try {
|
|
530
|
+
fs.writeFileSync(dbPath, "", "utf8");
|
|
531
|
+
return `Created new database file: ${dbPath}`;
|
|
532
|
+
} catch (e) {
|
|
533
|
+
return `Failed to create dbPath file: ${dbPath}`;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
src = fs.readFileSync(dbPath, "utf8");
|
|
537
|
+
const originalSrc = src;
|
|
538
|
+
const escaped = fnName.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
539
|
+
const startRe = new RegExp(
|
|
540
|
+
`function\\s+${escaped}\\s*\\(|\\bclass\\s+${escaped}\\b|\\b(?:const|let|var)\\s+${escaped}\\s*=\\s*(?:function\\b|class\\b|\\(|\\{|\\[)`,
|
|
541
|
+
"m"
|
|
542
|
+
);
|
|
543
|
+
const startMatch = src.match(startRe);
|
|
544
|
+
let declKind = null;
|
|
545
|
+
if (startMatch) {
|
|
546
|
+
let startIdx = startMatch.index;
|
|
547
|
+
const snippet = src.slice(startIdx, startIdx + 80);
|
|
548
|
+
if (/^function\b/.test(snippet)) declKind = "functionDecl";
|
|
549
|
+
else if (/^class\b/.test(snippet)) declKind = "classDecl";
|
|
550
|
+
else if (/^\b(?:const|let|var)\b/.test(snippet)) declKind = "varAssign";
|
|
551
|
+
}
|
|
552
|
+
let newCode;
|
|
553
|
+
if (typeof code === "function") {
|
|
554
|
+
const fnStr = code.toString();
|
|
555
|
+
if (declKind === "functionDecl") {
|
|
556
|
+
if (/^function\s+\w+/.test(fnStr)) newCode = fnStr;
|
|
557
|
+
else
|
|
558
|
+
newCode = `function ${fnName}${fnStr.replace(
|
|
559
|
+
/^function\s*\(/,
|
|
560
|
+
"("
|
|
561
|
+
)}`;
|
|
562
|
+
} else if (declKind === "classDecl") {
|
|
563
|
+
if (/^class\s+\w+/.test(fnStr)) newCode = fnStr;
|
|
564
|
+
else if (/^class\s*\{/.test(fnStr))
|
|
565
|
+
newCode = fnStr.replace(/^class\s*\{/, `class ${fnName} {`);
|
|
566
|
+
else newCode = `const ${fnName} = ${fnStr};`;
|
|
567
|
+
} else {
|
|
568
|
+
newCode = `const ${fnName} = ${fnStr};`;
|
|
569
|
+
}
|
|
570
|
+
} else {
|
|
571
|
+
newCode = `const ${fnName} = ${valueToCode(code, 0)};`;
|
|
572
|
+
}
|
|
573
|
+
if (startMatch) {
|
|
574
|
+
const startIdx = startMatch.index;
|
|
575
|
+
const idxCurly = src.indexOf("{", startIdx);
|
|
576
|
+
const idxBracket = src.indexOf("[", startIdx);
|
|
577
|
+
let braceOpen = -1;
|
|
578
|
+
if (idxCurly === -1) braceOpen = idxBracket;
|
|
579
|
+
else if (idxBracket === -1) braceOpen = idxCurly;
|
|
580
|
+
else braceOpen = Math.min(idxCurly, idxBracket);
|
|
581
|
+
if (braceOpen === -1) {
|
|
582
|
+
const exportRe = new RegExp(
|
|
583
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
584
|
+
"m"
|
|
585
|
+
);
|
|
586
|
+
if (exportRe.test(src)) {
|
|
587
|
+
src = src.replace(
|
|
588
|
+
exportRe,
|
|
589
|
+
`${newCode}
|
|
590
|
+
|
|
591
|
+
export const ${fnName}: any = ${fnName};`
|
|
592
|
+
);
|
|
593
|
+
} else {
|
|
594
|
+
src = src + `
|
|
595
|
+
|
|
596
|
+
${newCode}
|
|
597
|
+
|
|
598
|
+
export const ${fnName}: any = ${fnName};`;
|
|
599
|
+
}
|
|
600
|
+
} else {
|
|
601
|
+
const openingChar = src[braceOpen];
|
|
602
|
+
const closingChar = openingChar === "[" ? "]" : "}";
|
|
603
|
+
let i = braceOpen + 1;
|
|
604
|
+
let depth = 1;
|
|
605
|
+
const len = src.length;
|
|
606
|
+
while (i < len && depth > 0) {
|
|
607
|
+
const ch = src[i];
|
|
608
|
+
if (ch === openingChar) depth++;
|
|
609
|
+
else if (ch === closingChar) depth--;
|
|
610
|
+
i++;
|
|
611
|
+
}
|
|
612
|
+
let braceClose = i;
|
|
613
|
+
let endIdx = braceClose;
|
|
614
|
+
if (src.slice(braceClose, braceClose + 1) === ";")
|
|
615
|
+
endIdx = braceClose + 1;
|
|
616
|
+
const before = src.slice(0, startIdx);
|
|
617
|
+
const after = src.slice(endIdx);
|
|
618
|
+
src = before + newCode + after;
|
|
619
|
+
}
|
|
620
|
+
} else {
|
|
621
|
+
const exportRe = new RegExp(
|
|
622
|
+
`export\\s+const\\s+${escaped}\\s*:\\s*any\\s*=\\s*${escaped}\\s*;?`,
|
|
623
|
+
"m"
|
|
624
|
+
);
|
|
625
|
+
if (exportRe.test(src)) {
|
|
626
|
+
src = src.replace(
|
|
627
|
+
exportRe,
|
|
628
|
+
`${newCode}
|
|
629
|
+
|
|
630
|
+
export const ${fnName}: any = ${fnName};`
|
|
631
|
+
);
|
|
632
|
+
} else {
|
|
633
|
+
src = src + `
|
|
634
|
+
|
|
635
|
+
${newCode}
|
|
636
|
+
|
|
637
|
+
export const ${fnName}: any = ${fnName};`;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
fs.writeFileSync(dbPath, src, "utf8");
|
|
641
|
+
if (src === originalSrc) {
|
|
642
|
+
return `Saved ${fnName} to database ${dbName}.`;
|
|
643
|
+
} else {
|
|
644
|
+
return `Updated ${dbName} with ${fnName}.`;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function valueToCode(val, depth = 0) {
|
|
648
|
+
const indentUnit = " ";
|
|
649
|
+
const indent = indentUnit.repeat(depth);
|
|
650
|
+
const indentInner = indentUnit.repeat(depth + 1);
|
|
651
|
+
if (val === null) return "null";
|
|
652
|
+
const t = typeof val;
|
|
653
|
+
if (t === "string") return JSON.stringify(val);
|
|
654
|
+
if (t === "number" || t === "boolean") return String(val);
|
|
655
|
+
if (t === "function") return val.toString();
|
|
656
|
+
if (Array.isArray(val)) {
|
|
657
|
+
if (val.length === 0) return "[]";
|
|
658
|
+
const items = val.map((v) => valueToCode(v, depth + 1));
|
|
659
|
+
return "[\n" + items.map((it) => indentInner + it).join(",\n") + "\n" + indent + "]";
|
|
660
|
+
}
|
|
661
|
+
if (t === "object") {
|
|
662
|
+
const keys = Object.keys(val);
|
|
663
|
+
if (keys.length === 0) return "{}";
|
|
664
|
+
const entries = keys.map((k) => {
|
|
665
|
+
const keyPart = isIdentifier(k) ? k : JSON.stringify(k);
|
|
666
|
+
const v = valueToCode(val[k], depth + 1);
|
|
667
|
+
return indentInner + keyPart + ": " + v;
|
|
668
|
+
});
|
|
669
|
+
return "{\n" + entries.join(",\n") + "\n" + indent + "}";
|
|
670
|
+
}
|
|
671
|
+
return String(val);
|
|
672
|
+
}
|
|
673
|
+
function isIdentifier(key) {
|
|
674
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key);
|
|
675
|
+
}
|
|
396
676
|
function database() {
|
|
397
677
|
return new Database({
|
|
398
678
|
dir: resolve(process.cwd(), "databases")
|
|
399
679
|
});
|
|
400
680
|
}
|
|
681
|
+
var dbConsole = {
|
|
682
|
+
create,
|
|
683
|
+
read,
|
|
684
|
+
remove,
|
|
685
|
+
rename,
|
|
686
|
+
save,
|
|
687
|
+
update,
|
|
688
|
+
...console
|
|
689
|
+
};
|
|
401
690
|
var database_default = database;
|
|
402
691
|
export {
|
|
403
692
|
Database,
|
|
404
693
|
database,
|
|
694
|
+
dbConsole,
|
|
405
695
|
database_default as default
|
|
406
696
|
};
|