@cluerise/tools 4.1.2 → 4.2.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,12 +1,3 @@
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 _packageName, _CoreConfig_static, rootDirectory_get, _command, _ConsoleStatusLogger_static, createResultMessage_fn, _config, _CommitLinter_instances, isValidEnumValue_fn, isValidType_fn, isValidScope_fn, _CommitLinter_static, parseSemanticCommitMessage_fn, _options, _eslint, _eslintFixer, _FileLinter_static, preparePrettierLintResult_fn, _FileLinter_instances, prettierLint_fn, prepareESLintLintResult_fn, eslintLint_fn;
10
1
  import Path from "node:path";
11
2
  import { z, ZodError } from "zod";
12
3
  import ChildProcess from "node:child_process";
@@ -17,6 +8,7 @@ import { ESLint } from "eslint";
17
8
  import { glob } from "glob";
18
9
  import * as Prettier from "prettier";
19
10
  class CoreConfig {
11
+ static #packageName = "@cluerise/tools";
20
12
  /**
21
13
  * Determines if the application is running in production mode.
22
14
  *
@@ -35,7 +27,10 @@ class CoreConfig {
35
27
  * @returns The path to the configuration files in the package.
36
28
  */
37
29
  static get configPackage() {
38
- return `${__privateGet(this, _packageName)}/dist/configs`;
30
+ return `${this.#packageName}/dist/configs`;
31
+ }
32
+ static get #rootDirectory() {
33
+ return this.isProd ? `./node_modules/${this.#packageName}` : ".";
39
34
  }
40
35
  /**
41
36
  * Returns the directory where configuration files are stored.
@@ -46,20 +41,13 @@ class CoreConfig {
46
41
  * @returns The path to the configuration directory.
47
42
  */
48
43
  static get configDirectory() {
49
- return this.isProd ? `${__privateGet(this, _CoreConfig_static, rootDirectory_get)}/dist/configs` : __privateGet(this, _CoreConfig_static, rootDirectory_get);
44
+ return this.isProd ? `${this.#rootDirectory}/dist/configs` : this.#rootDirectory;
50
45
  }
51
46
  }
52
- _packageName = new WeakMap();
53
- _CoreConfig_static = new WeakSet();
54
- rootDirectory_get = function() {
55
- return this.isProd ? `./node_modules/${__privateGet(this, _packageName)}` : ".";
56
- };
57
- __privateAdd(CoreConfig, _CoreConfig_static);
58
- __privateAdd(CoreConfig, _packageName, "@cluerise/tools");
59
- const _ConsoleStatusLogger = class _ConsoleStatusLogger {
47
+ class ConsoleStatusLogger {
48
+ #command;
60
49
  constructor(command) {
61
- __privateAdd(this, _command);
62
- __privateSet(this, _command, command);
50
+ this.#command = command;
63
51
  }
64
52
  /**
65
53
  * Begins logging for a command with the specified argument.
@@ -71,7 +59,13 @@ const _ConsoleStatusLogger = class _ConsoleStatusLogger {
71
59
  * logger.begin('src/index.ts'); // Logs: "Linting src/index.ts..."
72
60
  */
73
61
  begin(argument) {
74
- console.info(`${__privateGet(this, _command)} ${argument}...`);
62
+ console.info(`${this.#command} ${argument}...`);
63
+ }
64
+ static #createResultMessage(exitCode) {
65
+ if (exitCode === null) {
66
+ return "Done";
67
+ }
68
+ return exitCode === 0 ? "OK" : "Failed";
75
69
  }
76
70
  /**
77
71
  * Ends logging for a command with the specified argument and exit code.
@@ -86,20 +80,9 @@ const _ConsoleStatusLogger = class _ConsoleStatusLogger {
86
80
  * logger.end('src/index.ts', 1); // Logs: "Linting src/index.ts... Failed"
87
81
  */
88
82
  end(argument, exitCode = null) {
89
- var _a;
90
- console.info(`${__privateGet(this, _command)} ${argument}... ${__privateMethod(_a = _ConsoleStatusLogger, _ConsoleStatusLogger_static, createResultMessage_fn).call(_a, exitCode)}`);
83
+ console.info(`${this.#command} ${argument}... ${ConsoleStatusLogger.#createResultMessage(exitCode)}`);
91
84
  }
92
- };
93
- _command = new WeakMap();
94
- _ConsoleStatusLogger_static = new WeakSet();
95
- createResultMessage_fn = function(exitCode) {
96
- if (exitCode === null) {
97
- return "Done";
98
- }
99
- return exitCode === 0 ? "OK" : "Failed";
100
- };
101
- __privateAdd(_ConsoleStatusLogger, _ConsoleStatusLogger_static);
102
- let ConsoleStatusLogger = _ConsoleStatusLogger;
85
+ }
103
86
  const enginesSchema = z.object({
104
87
  node: z.string()
105
88
  });
