@elchinabilov/nestjs-audit-logs 1.1.0 ā 1.1.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.
- package/bin/cli.js +111 -0
- package/package.json +5 -1
package/bin/cli.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const command = args[0];
|
|
8
|
+
|
|
9
|
+
function getArgValue(flag) {
|
|
10
|
+
const index = args.indexOf(flag);
|
|
11
|
+
return index !== -1 ? args[index + 1] : null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const customPath = getArgValue("--path");
|
|
15
|
+
|
|
16
|
+
// --- paths ---
|
|
17
|
+
const PACKAGE_ROOT = path.resolve(__dirname, "..");
|
|
18
|
+
const MIGRATIONS_SOURCE = path.join(PACKAGE_ROOT, "src/migrations");
|
|
19
|
+
|
|
20
|
+
const PROJECT_ROOT = process.cwd();
|
|
21
|
+
const DEFAULT_TARGET = path.join(PROJECT_ROOT, "src/database/migrations");
|
|
22
|
+
|
|
23
|
+
const MIGRATIONS_TARGET = customPath
|
|
24
|
+
? path.isAbsolute(customPath)
|
|
25
|
+
? customPath
|
|
26
|
+
: path.join(PROJECT_ROOT, customPath)
|
|
27
|
+
: DEFAULT_TARGET;
|
|
28
|
+
|
|
29
|
+
// --- helpers ---
|
|
30
|
+
function toKebabCase(str) {
|
|
31
|
+
return str
|
|
32
|
+
.replace(/([a-z])([A-Z])/g, "$1-$2")
|
|
33
|
+
.replace(/\s+/g, "-")
|
|
34
|
+
.toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function generateTimestamp() {
|
|
38
|
+
return Date.now().toString();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function copyMigrations() {
|
|
42
|
+
if (!fs.existsSync(MIGRATIONS_SOURCE)) {
|
|
43
|
+
console.error("ā Audit Log migrations folder not found.");
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!fs.existsSync(MIGRATIONS_TARGET)) {
|
|
48
|
+
fs.mkdirSync(MIGRATIONS_TARGET, { recursive: true });
|
|
49
|
+
console.log(`š Created folder: ${MIGRATIONS_TARGET}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const files = fs.readdirSync(MIGRATIONS_SOURCE);
|
|
53
|
+
|
|
54
|
+
files.forEach((file) => {
|
|
55
|
+
const sourcePath = path.join(MIGRATIONS_SOURCE, file);
|
|
56
|
+
const originalContent = fs.readFileSync(sourcePath, "utf8");
|
|
57
|
+
|
|
58
|
+
// 1ļøā£ Class name-i tap
|
|
59
|
+
const classMatch = originalContent.match(/export class (\w+)/);
|
|
60
|
+
if (!classMatch) {
|
|
61
|
+
console.log(`ā ļø Skipped (no class found): ${file}`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const baseClassName = classMatch[1];
|
|
66
|
+
const timestamp = generateTimestamp();
|
|
67
|
+
const finalClassName = `${baseClassName}${timestamp}`;
|
|
68
|
+
|
|
69
|
+
// 2ļøā£ Content-i update et
|
|
70
|
+
let updatedContent = originalContent
|
|
71
|
+
.replace(
|
|
72
|
+
new RegExp(`export class ${baseClassName}`),
|
|
73
|
+
`export class ${finalClassName}`
|
|
74
|
+
)
|
|
75
|
+
.replace(
|
|
76
|
+
new RegExp(`name\\s*=\\s*['"]${baseClassName}['"]`),
|
|
77
|
+
`name = '${finalClassName}'`
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// 3ļøā£ File name yarat
|
|
81
|
+
const fileBaseName = toKebabCase(baseClassName);
|
|
82
|
+
const finalFileName = `${timestamp}-${fileBaseName}.ts`;
|
|
83
|
+
const targetPath = path.join(MIGRATIONS_TARGET, finalFileName);
|
|
84
|
+
|
|
85
|
+
if (fs.existsSync(targetPath)) {
|
|
86
|
+
console.log(`āļø Skipped (already exists): ${finalFileName}`);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fs.writeFileSync(targetPath, updatedContent);
|
|
91
|
+
console.log(`ā
Created: ${finalFileName}`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log("\nš Audit Log migration(s) generated successfully.");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// --- command router ---
|
|
98
|
+
switch (command) {
|
|
99
|
+
case "migration:copy":
|
|
100
|
+
copyMigrations();
|
|
101
|
+
break;
|
|
102
|
+
|
|
103
|
+
default:
|
|
104
|
+
console.log(`
|
|
105
|
+
Usage:
|
|
106
|
+
npx @elchinabilov/nestjs-audit-logs migration:copy [--path <folder>]
|
|
107
|
+
|
|
108
|
+
Description:
|
|
109
|
+
Copies audit log migrations into your project with timestamped class names.
|
|
110
|
+
`);
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elchinabilov/nestjs-audit-logs",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "NestJS audit log module with TypeORM and CLS context",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,8 +11,12 @@
|
|
|
11
11
|
"default": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"nestjs-audit-logs": "bin/cli.js"
|
|
16
|
+
},
|
|
14
17
|
"files": [
|
|
15
18
|
"dist",
|
|
19
|
+
"bin",
|
|
16
20
|
"README.md"
|
|
17
21
|
],
|
|
18
22
|
"scripts": {
|