@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,25 +1,135 @@
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 _origins, _names, _data, _PackageJson_instances, parseGitRepository_fn, _releasesUrl;
10
+ import FileSystem from "node:fs/promises";
1
11
  import * as ActionsCore from "@actions/core";
2
- import FileSystem from "fs/promises";
3
12
  import SemVer from "semver";
4
13
  import { z } from "zod";
5
- class ParseGitRepositoryError extends Error {
6
- }
7
14
  const gitProviderOrigins = {
8
15
  github: "https://github.com"
9
16
  };
10
- const gitProviderNames = Object.keys(gitProviderOrigins);
11
- const isGitProviderName = (name) => gitProviderNames.includes(name);
12
- const parseRepositoryUrl = (urlString) => {
17
+ const _GitProvider = class _GitProvider {
18
+ /**
19
+ * Checks if the provided name is a valid Git provider name.
20
+ *
21
+ * @param name - The name of the Git provider to validate.
22
+ * @returns True if the name is valid, false otherwise.
23
+ */
24
+ static isValidName(name) {
25
+ return __privateGet(this, _names).includes(name);
26
+ }
27
+ /**
28
+ * Returns a Git provider origin.
29
+ *
30
+ * @param name - The name of the Git provider.
31
+ * @returns The origin URL of the Git provider.
32
+ */
33
+ static getOrigin(name) {
34
+ return __privateGet(this, _origins)[name];
35
+ }
36
+ };
37
+ _origins = new WeakMap();
38
+ _names = new WeakMap();
39
+ __privateAdd(_GitProvider, _origins, gitProviderOrigins);
40
+ __privateAdd(_GitProvider, _names, Object.keys(__privateGet(_GitProvider, _origins)));
41
+ let GitProvider = _GitProvider;
42
+ const enginesSchema = z.object({
43
+ node: z.string()
44
+ });
45
+ const repositoryObjectSchema = z.object({
46
+ type: z.string(),
47
+ url: z.string(),
48
+ directory: z.ostring()
49
+ });
50
+ const repositorySchema = z.union([z.string(), repositoryObjectSchema]);
51
+ const packageJsonDataSchema = z.object({
52
+ name: z.string(),
53
+ version: z.string(),
54
+ description: z.string(),
55
+ engines: enginesSchema.optional(),
56
+ repository: repositorySchema.optional()
57
+ });
58
+ const _PackageJson = class _PackageJson {
59
+ constructor(data) {
60
+ __privateAdd(this, _PackageJson_instances);
61
+ __privateAdd(this, _data);
62
+ __privateSet(this, _data, data);
63
+ }
64
+ static async init() {
65
+ const content = await FileSystem.readFile("package.json", { encoding: "utf8" });
66
+ const data = JsonUtils.parse(content);
67
+ const parseResult = packageJsonDataSchema.safeParse(data);
68
+ if (!parseResult.success) {
69
+ throw new Error("Invalid package.json", {
70
+ cause: parseResult.error
71
+ });
72
+ }
73
+ return new _PackageJson(parseResult.data);
74
+ }
75
+ /**
76
+ * Returns the required engines.
77
+ */
78
+ get engines() {
79
+ return __privateGet(this, _data).engines;
80
+ }
81
+ /**
82
+ * Sets the required engines.
83
+ *
84
+ * @param engines - The engines to set.
85
+ */
86
+ set engines(engines) {
87
+ __privateGet(this, _data).engines = engines;
88
+ }
89
+ /**
90
+ * Returns the repository information.
91
+ */
92
+ get repository() {
93
+ return __privateGet(this, _data).repository;
94
+ }
95
+ /**
96
+ * Parses the repository information from package.json.
97
+ *
98
+ * @returns The parsed GitRepository or null if no repository is defined.
99
+ */
100
+ parseGitRepository() {
101
+ if (!this.repository) {
102
+ return null;
103
+ }
104
+ if (typeof this.repository === "string") {
105
+ return __privateMethod(this, _PackageJson_instances, parseGitRepository_fn).call(this, this.repository);
106
+ }
107
+ return __privateMethod(this, _PackageJson_instances, parseGitRepository_fn).call(this, this.repository.url);
108
+ }
109
+ /**
110
+ * Saves the package.json file with the current data.
111
+ */
112
+ async save() {
113
+ const content = JsonUtils.prettify(__privateGet(this, _data)) + "\n";
114
+ await FileSystem.writeFile("package.json", content, { encoding: "utf8" });
115
+ }
116
+ };
117
+ _data = new WeakMap();
118
+ _PackageJson_instances = new WeakSet();
119
+ parseGitRepository_fn = function(urlString) {
120
+ if (!urlString) {
121
+ return null;
122
+ }
13
123
  const urlValue = urlString.includes(":") ? urlString : `https://${urlString}`;
14
124
  const url = new URL(urlValue);
15
125
  const scheme = url.protocol.slice(0, -1);
16
- if (isGitProviderName(scheme)) {
126
+ if (GitProvider.isValidName(scheme)) {
17
127
  const [owner, repositoryName] = url.pathname.split("/");
18
128
  if (!owner || !repositoryName) {
19
- throw new ParseGitRepositoryError("Unknown owner or repositoryName");
129
+ throw new Error("Unknown owner or repositoryName");
20
130
  }
21
131
  return {
22
- origin: gitProviderOrigins[scheme],
132
+ origin: GitProvider.getOrigin(scheme),
23
133
  owner,
24
134
  repositoryName
25
135
  };
@@ -27,7 +137,7 @@ const parseRepositoryUrl = (urlString) => {
27
137
  if (scheme === "https") {
28
138
  const [, owner, repositoryName] = url.pathname.split("/");
29
139
  if (!owner || !repositoryName) {
30
- throw new ParseGitRepositoryError("Unknown owner or repositoryName");
140
+ throw new Error("Unknown owner or repositoryName");
31
141
  }
32
142
  return {
33
143
  origin: url.origin,
@@ -35,53 +145,29 @@ const parseRepositoryUrl = (urlString) => {
35
145
  repositoryName: repositoryName.endsWith(".git") ? repositoryName.slice(0, -4) : repositoryName
36
146
  };
37
147
  }
38
- throw new ParseGitRepositoryError("Unsupported repository URL");
39
- };
40
- const parsePackageJsonRepository = (repository) => {
41
- if (typeof repository === "string") {
42
- return parseRepositoryUrl(repository);
43
- }
44
- return parseRepositoryUrl(repository.url);
45
- };
46
- const enginesSchema = z.object({
47
- node: z.string()
48
- });
49
- const repositoryObjectSchema = z.object({
50
- type: z.string(),
51
- url: z.string(),
52
- directory: z.ostring()
53
- });
54
- const repositorySchema = z.union([z.string(), repositoryObjectSchema]);
55
- const packageJsonSchema = z.object({
56
- name: z.string(),
57
- version: z.string(),
58
- description: z.string(),
59
- engines: enginesSchema.optional(),
60
- repository: repositorySchema.optional()
61
- });
62
- const readPackageJson = async () => {
63
- const packageJsonData = await FileSystem.readFile("package.json", { encoding: "utf8" });
64
- const packageJson = JSON.parse(packageJsonData);
65
- const parseResult = packageJsonSchema.safeParse(packageJson);
66
- if (!parseResult.success) {
67
- throw parseResult.error;
68
- }
69
- return packageJson;
70
- };
71
- const CoreCommands = {
72
- readPackageJson,
73
- parsePackageJsonRepository
148
+ throw new Error("Unsupported repository URL");
74
149
  };
150
+ let PackageJson = _PackageJson;
75
151
  const runMain = (main2) => {
76
- main2(process.argv.slice(2)).then((exitCode) => {
152
+ Promise.resolve().then(() => main2(process.argv.slice(2))).then((exitCode) => {
77
153
  process.exit(exitCode);
78
154
  }).catch((error) => {
79
- console.error(error);
155
+ console.error("Error:", error);
80
156
  process.exit(1);
81
157
  });
82
158
  };
83
- const nodeReleasesUrl = "https://nodejs.org/dist/index.json";
84
- const nodeReleaseSchema = z.object({
159
+ class JsonUtils {
160
+ static stringify(value) {
161
+ return JSON.stringify(value);
162
+ }
163
+ static prettify(value) {
164
+ return JSON.stringify(value, null, 2);
165
+ }
166
+ static parse(value) {
167
+ return JSON.parse(value);
168
+ }
169
+ }
170
+ const nodeJsReleaseSchema = z.object({
85
171
  version: z.string(),
86
172
  // 'v20.5.1',
87
173
  date: z.string(),
@@ -103,25 +189,34 @@ const nodeReleaseSchema = z.object({
103
189
  // false, 'Argon',
104
190
  security: z.boolean()
105
191
  });
106
- const nodeReleasesSchema = z.array(nodeReleaseSchema);
107
- const fetchNodeReleases = async () => {
108
- const response = await fetch(nodeReleasesUrl);
109
- const data = await response.json();
110
- return nodeReleasesSchema.parse(data);
111
- };
112
- const fetchLatestNodeRelease = async () => {
113
- const [latestRelease] = await fetchNodeReleases();
114
- return latestRelease;
115
- };
116
- const NodeJsCommands = {
117
- fetchLatestNodeRelease,
118
- fetchNodeReleases
119
- };
192
+ class NodeJs {
193
+ /**
194
+ * Fetches all Node.js releases.
195
+ *
196
+ * @returns A promise that resolves to an array of NodeJsRelease objects.
197
+ */
198
+ static async fetchReleases() {
199
+ const response = await fetch(__privateGet(this, _releasesUrl));
200
+ const data = await response.json();
201
+ return z.array(nodeJsReleaseSchema).parse(data);
202
+ }
203
+ /**
204
+ * Fetches the latest Node.js release.
205
+ *
206
+ * @returns A promise that resolves to the latest NodeJsRelease object or null if no releases are found.
207
+ */
208
+ static async fetchLatestRelease() {
209
+ const [latestRelease] = await this.fetchReleases();
210
+ return latestRelease ?? null;
211
+ }
212
+ }
213
+ _releasesUrl = new WeakMap();
214
+ __privateAdd(NodeJs, _releasesUrl, "https://nodejs.org/dist/index.json");
120
215
  const updateNvmrc = (version) => FileSystem.writeFile(".nvmrc", `v${version}
121
216
  `);
122
217
  const updatePackageJson = async (version) => {
123
218
  var _a;
124
- const packageJson = await CoreCommands.readPackageJson();
219
+ const packageJson = await PackageJson.init();
125
220
  if (!packageJson.engines) {
126
221
  return;
127
222
  }
@@ -134,12 +229,11 @@ const updatePackageJson = async (version) => {
134
229
  return;
135
230
  }
136
231
  packageJson.engines.node = nodeVersion.replace(coerceNodeVersion, version);
137
- const nextContent = JSON.stringify(packageJson, null, 2) + "\n";
138
- return FileSystem.writeFile("package.json", nextContent);
232
+ return packageJson.save();
139
233
  };
140
234
  const main = async () => {
141
235
  try {
142
- const nodeRelease = await NodeJsCommands.fetchLatestNodeRelease();
236
+ const nodeRelease = await NodeJs.fetchLatestRelease();
143
237
  const nodeVersion = nodeRelease == null ? void 0 : nodeRelease.version;
144
238
  const versionNumber = (nodeVersion == null ? void 0 : nodeVersion.startsWith("v")) ? nodeVersion.slice(1) : nodeVersion;
145
239
  if (!versionNumber) {
@@ -155,7 +249,11 @@ const main = async () => {
155
249
  }
156
250
  return 0;
157
251
  } catch (error) {
158
- console.log(JSON.stringify(error, null, 2));
252
+ if (error instanceof Error) {
253
+ console.error("Error:", error.message);
254
+ } else {
255
+ console.error("Error:", error);
256
+ }
159
257
  return 1;
160
258
  }
161
259
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cluerise/tools",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Tools for maintaining TypeScript projects.",
5
5
  "author": "Branislav Holý <brano@holy.am>",
6
6
  "repository": "github:cluerise/tools",
@@ -17,29 +17,34 @@
17
17
  "./dist/"
18
18
  ],
19
19
  "dependencies": {
20
- "@actions/core": "1.10.1",
21
- "@commitlint/cli": "19.3.0",
22
- "@commitlint/load": "19.2.0",
23
- "@commitlint/types": "19.0.3",
24
- "@html-eslint/eslint-plugin": "0.24.1",
25
- "@html-eslint/parser": "0.24.1",
26
- "@typescript-eslint/eslint-plugin": "7.8.0",
27
- "@typescript-eslint/parser": "7.8.0",
28
- "conventional-changelog-conventionalcommits": "7.0.2",
29
- "eslint": "8.57.0",
30
- "eslint-config-prettier": "9.1.0",
31
- "eslint-plugin-import": "2.29.1",
32
- "eslint-plugin-json": "3.1.0",
33
- "eslint-plugin-markdown": "5.0.0",
34
- "eslint-plugin-prettier": "5.1.3",
35
- "eslint-plugin-simple-import-sort": "12.1.0",
36
- "eslint-plugin-yml": "1.14.0",
37
- "glob": "10.3.14",
38
- "lint-staged": "15.2.2",
39
- "prettier": "3.2.5",
40
- "semantic-release": "23.0.8",
41
- "semver": "7.6.2",
42
- "smol-toml": "1.1.4",
43
- "zod": "3.23.8"
20
+ "@actions/core": "1.11.1",
21
+ "@commitlint/cli": "19.8.1",
22
+ "@commitlint/config-conventional": "19.8.1",
23
+ "@commitlint/load": "19.8.1",
24
+ "@commitlint/types": "19.8.1",
25
+ "@eslint/js": "9.28.0",
26
+ "@eslint/json": "0.12.0",
27
+ "@eslint/markdown": "6.4.0",
28
+ "@html-eslint/eslint-plugin": "0.41.0",
29
+ "@html-eslint/parser": "0.41.0",
30
+ "@typescript-eslint/parser": "8.33.1",
31
+ "conventional-changelog-conventionalcommits": "9.0.0",
32
+ "eslint": "9.28.0",
33
+ "eslint-config-prettier": "10.1.5",
34
+ "eslint-import-resolver-typescript": "4.4.2",
35
+ "eslint-plugin-import": "2.31.0",
36
+ "eslint-plugin-prettier": "5.4.1",
37
+ "eslint-plugin-simple-import-sort": "12.1.1",
38
+ "eslint-plugin-unicorn": "59.0.1",
39
+ "eslint-plugin-yml": "1.18.0",
40
+ "glob": "11.0.2",
41
+ "globals": "16.2.0",
42
+ "lint-staged": "16.1.0",
43
+ "prettier": "3.5.3",
44
+ "semantic-release": "24.2.5",
45
+ "semver": "7.7.2",
46
+ "smol-toml": "1.3.4",
47
+ "typescript-eslint": "8.33.1",
48
+ "zod": "3.25.51"
44
49
  }
45
- }
50
+ }
@@ -1,47 +0,0 @@
1
- # All hidden files and directories
2
- .*
3
-
4
- # Enable hidden files with extensions
5
- !.*.*
6
-
7
- # Enable all hidden directories
8
- !.*/
9
-
10
- # Archives
11
- *.zip
12
- *.gz
13
- *.tgz
14
-
15
- # Build
16
- build/
17
- dist/
18
-
19
- # Dependencies
20
- node_modules/
21
-
22
- # Git
23
- .git/
24
-
25
- # Git hooks
26
- hooks/*
27
- !hooks/*.js
28
- !hooks/*.ts
29
-
30
- # Lock files
31
- package-lock.json
32
- yarn.lock
33
-
34
- # Misc
35
- etc/
36
-
37
- # Sample of .env and .envrc files
38
- .env.example
39
- .env.sample
40
- .envrc.example
41
- .envrc.sample
42
-
43
- # Shell scripts
44
- *.sh
45
-
46
- # Tests coverage
47
- coverage/
@@ -1,114 +0,0 @@
1
- const importRestrictions = {
2
- srcDirectory: {
3
- group: ['src/*'],
4
- message: '\n Error: Do not import from the src directory'
5
- },
6
- nestedDirectories: {
7
- group: ['./*/*'],
8
- message: '\n Error: Do not import from nested directories'
9
- },
10
- nestedModules: {
11
- group: Array.from({ length: 10 }, (_, i) => `:/modules${'/*'.repeat(i + 2)}`),
12
- message: '\n Error: Do not import from nested modules'
13
- },
14
- nestedDirectoriesInParentModules: {
15
- group: Array.from({ length: 10 }, (_, i) => [`${`../`.repeat(i + 1)}*/**`, `!${`../`.repeat(i + 1)}../*`]).flat(),
16
- message: '\n Error: Do not import from nested directories in parent modules'
17
- },
18
- appDirectoryInModules: {
19
- group: [':/app', ':/app/**'],
20
- message: '\n Error: Do not import from the app directory in the modules'
21
- }
22
- };
23
-
24
- const commonImportRestrictions = [
25
- importRestrictions.srcDirectory,
26
- importRestrictions.nestedDirectories,
27
- importRestrictions.nestedModules,
28
- importRestrictions.nestedDirectoriesInParentModules
29
- ];
30
-
31
- module.exports = {
32
- env: {
33
- es6: true,
34
- node: true
35
- },
36
- extends: [
37
- 'eslint:recommended',
38
- 'plugin:@typescript-eslint/recommended',
39
- 'plugin:prettier/recommended',
40
- 'plugin:json/recommended-with-comments',
41
- 'plugin:markdown/recommended-legacy',
42
- 'plugin:yml/standard'
43
- ],
44
- parser: '@typescript-eslint/parser',
45
- parserOptions: {
46
- ecmaVersion: 2018,
47
- sourceType: 'module'
48
- },
49
- plugins: ['import', 'simple-import-sort', '@html-eslint'],
50
- rules: {
51
- '@typescript-eslint/ban-ts-comment': 'off',
52
- '@typescript-eslint/explicit-module-boundary-types': 'off',
53
- '@typescript-eslint/no-unused-vars': 'off',
54
-
55
- 'import/first': 'error',
56
- 'import/newline-after-import': 'error',
57
- 'import/no-amd': 'error',
58
- 'import/no-commonjs': 'error',
59
- 'import/no-default-export': 'error',
60
- 'import/no-duplicates': 'error',
61
- 'import/no-mutable-exports': 'error',
62
- 'import/no-unassigned-import': 'error',
63
- 'import/order': 'off',
64
-
65
- 'no-restricted-imports': ['error', { patterns: commonImportRestrictions }],
66
- 'no-undef-init': 'error',
67
- 'object-shorthand': 'error',
68
-
69
- 'simple-import-sort/exports': 'error',
70
- 'simple-import-sort/imports': 'error',
71
-
72
- 'sort-imports': 'off',
73
-
74
- 'yml/no-empty-mapping-value': 'off',
75
- 'yml/quotes': ['error', { prefer: 'single' }]
76
- },
77
- overrides: [
78
- {
79
- files: ['*.cjs', '*.cts'],
80
- rules: {
81
- 'import/no-commonjs': 'off',
82
- '@typescript-eslint/no-var-requires': 'off'
83
- }
84
- },
85
- {
86
- files: ['*.config.js', '*.config.ts'],
87
- rules: {
88
- 'import/no-default-export': 'off'
89
- }
90
- },
91
- {
92
- files: ['src/modules/**'],
93
- rules: {
94
- 'no-restricted-imports': [
95
- 'error',
96
- { patterns: [importRestrictions.appDirectoryInModules, ...commonImportRestrictions] }
97
- ]
98
- }
99
- },
100
- {
101
- files: ['*.yaml', '*.yml'],
102
- parser: 'yaml-eslint-parser'
103
- },
104
- {
105
- files: ['*.html'],
106
- parser: '@html-eslint/parser',
107
- extends: ['plugin:@html-eslint/recommended'],
108
- rules: {
109
- '@html-eslint/require-title': 'off',
110
- '@html-eslint/indent': ['error', 2]
111
- }
112
- }
113
- ]
114
- };
@@ -1 +0,0 @@
1
- engine-strict = true