-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocutil.ts
More file actions
84 lines (71 loc) · 2.92 KB
/
docutil.ts
File metadata and controls
84 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* JSDoc Util to change headers
*
* @author Elijah Rastorguev
* @version 1.0.0
* @build 1005
* @git https://github.com/devsdaddy/bitwarp
* @license MIT
* @updated 12.04.2026
*/
import { Project, SyntaxKind } from "ts-morph";
import { execSync } from "child_process";
/**
* JSDoc Change Util
*/
async function updateJSDocInChangedFiles() {
// Get all changed files via GIT
const changedFiles = execSync("git diff --name-only").toString().trim().split("\n").filter(file => file.endsWith(".ts"));
if (changedFiles.length === 0) {
console.log("No changes found in project.");
return;
}
console.log(`Found changed files: ${changedFiles.length}`);
// Create ts-morph project
const project = new Project({
tsConfigFilePath: "tsconfig.json",
skipAddingFilesFromTsConfig: true, // Only changed
});
// Add changed files
changedFiles.forEach(file => project.addSourceFileAtPath(file));
// Prepare date for @updated
const today = new Date();
const formattedDate = `${today.getDate().toString().padStart(2, '0')}.${(today.getMonth() + 1).toString().padStart(2, '0')}.${today.getFullYear()}`;
let filesUpdatedCount = 0;
// Change every changed file
for (const sourceFile of project.getSourceFiles()) {
let fileWasModified = false;
const jsdocs = sourceFile.getDescendantsOfKind(SyntaxKind.JSDoc);
for (const jsdoc of jsdocs) {
const buildTag = jsdoc.getTags().find(tag => tag.getTagName() === "build");
const updatedTag = jsdoc.getTags().find(tag => tag.getTagName() === "updated");
// Work with @build
if (buildTag) {
const commentText = buildTag.getCommentText();
if (commentText) {
const currentBuild = parseInt(commentText.trim(), 10);
if (!isNaN(currentBuild)) {
const newBuild = currentBuild + 1;
buildTag.replaceWithText(`@build ${newBuild}`);
fileWasModified = true;
console.log(` -> File: ${sourceFile.getFilePath()}, @build updated from ${currentBuild} to ${newBuild}`);
}
}
}
// Work with @updated
if (updatedTag) {
updatedTag.replaceWithText(`@updated ${formattedDate}`);
fileWasModified = true;
console.log(` -> File: ${sourceFile.getFilePath()}, @updated changed to ${formattedDate}`);
}
}
// Save modified file
if (fileWasModified) {
await sourceFile.save();
filesUpdatedCount++;
}
}
console.log(`Done! Updated files: ${filesUpdatedCount}.`);
}
// Update JSDoc in Changed Files
updateJSDocInChangedFiles().catch(console.error);