Skip to content

Commit f61325c

Browse files
committed
✨ feature(repo_management_files/extractfilescopes.js): created an automatic scope updater script
adds new scopes of all the non auto generated files in the repo
1 parent 69511c0 commit f61325c

File tree

3 files changed

+200
-12
lines changed

3 files changed

+200
-12
lines changed

package.json

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"build:devdeps2": "grep -Fxv -f <(cat d) <(cat dd) | awk '{print $1 > \"dd\"}'",
6868
"build:devdeps:badges": "bun run build:deps && bun run build:devdeps1 && bun run build:devdeps2 && cat dd | xargs npx update-badge --behind && rm d dd",
6969
"clean": "rm -rf node_modules",
70-
"cm": "git pull && git add . && bun git:all && cz",
70+
"cm": "git pull && node repo_management_files/extractFileScopes.js && git add . && bun git:all && cz",
7171
"dep:check": "bunx upgradeps && npm outdated | xargs && bunx npm-check-updates && bunx check-dependencies && bun ./node_modules/npm-check/bin/cli.js -s | xargs && npm audit",
7272
"dep:skip": "bunx upgradeps -s semantic-release",
7373
"dep:update:all": "bunx upgradeps -u -f -s semantic-release,eslint",
@@ -133,27 +133,76 @@
133133
".depcheckrc.json",
134134
".eslintignore",
135135
".eslintrc.json",
136-
".git",
137136
".gitignore",
138-
".husky/commit-msg",
139-
".husky/prepare-commit-msg",
140137
".prettierignore",
138+
"license",
139+
"readme.md",
141140
".prettierrc.json",
142141
"commit-template.hbs",
143142
"commitlint.config.ts",
144143
"default-template.hbs",
145-
".husky/commit-msg",
146-
".husky/prepare-commit-msg",
147144
"jest.config.json",
148-
"license",
145+
"package-lock.json",
149146
"package.json",
150-
"readme.md",
151147
"release.config.ts",
152148
"renovate.json",
153-
"src/cz-type-extract.js",
154-
"src/gitmojiSemVer-Extract.js",
155149
"tsconfig.json",
156-
"typedoc.json"
150+
"typedoc.json",
151+
".vscode/settings.json",
152+
"public/.gitkeep",
153+
"repo_management_files/cz-emoji-type-extract.js",
154+
"repo_management_files/cz-type-extract.js",
155+
"repo_management_files/directorystructuredocument.md",
156+
"repo_management_files/extractfilescopes.js",
157+
"repo_management_files/genericcommitmessages.md",
158+
"repo_management_files/issues.sh",
159+
"repo_management_files/labels.sh",
160+
"repo_management_files/gitmojisemver-extract.js",
161+
"repo_management_files/packagejsondocument.md",
162+
".github/dependabot.yml",
163+
".github/workflows/build.yml",
164+
".github/workflows/docs.yml",
165+
".github/workflows/pr.yml",
166+
".github/workflows/lint.yml",
167+
".github/workflows/release.yml",
168+
".github/workflows/test.yml",
169+
".husky/commit-msg",
170+
".husky/prepare-commit-msg",
171+
".husky/_/.gitignore",
172+
".husky/_/applypatch-msg",
173+
".husky/_/commit-msg",
174+
".husky/_/h",
175+
".husky/_/post-applypatch",
176+
".husky/_/post-checkout",
177+
".husky/_/post-merge",
178+
".husky/_/post-commit",
179+
".husky/_/post-rewrite",
180+
".husky/_/pre-auto-gc",
181+
".husky/_/pre-commit",
182+
".husky/_/pre-push",
183+
".husky/_/pre-applypatch",
184+
".husky/_/pre-rebase",
185+
".husky/_/prepare-commit-msg",
186+
"test/.gitkeep",
187+
"test/bin/.gitkeep",
188+
"test/configs/.gitkeep",
189+
"test/controllers/.gitkeep",
190+
"test/routes/.gitkeep",
191+
"test/plugins/.gitkeep",
192+
"test/utils/.gitkeep",
193+
"test/models/.gitkeep",
194+
"test/middleware/.gitkeep",
195+
"test/views/.gitkeep",
196+
"src/app.ts",
197+
"src/configs/.gitkeep",
198+
"src/bin/.gitkeep",
199+
"src/middleware/.gitkeep",
200+
"src/plugins/.gitkeep",
201+
"src/controllers/.gitkeep",
202+
"src/utils/.gitkeep",
203+
"src/views/.gitkeep",
204+
"src/routes/.gitkeep",
205+
"src/models/.gitkeep"
157206
],
158207
"questions": {
159208
"scope": "Specify a scope: "
@@ -222,4 +271,4 @@
222271
}
223272
}
224273
}
225-
}
274+
}

