@cluerise/tools 5.4.0 → 5.4.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.
@@ -1,226 +1,240 @@
1
1
  import FileSystem from "node:fs/promises";
2
- import { z, ZodError } from "zod";
2
+ import { ZodError, z } from "zod";
3
3
  import ChildProcess from "node:child_process";
4
4
  import loadCommitlintConfig from "@commitlint/load";
5
5
  import "eslint";
6
6
  import "glob";
7
- const enginesSchema = z.object({
8
- node: z.string()
7
+ //#region src/modules/core/package-json/git-provider.ts
8
+ var gitProviderOrigins = { github: "https://github.com" };
9
+ (class {
10
+ static #origins = gitProviderOrigins;
11
+ static #names = Object.keys(this.#origins);
12
+ /**
13
+ * Check if the provided name is a valid Git provider name.
14
+ *
15
+ * @param name The name to check.
16
+ * @returns True if the name is valid, otherwise false.
17
+ */
18
+ static isValidName(name) {
19
+ return this.#names.includes(name);
20
+ }
21
+ /**
22
+ * Get the origin URL for the given Git provider name.
23
+ *
24
+ * @param name The Git provider name.
25
+ * @returns The origin URL.
26
+ */
27
+ static getOrigin(name) {
28
+ return this.#origins[name];
29
+ }
9
30
  });
10
- const repositoryObjectSchema = z.object({
11
- type: z.string(),
12
- url: z.string(),
13
- directory: z.string().optional()
31
+ //#endregion
32
+ //#region src/modules/core/package-json/package-json-data.ts
33
+ var enginesSchema = z.object({ node: z.string() });
34
+ var repositoryObjectSchema = z.object({
35
+ type: z.string(),
36
+ url: z.string(),
37
+ directory: z.string().optional()
14
38
  });
15
- const repositorySchema = z.union([z.string(), repositoryObjectSchema]);
39
+ var repositorySchema = z.union([z.string(), repositoryObjectSchema]);
16
40
  z.object({
17
- name: z.string(),
18
- version: z.string().optional(),
19
- description: z.string().optional(),
20
- engines: enginesSchema.optional(),
21
- repository: repositorySchema.optional()
41
+ name: z.string(),
42
+ version: z.string().optional(),
43
+ description: z.string().optional(),
44
+ engines: enginesSchema.optional(),
45
+ repository: repositorySchema.optional()
22
46
  });
23
- const runMain = (main2) => {
24
- Promise.resolve().then(() => main2(process.argv.slice(2))).then((exitCode) => {
25
- process.exit(exitCode);
26
- }).catch((error) => {
27
- console.error("Error:", error);
28
- process.exit(1);
29
- });
47
+ //#endregion
48
+ //#region src/modules/core/run-main.ts
49
+ /**
50
+ * Execute the provided main function and handle any errors that occur.
51
+ *
52
+ * If the main function resolves, the process exits with the returned code.
53
+ * If an error is thrown, it logs the error and exits with code 1.
54
+ *
55
+ * @param main The main function to execute.
56
+ */
57
+ var runMain = (main) => {
58
+ Promise.resolve().then(() => main(process.argv.slice(2))).then((exitCode) => {
59
+ process.exit(exitCode);
60
+ }).catch((error) => {
61
+ console.error("Error:", error);
62
+ process.exit(1);
63
+ });
30
64
  };
31
- class StringUtils {
32
- /**
33
- * Capitalize the first letter of a given string.
34
- *
35
- * If the string is empty, it is returned unchanged.
36
- *
37
- * @param value The string to capitalize.
38
- * @returns The string with the first letter capitalized, or the original string if empty.
39
- */
40
- static capitalize(value) {
41
- const [firstLetter] = value;
42
- if (!firstLetter) {
43
- return value;
44
- }
45
- return `${firstLetter.toUpperCase()}${value.slice(1)}`;
46
- }
47
- }
48
- class Git {
49
- /**
50
- * Get the name of the current branch in the git repository.
51
- *
52
- * Uses `git symbolic-ref --short HEAD` to determine the branch name.
53
- *
54
- * @returns The current branch name.
55
- */
56
- static getBranchName() {
57
- return ChildProcess.execSync("git symbolic-ref --short HEAD").toString().trim();
58
- }
59
- /**
60
- * Check if the repository is currently in a rebase state.
61
- *
62
- * Checks for the presence of a rebase directory in the git directory.
63
- *
64
- * @returns True if rebasing, otherwise false.
65
- */
66
- static isRebasing() {
67
- try {
68
- ChildProcess.execSync("ls `git rev-parse --git-dir` | grep rebase");
69
- return true;
70
- } catch {
71
- return false;
72
- }
73
- }
74
- }
75
- class CommitLinter {
76
- #config;
77
- constructor(config) {
78
- this.#config = config;
79
- }
80
- /**
81
- * Initialize the CommitLinter with the loaded commitlint configuration.
82
- *
83
- * @returns A Promise that resolves to an instance of CommitLinter.
84
- */
85
- static async init() {
86
- const config = await loadCommitlintConfig();
87
- return new CommitLinter(config);
88
- }
89
- /**
90
- * Lint commit messages using commitlint.
91
- *
92
- * @param args An array of arguments to pass to commitlint.
93
- * @returns The exit status of the commitlint command.
94
- */
95
- static lint(args) {
96
- const { status } = ChildProcess.spawnSync("commitlint", args, { stdio: "inherit" });
97
- return status ?? 0;
98
- }
99
- #getRuleValues(ruleName) {
100
- const rule = this.#config.rules[ruleName];
101
- if (!rule) {
102
- return null;
103
- }
104
- const [_severity, _condition, values] = rule;
105
- return values ?? null;
106
- }
107
- #isValidType(type) {
108
- const values = this.#getRuleValues("type-enum");
109
- if (!values) {
110
- return true;
111
- }
112
- return values.includes(type);
113
- }
114
- #isValidScope(scope) {
115
- const values = this.#getRuleValues("scope-enum");
116
- if (!values) {
117
- return true;
118
- }
119
- if ("scopes" in values) {
120
- return values.scopes.includes(scope);
121
- }
122
- return values.includes(scope);
123
- }
124
- /**
125
- * Parse a semantic branch name into its type and scope.
126
- *
127
- * @param name The branch name to parse.
128
- * @returns An object containing the type and scope of the commit message.
129
- * @throws An error if the branch name is invalid.
130
- */
131
- parseSemanticBranchName(name) {
132
- const [typeValue, scopeValue] = name.split("-");
133
- if (!typeValue || !this.#isValidType(typeValue)) {
134
- throw new Error("Invalid commit type in branch name");
135
- }
136
- const type = typeValue.toLowerCase();
137
- const scope = scopeValue && this.#isValidScope(scopeValue) ? scopeValue.toLowerCase() : null;
138
- return {
139
- type,
140
- scope
141
- };
142
- }
143
- /**
144
- * Get the prefix of a semantic commit message as a string.
145
- *
146
- * This prefix consists of the type and scope of the commit message.
147
- * If the scope is not provided, it will only return the type.
148
- *
149
- * @param prefix An object containing the type and scope of the commit message.
150
- * @returns The stringified prefix.
151
- */
152
- static stringifySemanticCommitMessagePrefix({ type, scope }) {
153
- return `${type}${scope ? `(${scope})` : ""}`;
154
- }
155
- static #parseSemanticCommitMessage(message) {
156
- const firstColonPosition = message.search(":");
157
- const prefix = message.slice(0, firstColonPosition).trim();
158
- const content = message.slice(firstColonPosition + 1).trim();
159
- return { prefix, content };
160
- }
161
- /**
162
- * Format a semantic commit message by capitalizing the content after the prefix.
163
- *
164
- * @param message The commit message to format.
165
- * @returns The formatted commit message.
166
- */
167
- static formatSemanticCommitMessage(message) {
168
- const { prefix, content } = this.#parseSemanticCommitMessage(message);
169
- if (!prefix || !content) {
170
- return message;
171
- }
172
- return `${prefix}: ${StringUtils.capitalize(content)}`;
173
- }
174
- /**
175
- * Create a semantic commit message from a prefix and a message.
176
- *
177
- * @param prefix The prefix of the commit message, containing type and scope.
178
- * @param message The content of the commit message.
179
- * @returns The formatted semantic commit message.
180
- */
181
- static createSemanticCommitMessage(prefix, message) {
182
- const [subject, ...comments] = message.split("\n#");
183
- let commitMessage = this.stringifySemanticCommitMessagePrefix(prefix) + ": ";
184
- if (subject) {
185
- commitMessage += StringUtils.capitalize(subject.trim());
186
- }
187
- if (comments.length > 0) {
188
- commitMessage += "\n\n#" + comments.map((comment) => comment.trim()).join("\n#");
189
- }
190
- return commitMessage;
191
- }
192
- }
193
- const commitMessagePathArgSchema = z.string();
194
- const parseCreateCommitMessageArgs = ([commitMessagePathArg]) => {
195
- const commitMessagePath = commitMessagePathArgSchema.parse(commitMessagePathArg);
196
- return {
197
- commitMessagePath
198
- };
65
+ //#endregion
66
+ //#region src/modules/core/utils/string-utils.ts
67
+ var StringUtils = class {
68
+ /**
69
+ * Capitalize the first letter of a given string.
70
+ *
71
+ * If the string is empty, it is returned unchanged.
72
+ *
73
+ * @param value The string to capitalize.
74
+ * @returns The string with the first letter capitalized, or the original string if empty.
75
+ */
76
+ static capitalize(value) {
77
+ const [firstLetter] = value;
78
+ if (!firstLetter) return value;
79
+ return `${firstLetter.toUpperCase()}${value.slice(1)}`;
80
+ }
199
81
  };
200
- const main = async (args) => {
201
- try {
202
- if (Git.isRebasing()) {
203
- return 0;
204
- }
205
- const { commitMessagePath } = parseCreateCommitMessageArgs(args);
206
- const commitMessageBuffer = await FileSystem.readFile(commitMessagePath);
207
- const commitMessage = commitMessageBuffer.toString();
208
- const commitLinter = await CommitLinter.init();
209
- const commitMessagePrefix = commitLinter.parseSemanticBranchName(Git.getBranchName());
210
- if (!commitMessage.startsWith(CommitLinter.stringifySemanticCommitMessagePrefix(commitMessagePrefix))) {
211
- const nextCommitMessage = CommitLinter.createSemanticCommitMessage(commitMessagePrefix, commitMessage);
212
- await FileSystem.writeFile(commitMessagePath, nextCommitMessage);
213
- }
214
- } catch (error) {
215
- if (error instanceof ZodError) {
216
- console.warn("Warning: Invalid commit message path");
217
- }
218
- if (error instanceof Error) {
219
- console.warn("Warning:", error.message);
220
- } else {
221
- console.warn("Warning:", error);
222
- }
223
- }
224
- return 0;
82
+ //#endregion
83
+ //#region src/modules/git/git.ts
84
+ var Git = class {
85
+ /**
86
+ * Get the name of the current branch in the git repository.
87
+ *
88
+ * Uses `git symbolic-ref --short HEAD` to determine the branch name.
89
+ *
90
+ * @returns The current branch name.
91
+ */
92
+ static getBranchName() {
93
+ return ChildProcess.execSync("git symbolic-ref --short HEAD").toString().trim();
94
+ }
95
+ /**
96
+ * Check if the repository is currently in a rebase state.
97
+ *
98
+ * Checks for the presence of a rebase directory in the git directory.
99
+ *
100
+ * @returns True if rebasing, otherwise false.
101
+ */
102
+ static isRebasing() {
103
+ try {
104
+ ChildProcess.execSync("ls `git rev-parse --git-dir` | grep rebase");
105
+ return true;
106
+ } catch {
107
+ return false;
108
+ }
109
+ }
110
+ };
111
+ //#endregion
112
+ //#region src/modules/lint/commit-linter.ts
113
+ var CommitLinter = class CommitLinter {
114
+ #config;
115
+ constructor(config) {
116
+ this.#config = config;
117
+ }
118
+ /**
119
+ * Initialize the CommitLinter with the loaded commitlint configuration.
120
+ *
121
+ * @returns A Promise that resolves to an instance of CommitLinter.
122
+ */
123
+ static async init() {
124
+ return new CommitLinter(await loadCommitlintConfig());
125
+ }
126
+ /**
127
+ * Lint commit messages using commitlint.
128
+ *
129
+ * @param args An array of arguments to pass to commitlint.
130
+ * @returns The exit status of the commitlint command.
131
+ */
132
+ static lint(args) {
133
+ const { status } = ChildProcess.spawnSync("commitlint", args, { stdio: "inherit" });
134
+ return status ?? 0;
135
+ }
136
+ #getRuleValues(ruleName) {
137
+ const rule = this.#config.rules[ruleName];
138
+ if (!rule) return null;
139
+ const [_severity, _condition, values] = rule;
140
+ return values ?? null;
141
+ }
142
+ #isValidType(type) {
143
+ const values = this.#getRuleValues("type-enum");
144
+ if (!values) return true;
145
+ return values.includes(type);
146
+ }
147
+ #isValidScope(scope) {
148
+ const values = this.#getRuleValues("scope-enum");
149
+ if (!values) return true;
150
+ if ("scopes" in values) return values.scopes.includes(scope);
151
+ return values.includes(scope);
152
+ }
153
+ /**
154
+ * Parse a semantic branch name into its type and scope.
155
+ *
156
+ * @param name The branch name to parse.
157
+ * @returns An object containing the type and scope of the commit message.
158
+ * @throws An error if the branch name is invalid.
159
+ */
160
+ parseSemanticBranchName(name) {
161
+ const [typeValue, scopeValue] = name.split("-");
162
+ if (!typeValue || !this.#isValidType(typeValue)) throw new Error("Invalid commit type in branch name");
163
+ return {
164
+ type: typeValue.toLowerCase(),
165
+ scope: scopeValue && this.#isValidScope(scopeValue) ? scopeValue.toLowerCase() : null
166
+ };
167
+ }
168
+ /**
169
+ * Get the prefix of a semantic commit message as a string.
170
+ *
171
+ * This prefix consists of the type and scope of the commit message.
172
+ * If the scope is not provided, it will only return the type.
173
+ *
174
+ * @param prefix An object containing the type and scope of the commit message.
175
+ * @returns The stringified prefix.
176
+ */
177
+ static stringifySemanticCommitMessagePrefix({ type, scope }) {
178
+ return `${type}${scope ? `(${scope})` : ""}`;
179
+ }
180
+ static #parseSemanticCommitMessage(message) {
181
+ const firstColonPosition = message.search(":");
182
+ return {
183
+ prefix: message.slice(0, firstColonPosition).trim(),
184
+ content: message.slice(firstColonPosition + 1).trim()
185
+ };
186
+ }
187
+ /**
188
+ * Format a semantic commit message by capitalizing the content after the prefix.
189
+ *
190
+ * @param message The commit message to format.
191
+ * @returns The formatted commit message.
192
+ */
193
+ static formatSemanticCommitMessage(message) {
194
+ const { prefix, content } = this.#parseSemanticCommitMessage(message);
195
+ if (!prefix || !content) return message;
196
+ return `${prefix}: ${StringUtils.capitalize(content)}`;
197
+ }
198
+ /**
199
+ * Create a semantic commit message from a prefix and a message.
200
+ *
201
+ * @param prefix The prefix of the commit message, containing type and scope.
202
+ * @param message The content of the commit message.
203
+ * @returns The formatted semantic commit message.
204
+ */
205
+ static createSemanticCommitMessage(prefix, message) {
206
+ const [subject, ...comments] = message.split("\n#");
207
+ let commitMessage = this.stringifySemanticCommitMessagePrefix(prefix) + ": ";
208
+ if (subject) commitMessage += StringUtils.capitalize(subject.trim());
209
+ if (comments.length > 0) commitMessage += "\n\n#" + comments.map((comment) => comment.trim()).join("\n#");
210
+ return commitMessage;
211
+ }
212
+ };
213
+ //#endregion
214
+ //#region src/app/scripts/create-commit-message/args.ts
215
+ var commitMessagePathArgSchema = z.string();
216
+ var parseCreateCommitMessageArgs = ([commitMessagePathArg]) => {
217
+ return { commitMessagePath: commitMessagePathArgSchema.parse(commitMessagePathArg) };
218
+ };
219
+ //#endregion
220
+ //#region src/app/scripts/create-commit-message/main.ts
221
+ var main = async (args) => {
222
+ try {
223
+ if (Git.isRebasing()) return 0;
224
+ const { commitMessagePath } = parseCreateCommitMessageArgs(args);
225
+ const commitMessage = (await FileSystem.readFile(commitMessagePath)).toString();
226
+ const commitMessagePrefix = (await CommitLinter.init()).parseSemanticBranchName(Git.getBranchName());
227
+ if (!commitMessage.startsWith(CommitLinter.stringifySemanticCommitMessagePrefix(commitMessagePrefix))) {
228
+ const nextCommitMessage = CommitLinter.createSemanticCommitMessage(commitMessagePrefix, commitMessage);
229
+ await FileSystem.writeFile(commitMessagePath, nextCommitMessage);
230
+ }
231
+ } catch (error) {
232
+ if (error instanceof ZodError) console.warn("Warning: Invalid commit message path");
233
+ if (error instanceof Error) console.warn("Warning:", error.message);
234
+ else console.warn("Warning:", error);
235
+ }
236
+ return 0;
225
237
  };
226
238
  runMain(main);
239
+ //#endregion
240
+ export {};