-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathupdateREADME.ts
90 lines (78 loc) · 2.78 KB
/
updateREADME.ts
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
85
86
87
88
89
90
import { exec } from "node:child_process";
import { readFile, readdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
const execPromise = promisify(exec);
// Function to increment version in .codemodrc.json
const incrementCodemodrcVersion = async (filePath: string) => {
try {
const data = await readFile(filePath, "utf8");
const config = JSON.parse(data);
if (config.version) {
const versionParts = config.version.split(".").map(Number);
versionParts[versionParts.length - 1]++;
config.version = versionParts.join(".");
} else {
config.version = "1.0.1"; // default initial version if not set
}
await writeFile(filePath, JSON.stringify(config, null, 2), "utf8");
} catch (error) {
console.error(`Failed to update version in ${filePath}: ${error}`);
}
};
// Function to modify README.md
const modifyReadme = async (filePath: string) => {
let modified = false;
try {
const data = await readFile(filePath, "utf8");
const lines = data.split("\n");
const filteredLines = lines.filter(
(line) => !(line.startsWith("# ") || line.startsWith("## Description")),
);
await writeFile(filePath, filteredLines.join("\n"), "utf8");
modified = filteredLines.length !== lines.length;
} catch (error) {
console.error(`Failed to modify README in ${filePath}: ${error}`);
}
return modified;
};
// Function to recursively walk through directory tree
const walkDirectory = async (dir: string, accumulator: string[]) => {
// Read directory contents
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
// Recursively walk into directories
await walkDirectory(fullPath, accumulator);
} else if (entry.name === ".codemodrc.json") {
// If the entry is a .codemodrc.json file, accumulate the directory path
accumulator.push(dir);
}
}
};
// Main function to execute the script
(async () => {
const args = process.argv.slice(2);
if (args.length === 0) {
console.error("Error: Please provide the target path as an argument.");
process.exit(1);
}
const targetPath = args[0];
const accumulator: string[] = [];
await walkDirectory(targetPath, accumulator);
for (let i = 0; i < accumulator.length; i++) {
const dir = accumulator[i].replace(/(\s+)/g, "\\$1");
const codemodrcPath = join(dir, ".codemodrc.json");
const readmePath = join(dir, "README.md");
try {
// Increment version in .codemodrc.json
const modified = await modifyReadme(readmePath);
if (modified) {
await incrementCodemodrcVersion(codemodrcPath);
}
} catch (error) {
console.error(error);
}
}
})();