Skip to content

Commit 12219a7

Browse files
committed
chore(utils) add ts file naming convention checker #1423
1 parent 813dee6 commit 12219a7

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ jobs:
9898
node-version: 20
9999
- name: verify packages version consistency accross sub-modules
100100
run: npm run check:versions
101+
naming_consistency:
102+
name: Check typescript file naming consistency
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@v1
106+
- name: install node v24
107+
uses: actions/setup-node@v1
108+
with:
109+
node-version: 24
110+
- name: verify that source file follow the kebab-case naming convention
111+
run: npm run check:naming
101112

102113
prettier:
103114
name: Check coding style

package-lock.json

Lines changed: 65 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"prepare": "husky install",
3232
"publish": "npm publish --workspaces",
3333
"check:versions": "node utils/check_package_version_consistency.js",
34+
"check:naming": "npx tsx utils/check-kebab-case-naming-convention.ts",
3435
"build:docker": "docker build -t wot-servient .",
3536
"build:podman": "podman build -t wot-servient .",
3637
"clean:dist": "npm exec --workspaces -- npx rimraf tsconfig.tsbuildinfo dist",
@@ -70,6 +71,7 @@
7071
"eslint-plugin-unused-imports": "^3.0.0",
7172
"eslint-plugin-workspaces": "^0.9.0",
7273
"husky": "^7.0.4",
74+
"ignore": "^7.0.5",
7375
"mocha": "^11.7.2",
7476
"prettier": "3.3.3",
7577
"pretty-quick": "^4.0.0",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import ignore from "ignore";
4+
5+
function isKebabCase(filename: string): boolean {
6+
const basename = path.basename(filename, path.extname(filename));
7+
return /^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+)?$/.test(basename);
8+
}
9+
10+
function checkTsFiles(dir: string, ig: ignore.Ignore, invalidFiles: string[] = []): string[] {
11+
const files = fs.readdirSync(dir);
12+
13+
for (const file of files) {
14+
const fullPath = path.join(dir, file);
15+
const relativePath = path.relative(process.cwd(), fullPath);
16+
const stat = fs.statSync(fullPath);
17+
18+
if (ig.ignores(relativePath)) {
19+
continue; // Skip ignored files/folders
20+
}
21+
22+
if (stat.isDirectory()) {
23+
checkTsFiles(fullPath, ig, invalidFiles);
24+
} else if (file.endsWith(".ts")) {
25+
if (!isKebabCase(file)) {
26+
invalidFiles.push(relativePath);
27+
}
28+
}
29+
}
30+
31+
return invalidFiles;
32+
}
33+
34+
function main() {
35+
const projectRoot = process.cwd();
36+
const ig = ignore().add(fs.readFileSync(path.join(projectRoot, ".gitignore")).toString());
37+
const invalidFiles = checkTsFiles(projectRoot, ig);
38+
39+
if (invalidFiles.length > 0) {
40+
console.error("The following files do not follow kebab-case naming:");
41+
invalidFiles.forEach((file) => console.error(`- ${file}`));
42+
process.exit(1);
43+
} else {
44+
console.log("All .ts files follow kebab-case naming!");
45+
process.exit(0);
46+
}
47+
}
48+
49+
main();

0 commit comments

Comments
 (0)