@cluerise/tools 3.0.0 → 4.0.0

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,9 +1,19 @@
1
- import FileSystem from "fs/promises";
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var _config, _CommitLinter_instances, isValidEnumValue_fn, isValidType_fn, isValidScope_fn, _CommitLinter_static, parseSemanticCommitMessage_fn;
10
+ import FileSystem from "node:fs/promises";
2
11
  import { z, ZodError } from "zod";
12
+ import ChildProcess from "node:child_process";
13
+ import loadCommitlintConfig from "@commitlint/load";
3
14
  import "eslint";
4
15
  import "glob";
5
16
  import "prettier";
6
- import loadCommitlintConfig from "@commitlint/load";
7
17
  const enginesSchema = z.object({
8
18
  node: z.string()
9
19
  });
@@ -21,40 +31,119 @@ z.object({
21
31
  repository: repositorySchema.optional()
22
32
  });
23
33
  const runMain = (main2) => {
24
- main2(process.argv.slice(2)).then((exitCode) => {
34
+ Promise.resolve().then(() => main2(process.argv.slice(2))).then((exitCode) => {
25
35
  process.exit(exitCode);
26
36
  }).catch((error) => {
27
- console.error(error);
37
+ console.error("Error:", error);
28
38
  process.exit(1);
29
39
  });
30
40
  };
31
- const capitalize = (value) => {
32
- const [firstLetter] = value;
33
- if (!firstLetter) {
34
- return value;
41
+ class StringUtils {
42
+ /**
43
+ * Capitalizes the first letter of a given string.
44
+ *
45
+ * @param value - The string to capitalize.
46
+ * @returns The string with the first letter capitalized.
47
+ */
48
+ static capitalize(value) {
49
+ const [firstLetter] = value;
50
+ if (!firstLetter) {
51
+ return value;
52
+ }
53
+ return `${firstLetter.toUpperCase()}${value.slice(1)}`;
35
54
  }
36
- return `${firstLetter.toUpperCase()}${value.slice(1)}`;
37
- };
38
- const StringUtils = {
39
- capitalize
40
- };
41
- z.union([z.literal("all"), z.literal("code")]);
42
- const stringifySemanticCommitMessagePrefix = ({ type, scope }) => `${type}${scope ? `(${scope})` : ""}`;
43
- const startsWithSemanticCommitMessagePrefix = (message, prefix) => message.startsWith(stringifySemanticCommitMessagePrefix(prefix));
44
- const createSemanticCommitMessage = (prefix, message) => {
45
- const [subject, ...comments] = message.split("\n#");
46
- let commitMessage = stringifySemanticCommitMessagePrefix(prefix) + ": ";
47
- if (subject) {
48
- commitMessage += StringUtils.capitalize(subject);
55
+ }
56
+ const _CommitLinter = class _CommitLinter {
57
+ constructor(config) {
58
+ __privateAdd(this, _CommitLinter_instances);
59
+ __privateAdd(this, _config);
60
+ __privateSet(this, _config, config);
49
61
  }
50
- if (comments.length > 0) {
51
- commitMessage += "\n\n#" + comments.join("\n#");
62
+ /**
63
+ * Initializes the CommitLinter with the loaded commitlint configuration.
64
+ *
65
+ * @returns A Promise that resolves to an instance of CommitLinter.
66
+ */
67
+ static async init() {
68
+ const config = await loadCommitlintConfig();
69
+ return new _CommitLinter(config);
70
+ }
71
+ /**
72
+ * Lints commit messages using commitlint.
73
+ *
74
+ * @param args - An array of arguments to pass to commitlint.
75
+ * @returns The exit status of the commitlint command.
76
+ */
77
+ static lint(args) {
78
+ const { status } = ChildProcess.spawnSync("commitlint", args, { stdio: "inherit" });
79
+ return status ?? 0;
80
+ }
81
+ /**
82
+ * Parses a semantic branch name into its type and scope.
83
+ *
84
+ * @param name - The branch name to parse.
85
+ * @returns An object containing the type and scope of the commit message.
86
+ * @throws An error if the branch name is invalid.
87
+ */
88
+ parseSemanticBranchName(name) {
89
+ const [typeValue, scopeValue] = name.split("-");
90
+ if (!typeValue || !__privateMethod(this, _CommitLinter_instances, isValidType_fn).call(this, typeValue)) {
91
+ throw new Error("Invalid commit type in branch name");
92
+ }
93
+ const type = typeValue.toLowerCase();
94
+ const scope = scopeValue && __privateMethod(this, _CommitLinter_instances, isValidScope_fn).call(this, scopeValue) ? scopeValue.toLowerCase() : null;
95
+ return {
96
+ type,
97
+ scope
98
+ };
99
+ }
100
+ /**
101
+ * Returns the prefix of a semantic commit message as a string.
102
+ *
103
+ * This prefix consists of the type and scope of the commit message.
104
+ * If the scope is not provided, it will only return the type.
105
+ * @param prefix - An object containing the type and scope of the commit message.
106
+ * @returns
107
+ */
108
+ static stringifySemanticCommitMessagePrefix({ type, scope }) {
109
+ return `${type}${scope ? `(${scope})` : ""}`;
110
+ }
111
+ /**
112
+ * Formats a semantic commit message by capitalizing the content after the prefix.
113
+ *
114
+ * @param message - The commit message to format.
115
+ * @returns The formatted commit message.
116
+ */
117
+ static formatSemanticCommitMessage(message) {
118
+ const { prefix, content } = __privateMethod(this, _CommitLinter_static, parseSemanticCommitMessage_fn).call(this, message);
119
+ if (!prefix || !content) {
120
+ return message;
121
+ }
122
+ return `${prefix}: ${StringUtils.capitalize(content)}`;
123
+ }
124
+ /**
125
+ * Creates a semantic commit message from a prefix and a message.
126
+ *
127
+ * @param prefix - The prefix of the commit message, containing type and scope.
128
+ * @param message - The content of the commit message.
129
+ * @returns The formatted semantic commit message.
130
+ */
131
+ static createSemanticCommitMessage(prefix, message) {
132
+ const [subject, ...comments] = message.split("\n#");
133
+ let commitMessage = this.stringifySemanticCommitMessagePrefix(prefix) + ": ";
134
+ if (subject) {
135
+ commitMessage += StringUtils.capitalize(subject);
136
+ }
137
+ if (comments.length > 0) {
138
+ commitMessage += "\n\n#" + comments.join("\n#");
139
+ }
140
+ return commitMessage;
52
141
  }
53
- return commitMessage;
54
142
  };
55
- const isValidEnumValue = async (ruleName, value) => {
56
- const { rules } = await loadCommitlintConfig();
57
- const rule = rules[ruleName];
143
+ _config = new WeakMap();
144
+ _CommitLinter_instances = new WeakSet();
145
+ isValidEnumValue_fn = function(ruleName, value) {
146
+ const rule = __privateGet(this, _config).rules[ruleName];
58
147
  if (!rule) {
59
148
  return true;
60
149
  }
@@ -64,43 +153,21 @@ const isValidEnumValue = async (ruleName, value) => {
64
153
  }
65
154
  return values.includes(value);
66
155
  };
67
- const isValidType = (type) => isValidEnumValue("type-enum", type);
68
- const isValidScope = (scope) => isValidEnumValue("scope-enum", scope);
69
- const CommitLintConfig = {
70
- isValidType,
71
- isValidScope
156
+ isValidType_fn = function(type) {
157
+ return __privateMethod(this, _CommitLinter_instances, isValidEnumValue_fn).call(this, "type-enum", type);
72
158
  };
73
- const parseSemanticBranchName = async (name) => {
74
- const [typeValue, scopeValue] = name.split("-");
75
- if (!typeValue || !await CommitLintConfig.isValidType(typeValue)) {
76
- throw new Error("Invalid commit type in branch name");
77
- }
78
- const type = typeValue.toLowerCase();
79
- const scope = scopeValue && await CommitLintConfig.isValidScope(scopeValue) ? scopeValue.toLowerCase() : null;
80
- return {
81
- type,
82
- scope
83
- };
159
+ isValidScope_fn = function(scope) {
160
+ return __privateMethod(this, _CommitLinter_instances, isValidEnumValue_fn).call(this, "scope-enum", scope);
84
161
  };
85
- const parseSemanticCommitMessage = (message) => {
162
+ _CommitLinter_static = new WeakSet();
163
+ parseSemanticCommitMessage_fn = function(message) {
86
164
  const firstColonPosition = message.search(":");
87
165
  const prefix = message.slice(0, firstColonPosition).trim();
88
166
  const content = message.slice(firstColonPosition + 1).trim();
89
167
  return { prefix, content };
90
168
  };
91
- const formatSemanticCommitMessage = (message) => {
92
- const { prefix, content } = parseSemanticCommitMessage(message);
93
- if (!prefix || !content) {
94
- return message;
95
- }
96
- return `${prefix}: ${StringUtils.capitalize(content)}`;
97
- };
98
- const CommitLintCommands = {
99
- createSemanticCommitMessage,
100
- parseSemanticBranchName,
101
- formatSemanticCommitMessage,
102
- startsWithSemanticCommitMessagePrefix
103
- };
169
+ __privateAdd(_CommitLinter, _CommitLinter_static);
170
+ let CommitLinter = _CommitLinter;
104
171
  const commitMessagePathArgSchema = z.string();
105
172
  const parseFormatCommitMessageArgs = ([commitMessagePathArg]) => {
106
173
  const commitMessagePath = commitMessagePathArgSchema.parse(commitMessagePathArg);
@@ -111,19 +178,21 @@ const parseFormatCommitMessageArgs = ([commitMessagePathArg]) => {
111
178
  const main = async (args) => {
112
179
  try {
113
180
  const { commitMessagePath } = parseFormatCommitMessageArgs(args);
114
- const commitMessage = (await FileSystem.readFile(commitMessagePath)).toString();
115
- const formattedCommitMessage = CommitLintCommands.formatSemanticCommitMessage(commitMessage);
181
+ const commitMessageBuffer = await FileSystem.readFile(commitMessagePath);
182
+ const commitMessage = commitMessageBuffer.toString();
183
+ const formattedCommitMessage = CommitLinter.formatSemanticCommitMessage(commitMessage);
116
184
  if (formattedCommitMessage !== commitMessage) {
117
185
  await FileSystem.writeFile(commitMessagePath, formattedCommitMessage);
118
186
  }
119
187
  } catch (error) {
120
188
  if (error instanceof ZodError) {
121
- console.warn("Invalid commit message path");
189
+ console.warn("Warning: Invalid commit message path");
122
190
  }
123
191
  if (error instanceof Error) {
124
- console.warn(error.message);
192
+ console.warn("Warning:", error.message);
193
+ } else {
194
+ console.warn("Warning:", error);
125
195
  }
126
- console.warn(error);
127
196
  }
128
197
  return 0;
129
198
  };