@@ -139,11 +122,10 @@ class StringUtils {
139
122
  return `${firstLetter.toUpperCase()}${value.slice(1)}`;
140
123
  }
141
124
  }
142
- const _CommitLinter = class _CommitLinter {
125
+ class CommitLinter {
126
+ #config;
143
127
  constructor(config) {
144
- __privateAdd(this, _CommitLinter_instances);
145
- __privateAdd(this, _config);
146
- __privateSet(this, _config, config);
128
+ this.#config = config;
147
129
  }
148
130
  /**
149
131
  * Initializes the CommitLinter with the loaded commitlint configuration.
@@ -152,7 +134,7 @@ const _CommitLinter = class _CommitLinter {
152
134
  */
153
135
  static async init() {
154
136
  const config = await loadCommitlintConfig();
155
- return new _CommitLinter(config);
137
+ return new CommitLinter(config);
156
138
  }
157
139
  /**
158
140
  * Lints commit messages using commitlint.
@@ -164,6 +146,23 @@ const _CommitLinter = class _CommitLinter {
164
146
  const { status } = ChildProcess.spawnSync("commitlint", args, { stdio: "inherit" });
165
147
  return status ?? 0;
166
148
  }
149
+ #isValidEnumValue(ruleName, value) {
150
+ const rule = this.#config.rules[ruleName];
151
+ if (!rule) {
152
+ return true;
153
+ }
154
+ const [_severity, _condition, values] = rule;
155
+ if (!values) {
156
+ return true;
157
+ }
158
+ return values.includes(value);
159
+ }
160
+ #isValidType(type) {
161
+ return this.#isValidEnumValue("type-enum", type);
162
+ }
163
+ #isValidScope(scope) {
164
+ return this.#isValidEnumValue("scope-enum", scope);
165
+ }
167
166
  /**
168
167
  * Parses a semantic branch name into its type and scope.
169
168
  *
@@ -173,11 +172,11 @@ const _CommitLinter = class _CommitLinter {
173
172
  */
174
173
  parseSemanticBranchName(name) {
175
174
  const [typeValue, scopeValue] = name.split("-");
176
- if (!typeValue || !__privateMethod(this, _CommitLinter_instances, isValidType_fn).call(this, typeValue)) {
175
+ if (!typeValue || !this.#isValidType(typeValue)) {
177
176
  throw new Error("Invalid commit type in branch name");
178
177
  }
179
178
  const type = typeValue.toLowerCase();
180
- const scope = scopeValue && __privateMethod(this, _CommitLinter_instances, isValidScope_fn).call(this, scopeValue) ? scopeValue.toLowerCase() : null;
179
+ const scope = scopeValue && this.#isValidScope(scopeValue) ? scopeValue.toLowerCase() : null;
181
180
  return {
182
181
  type,
183
182
  scope
@@ -194,6 +193,12 @@ const _CommitLinter = class _CommitLinter {
194
193
  static stringifySemanticCommitMessagePrefix({ type, scope }) {
195
194
  return `${type}${scope ? `(${scope})` : ""}`;
196
195
  }
196
+ static #parseSemanticCommitMessage(message) {
197
+ const firstColonPosition = message.search(":");
198
+ const prefix = message.slice(0, firstColonPosition).trim();
199
+ const content = message.slice(firstColonPosition + 1).trim();
200
+ return { prefix, content };
201
+ }
197
202
  /**
198
203
  * Formats a semantic commit message by capitalizing the content after the prefix.
199
204
  *
@@ -201,7 +206,7 @@ const _CommitLinter = class _CommitLinter {
201
206
  * @returns The formatted commit message.
202
207
  */
203
208
  static formatSemanticCommitMessage(message) {
204
- const { prefix, content } = __privateMethod(this, _CommitLinter_static, parseSemanticCommitMessage_fn).call(this, message);
209
+ const { prefix, content } = this.#parseSemanticCommitMessage(message);
205
210
  if (!prefix || !content) {
206
211
  return message;
207
212
  }
@@ -225,50 +230,21 @@ const _CommitLinter = class _CommitLinter {
225
230
  }
226
231
  return commitMessage;
227
232
  }
228
- };
229
- _config = new WeakMap();
230
- _CommitLinter_instances = new WeakSet();
231
- isValidEnumValue_fn = function(ruleName, value) {
232
- const rule = __privateGet(this, _config).rules[ruleName];
233
- if (!rule) {
234
- return true;
235
- }
236
- const [_severity, _condition, values] = rule;
237
- if (!values) {
238
- return true;
239
- }
240
- return values.includes(value);
241
- };
242
- isValidType_fn = function(type) {
243
- return __privateMethod(this, _CommitLinter_instances, isValidEnumValue_fn).call(this, "type-enum", type);
244
- };
245
- isValidScope_fn = function(scope) {
246
- return __privateMethod(this, _CommitLinter_instances, isValidEnumValue_fn).call(this, "scope-enum", scope);
247
- };
248
- _CommitLinter_static = new WeakSet();
249
- parseSemanticCommitMessage_fn = function(message) {
250
- const firstColonPosition = message.search(":");
251
- const prefix = message.slice(0, firstColonPosition).trim();
252
- const content = message.slice(firstColonPosition + 1).trim();
253
- return { prefix, content };
254
- };
255
- __privateAdd(_CommitLinter, _CommitLinter_static);
256
- let CommitLinter = _CommitLinter;
257
- const _FileLinter = class _FileLinter {
233
+ }
234
+ class FileLinter {
235
+ #options;
236
+ #eslint;
237
+ #eslintFixer = null;
258
238
  constructor(options) {
259
- __privateAdd(this, _FileLinter_instances);
260
- __privateAdd(this, _options);
261
- __privateAdd(this, _eslint);
262
- __privateAdd(this, _eslintFixer, null);
263
- __privateSet(this, _options, options ?? null);
264
- __privateSet(this, _eslint, new ESLint({
265
- overrideConfigFile: options == null ? void 0 : options.configPath
266
- }));
267
- if (options == null ? void 0 : options.fix) {
268
- __privateSet(this, _eslintFixer, new ESLint({
239
+ this.#options = options ?? null;
240
+ this.#eslint = new ESLint({
241
+ overrideConfigFile: options?.configPath
242
+ });
243
+ if (options?.fix) {
244
+ this.#eslintFixer = new ESLint({
269
245
  fix: options.fix,
270
- overrideConfigFile: options == null ? void 0 : options.configPath
271
- }));
246
+ overrideConfigFile: options?.configPath
247
+ });
272
248
  }
273
249
  }
274
250
  /**
@@ -281,6 +257,91 @@ const _FileLinter = class _FileLinter {
281
257
  const { status } = ChildProcess.spawnSync("lint-staged", args, { stdio: "inherit" });
282
258
  return status ?? 0;
283
259
  }
260
+ static #preparePrettierLintResult(results) {
261
+ if (results.length === 0) {
262
+ return { status: "success" };
263
+ }
264
+ const title = `Linting with Prettier failed with ${results.length} problem${results.length === 1 ? "" : "s"}
265
+ `;
266
+ let message = "Prettier failed with the following files:\n";
267
+ message += results.map((result) => `- ${result.path}`).join("\n");
268
+ message += OS.EOL;
269
+ return {
270
+ status: "failure",
271
+ title,
272
+ message,
273
+ problemCount: results.length
274
+ };
275
+ }
276
+ async #isIgnoredByPrettier(path) {
277
+ if (path.endsWith(".sh")) {
278
+ return false;
279
+ }
280
+ return this.#eslint.isPathIgnored(path);
281
+ }
282
+ async #prettierLint(patterns) {
283
+ const configPath = await Prettier.resolveConfigFile();
284
+ if (!configPath) {
285
+ return [];
286
+ }
287
+ const config = await Prettier.resolveConfig(configPath);
288
+ if (!config) {
289
+ return [];
290
+ }
291
+ const paths = await glob(patterns, {
292
+ nodir: true,
293
+ ignore: "node_modules/**"
294
+ });
295
+ const results = await Promise.all(
296
+ paths.map(async (path) => {
297
+ if (await this.#isIgnoredByPrettier(path)) {
298
+ return null;
299
+ }
300
+ const contentBuffer = await FileSystem.readFile(path);
301
+ const content = contentBuffer.toString();
302
+ if (this.#options?.fix) {
303
+ const nextContent = await Prettier.format(content, {
304
+ ...config,
305
+ filepath: path
306
+ });
307
+ await FileSystem.writeFile(path, nextContent);
308
+ return null;
309
+ }
310
+ const result = await Prettier.check(content, {
311
+ ...config,
312
+ filepath: path
313
+ });
314
+ return result ? null : { path };
315
+ })
316
+ );
317
+ return results.filter((result) => result !== null);
318
+ }
319
+ async #prepareESLintLintResult(results) {
320
+ const problemCount = results.reduce(
321
+ (sum, result) => sum + result.errorCount + result.warningCount + (this.#options?.fix ? -(result.fixableErrorCount + result.fixableWarningCount) : 0),
322
+ 0
323
+ );
324
+ if (problemCount === 0) {
325
+ return { status: "success" };
326
+ }
327
+ const title = `ESLint failed with ${problemCount} problem${problemCount === 1 ? "" : "s"}`;
328
+ const formatter = await this.#eslint.loadFormatter("stylish");
329
+ const message = await formatter.format(results);
330
+ return {
331
+ status: "failure",
332
+ title,
333
+ message,
334
+ problemCount
335
+ };
336
+ }
337
+ async #eslintLint(patterns) {
338
+ const results = await this.#eslint.lintFiles(patterns);
339
+ const errorResults = ESLint.getErrorResults(results);
340
+ if (this.#eslintFixer && errorResults.length > 0) {
341
+ await ESLint.outputFixes(await this.#eslintFixer.lintFiles(patterns));
342
+ }
343
+ return results;
344
+ }
284
345
  /**
285
346
  * Lints files matching the provided patterns using ESLint and Prettier.
286
347
  *
@@ -288,14 +349,13 @@ const _FileLinter = class _FileLinter {
288
349
  * @returns A promise that resolves to a LintFilesResult indicating the success or failure of the linting process.
289
350
  */
290
351
  async lint(patterns = ["**"]) {
291
- var _a;
292
- const prettierResults = await __privateMethod(this, _FileLinter_instances, prettierLint_fn).call(this, patterns);
293
- const eslintResults = await __privateMethod(this, _FileLinter_instances, eslintLint_fn).call(this, patterns);
294
- const eslintResult = await __privateMethod(this, _FileLinter_instances, prepareESLintLintResult_fn).call(this, eslintResults);
352
+ const prettierResults = await this.#prettierLint(patterns);
353
+ const eslintResults = await this.#eslintLint(patterns);
354
+ const eslintResult = await this.#prepareESLintLintResult(eslintResults);
295
355
  if (eslintResult.status === "failure") {
296
356
  return eslintResult;
297
357
  }
298
- const prettierResult = __privateMethod(_a = _FileLinter, _FileLinter_static, preparePrettierLintResult_fn).call(_a, prettierResults);
358
+ const prettierResult = FileLinter.#preparePrettierLintResult(prettierResults);
299
359
  if (prettierResult.status === "failure") {
300
360
  return prettierResult;
301
361
  }
@@ -303,97 +363,7 @@ const _FileLinter = class _FileLinter {
303
363
  status: "success"
304
364
  };
305
365
  }
306
- };
307
- _options = new WeakMap();
308
- _eslint = new WeakMap();
309
- _eslintFixer = new WeakMap();
310
- _FileLinter_static = new WeakSet();
311
- preparePrettierLintResult_fn = function(results) {
312
- if (results.length === 0) {
313
- return { status: "success" };
314
- }
315
- const title = `Linting with Prettier failed with ${results.length} problem${results.length === 1 ? "" : "s"}
316
- `;
317
- let message = "Prettier failed with the following files:\n";
318
- message += results.map((result) => `- ${result.path}`).join("\n");
319
- message += OS.EOL;
320
- return {
321
- status: "failure",
322
- title,
323
- message,
324
- problemCount: results.length
325
- };
326
- };
327
- _FileLinter_instances = new WeakSet();
328
- prettierLint_fn = async function(patterns) {
329
- const configPath = await Prettier.resolveConfigFile();
330
- if (!configPath) {
331
- return [];
332
- }
333
- const config = await Prettier.resolveConfig(configPath);
334
- if (!config) {
335
- return [];
336
- }
337
- const paths = await glob(patterns, {
338
- nodir: true,
339
- ignore: "node_modules/**"
340
- });
341
- const results = await Promise.all(
342
- paths.map(async (path) => {
343
- var _a;
344
- if (await __privateGet(this, _eslint).isPathIgnored(path)) {
345
- return null;
346
- }
347
- const contentBuffer = await FileSystem.readFile(path);
348
- const content = contentBuffer.toString();
349
- if ((_a = __privateGet(this, _options)) == null ? void 0 : _a.fix) {
350
- const nextContent = await Prettier.format(content, {
351
- ...config,
352
- filepath: path
353
- });
354
- await FileSystem.writeFile(path, nextContent);
355
- return null;
356
- }
357
- const result = await Prettier.check(content, {
358
- ...config,
359
- filepath: path
360
- });
361
- return result ? null : { path };
362
- })
363
- );
364
- return results.filter((result) => result !== null);
365
- };
366
- prepareESLintLintResult_fn = async function(results) {
367
- const problemCount = results.reduce(
368
- (sum, result) => {
369
- var _a;
370
- return sum + result.errorCount + result.warningCount + (((_a = __privateGet(this, _options)) == null ? void 0 : _a.fix) ? -(result.fixableErrorCount + result.fixableWarningCount) : 0);
371
- },
372
- 0
373
- );
374
- if (problemCount === 0) {
375
- return { status: "success" };
376
- }
377
- const title = `ESLint failed with ${problemCount} problem${problemCount === 1 ? "" : "s"}`;
378
- const formatter = await __privateGet(this, _eslint).loadFormatter("stylish");
379
- const message = await formatter.format(results);
380
- return {
381
- status: "failure",
382
- title,
383
- message,
384
- problemCount
385
- };
386
- };
387
- eslintLint_fn = async function(patterns) {
388
- const results = await __privateGet(this, _eslint).lintFiles(patterns);
389
- const errorResults = ESLint.getErrorResults(results);
390
- if (__privateGet(this, _eslintFixer) && errorResults.length > 0) {
391
- await ESLint.outputFixes(await __privateGet(this, _eslintFixer).lintFiles(patterns));
392
- }
393
- return results;
394
- };
395
- __privateAdd(_FileLinter, _FileLinter_static);
396
- let FileLinter = _FileLinter;
366
+ }
397
367
  const lintScopeArgSchema = z.union([z.literal("all"), z.literal("fix"), z.literal("commit"), z.literal("staged")]);
398
368
  const parseLintArgs = ([scopeArg]) => {
399
369
  const scope = lintScopeArgSchema.parse(scopeArg);
@@ -448,7 +418,7 @@ const main = async (args) => {
448
418
  } catch (error) {
449
419
  if (error instanceof ZodError) {
450
420
  const firstIssue = error.issues[0];
451
- if ((firstIssue == null ? void 0 : firstIssue.code) === "invalid_union") {
421
+ if (firstIssue?.code === "invalid_union") {
452
422
  console.error("Error: Invalid scope");
453
423
  return 1;
454
424
  }