public/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { readdir, stat, writeFile, readFile } from 'fs';
2+
import * as path from 'path';
3+
import { dirname } from 'path';
4+
import { fileURLToPath } from 'url';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
// Project root directory, back one directory from repo_management_files.
10+
// normalize the 'directoryPath' string to go from the form `/absolute_path/template-nodes-project/repo_management_files/../`
11+
// to `/absolute_path/template-nodes-project/` to subtract from the beginning of the filePath
12+
const directoryPath = path.normalize(__dirname + '/../');
13+
14+
// Exclude auto generated files and directories.
15+
const exclude = ['.dccache', '.git', 'node_modules', 'dist', 'coverage', 'CHANGELOG.md', 'bun.lockb', 'docs'];
16+
17+
/**
18+
* Subtracts all occurrences of a sub-string from a main string.
19+
*
20+
* This function takes two strings as input: a main string and a sub-string.
21+
* It removes all occurrences of the sub-string from the main string.
22+
*
23+
* @param {string} mainString - The main string from which sub-string will be removed.
24+
* @return {string} The resulting string after removing all occurrences of the sub-string.
25+
*/
26+
function subtractStrings(mainString) {
27+
// Create a regular expression to match all occurrences of the subString
28+
const regex = new RegExp(directoryPath, 'g');
29+
30+
// Replace all occurrences of subString with an empty string
31+
32+
return mainString.replace(regex, '').toLowerCase();
33+
}
34+
35+
36+
/**
37+
* Lists all files and directories in a directory excluding specified items.
38+
* @param {string} dirPath - The directory path to list items from.
39+
* @param {Array<string>} excludeList - The list of file and directory names to exclude.
40+
* @returns {Promise<Array<string>>} - A promise that resolves to the list of file paths.
41+
*/
42+
function listFilesAndDirectories(dirPath, excludeList) {
43+
const fileScopes = [];
44+
45+
return new Promise((resolve, reject) => {
46+
readdir(dirPath, (err, files) => {
47+
if (err) {
48+
return reject(`Unable to read directory: ${err.message}`);
49+
}
50+
51+
const fileStatPromises = files.map(file => {
52+
const filePath = path.join(dirPath, file);
53+
54+
return new Promise((resolve, reject) => {
55+
if (excludeList.includes(file)) {
56+
return resolve();
57+
}
58+
59+
stat(filePath, (err, stats) => {
60+
if (err) {
61+
return reject(`Unable to stat file: ${err.message}`);
62+
}
63+
64+
if (stats.isDirectory()) {
65+
// Recursively list the contents of the directory
66+
listFilesAndDirectories(filePath, excludeList).then(innerFileScopes => {
67+
fileScopes.push(...innerFileScopes);
68+
resolve();
69+
}).catch(reject);
70+
} else {
71+
// File
72+
fileScopes.push(filePath);
73+
resolve();
74+
}
75+
});
76+
});
77+
});
78+
79+
Promise.all(fileStatPromises).then(() => {
80+
resolve(fileScopes);
81+
}).catch(reject);
82+
});
83+
});
84+
}
85+
86+
87+
/**
88+
* Updates the scopes array in the package.json file.
89+
*
90+
* @param {string} packageJsonPath - The path to the package.json file.
91+
* @param {string[]} newScopes - The new scopes array to set.
92+
*/
93+
function updateScopesInPackageJson(packageJsonPath, newScopes) {
94+
// Read the package.json file
95+
readFile(packageJsonPath, 'utf8', (err, data) => {
96+
if (err) {
97+
console.error('Error reading package.json:', err);
98+
return;
99+
}
100+
101+
try {
102+
// Parse the JSON data
103+
const packageJson = JSON.parse(data);
104+
105+
// Update the scopes array
106+
if (packageJson.config && packageJson.config['cz-gitmoji-adapter']) {
107+
packageJson.config['cz-gitmoji-adapter'].scopes = newScopes;
108+
//console.log(newScopes);
109+
} else {
110+
console.error('cz-gitmoji-adapter config not found in package.json');
111+
return;
112+
}
113+
114+
// Convert the modified JSON object back to a string
115+
const updatedPackageJson = JSON.stringify(packageJson, null, 2);
116+
117+
// Write the updated package.json back to the file
118+
writeFile(packageJsonPath, updatedPackageJson, 'utf8', (err) => {
119+
if (err) {
120+
console.error('Error writing package.json:', err);
121+
return;
122+
}
123+
124+
console.log('Successfully updated the scopes array in package.json');
125+
});
126+
} catch (err) {
127+
console.error('Error parsing package.json:', err);
128+
}
129+
});
130+
}
131+
132+
listFilesAndDirectories(directoryPath, exclude)
133+
.then(fileScopes => {
134+
updateScopesInPackageJson(directoryPath + 'package.json', fileScopes.map(subtractStrings));
135+
})
136+
.catch(err => {
137+
console.error('Error:', err);
138+
})

0 commit comments

Comments
 (0)