@tsparticles/cli-command-build-prettier 4.0.0-beta.12

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.
package/src/utils.ts ADDED
@@ -0,0 +1,285 @@
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import klaw from "klaw";
4
+ import path from "node:path";
5
+ import prettier from "prettier";
6
+
7
+ type PrettierSupportedParser = "typescript" | "json" | "markdown";
8
+
9
+ /**
10
+ * @param basePath -
11
+ * @param parser -
12
+ * @returns -
13
+ */
14
+ async function getPrettierOptions(
15
+ basePath: string,
16
+ parser: PrettierSupportedParser,
17
+ ): Promise<prettier.Options & { parser: PrettierSupportedParser }> {
18
+ const baseOptions = (await prettier.resolveConfig(basePath)) ?? {};
19
+
20
+ return {
21
+ ...baseOptions,
22
+ printWidth: 120,
23
+ endOfLine: "lf",
24
+ tabWidth: 2,
25
+ arrowParens: "avoid",
26
+ parser,
27
+ };
28
+ }
29
+
30
+ /**
31
+ * @param filePath -
32
+ * @param options -
33
+ * @param ci -
34
+ * @param errorMessage -
35
+ */
36
+ async function formatOrCheckFile(
37
+ filePath: string,
38
+ options: prettier.Options,
39
+ ci: boolean,
40
+ errorMessage: string,
41
+ ): Promise<void> {
42
+ const contents = await readFile(filePath, "utf8");
43
+
44
+ if (ci) {
45
+ if (!(await prettier.check(contents, options))) {
46
+ throw new Error(errorMessage);
47
+ }
48
+
49
+ return;
50
+ }
51
+
52
+ const formatted = await prettier.format(contents, options);
53
+
54
+ if (formatted !== contents) {
55
+ await writeFile(filePath, formatted, "utf8");
56
+ }
57
+ }
58
+
59
+ /**
60
+ * @param basePath -
61
+ * @param srcPath -
62
+ * @param ci -
63
+ * @param silent -
64
+ * @returns true if the prettify src process was successful
65
+ */
66
+ export async function prettifySrc(basePath: string, srcPath: string, ci: boolean, silent: boolean): Promise<boolean> {
67
+ if (!silent) {
68
+ console.info("Prettier - started on src");
69
+ }
70
+
71
+ let res: boolean;
72
+
73
+ try {
74
+ const options = await getPrettierOptions(basePath, "typescript");
75
+
76
+ for await (const file of klaw(srcPath)) {
77
+ if (file.stats.isDirectory()) {
78
+ continue;
79
+ }
80
+
81
+ await formatOrCheckFile(file.path, options, ci, `${file.path} is not formatted correctly`);
82
+ }
83
+
84
+ res = true;
85
+ } catch (e) {
86
+ console.error(e);
87
+
88
+ res = false;
89
+ }
90
+
91
+ if (!silent) {
92
+ console.info("Prettier - done on src");
93
+ }
94
+
95
+ return res;
96
+ }
97
+
98
+ /**
99
+ * @param basePath -
100
+ * @param ci -
101
+ * @param silent -
102
+ * @returns true if the prettify package.json process was successful
103
+ */
104
+ export async function prettifyPackageJson(basePath: string, ci: boolean, silent: boolean): Promise<boolean> {
105
+ if (!silent) {
106
+ console.info("Prettier - started on package.json");
107
+ }
108
+
109
+ let res: boolean;
110
+
111
+ try {
112
+ const options = await getPrettierOptions(basePath, "json");
113
+
114
+ await formatOrCheckFile("package.json", options, ci, "package.json is not formatted correctly");
115
+
116
+ res = true;
117
+ } catch (e) {
118
+ console.error(e);
119
+
120
+ res = false;
121
+ }
122
+
123
+ if (!silent) {
124
+ console.info("Prettier - done on package.json");
125
+ }
126
+
127
+ return res;
128
+ }
129
+
130
+ /**
131
+ * @param basePath -
132
+ * @param ci -
133
+ * @param silent -
134
+ * @returns true if the prettify package.dist.json process was successful
135
+ */
136
+ export async function prettifyPackageDistJson(basePath: string, ci: boolean, silent: boolean): Promise<boolean> {
137
+ if (!silent) {
138
+ console.info("Prettier - started on package.dist.json");
139
+ }
140
+
141
+ let res: boolean;
142
+
143
+ try {
144
+ const options = await getPrettierOptions(basePath, "json");
145
+
146
+ await formatOrCheckFile("package.dist.json", options, ci, "package.dist.json is not formatted correctly");
147
+
148
+ res = true;
149
+ } catch (e) {
150
+ console.error(e);
151
+
152
+ res = false;
153
+ }
154
+
155
+ if (!silent) {
156
+ console.info("Prettier - done on package.dist.json");
157
+ }
158
+
159
+ return res;
160
+ }
161
+
162
+ /**
163
+ * @param basePath -
164
+ * @param ci -
165
+ * @param silent -
166
+ * @returns true if the prettify readme process was successful
167
+ */
168
+ export async function prettifyReadme(basePath: string, ci: boolean, silent: boolean): Promise<boolean> {
169
+ if (!silent) {
170
+ console.info("Prettier - started on README.md");
171
+ }
172
+
173
+ let res: boolean;
174
+
175
+ try {
176
+ const options = await getPrettierOptions(basePath, "markdown");
177
+
178
+ await formatOrCheckFile("README.md", options, ci, "README.md is not formatted correctly");
179
+
180
+ res =
181
+ (await prettifyTraductions(basePath, ci, silent)) && (await prettifyMarkdownTypeDocFiles(basePath, ci, silent));
182
+ } catch (e) {
183
+ console.error(e);
184
+
185
+ res = false;
186
+ }
187
+
188
+ if (!silent) {
189
+ console.info("Prettier - done on README.md");
190
+ }
191
+
192
+ return res;
193
+ }
194
+
195
+ /**
196
+ * @param basePath -
197
+ * @param ci -
198
+ * @param silent -
199
+ * @returns true if the prettify traductions process was successful
200
+ */
201
+ async function prettifyTraductions(basePath: string, ci: boolean, silent: boolean): Promise<boolean> {
202
+ if (!silent) {
203
+ console.info("Prettier - started on traductions");
204
+ }
205
+
206
+ let res = false;
207
+
208
+ try {
209
+ const folder = "traduction",
210
+ folderPath = path.join(basePath, folder),
211
+ options = await getPrettierOptions(basePath, "markdown");
212
+
213
+ if (!existsSync(folderPath)) {
214
+ res = true;
215
+ }
216
+
217
+ if (!res) {
218
+ for await (const file of klaw(folderPath)) {
219
+ if (file.stats.isDirectory()) {
220
+ continue;
221
+ }
222
+
223
+ await formatOrCheckFile(file.path, options, ci, `${file.path} is not formatted correctly`);
224
+ }
225
+
226
+ res = true;
227
+ }
228
+ } catch (e) {
229
+ console.error(e);
230
+
231
+ res = false;
232
+ }
233
+
234
+ if (!silent) {
235
+ console.info("Prettier - done on traductions");
236
+ }
237
+
238
+ return res;
239
+ }
240
+
241
+ /**
242
+ * @param basePath -
243
+ * @param ci -
244
+ * @param silent -
245
+ * @returns true if the prettify markdown typedoc files process was successful
246
+ */
247
+ async function prettifyMarkdownTypeDocFiles(basePath: string, ci: boolean, silent: boolean): Promise<boolean> {
248
+ if (!silent) {
249
+ console.info("Prettier - started on markdown");
250
+ }
251
+
252
+ let res = false;
253
+
254
+ try {
255
+ const folder = "markdown",
256
+ folderPath = path.join(basePath, folder),
257
+ options = await getPrettierOptions(basePath, "markdown");
258
+
259
+ if (!existsSync(folderPath)) {
260
+ res = true;
261
+ }
262
+
263
+ if (!res) {
264
+ for await (const file of klaw(folderPath)) {
265
+ if (file.stats.isDirectory()) {
266
+ continue;
267
+ }
268
+
269
+ await formatOrCheckFile(file.path, options, ci, `${file.path} is not formatted correctly`);
270
+ }
271
+
272
+ res = true;
273
+ }
274
+ } catch (e) {
275
+ console.error(e);
276
+
277
+ res = false;
278
+ }
279
+
280
+ if (!silent) {
281
+ console.info("Prettier - done on markdown");
282
+ }
283
+
284
+ return res;
285
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": ".",
4
+ "outDir": ".",
5
+ "resolveJsonModule": true,
6
+ "composite": true,
7
+ "target": "ESNext",
8
+ "module": "NodeNext",
9
+ "moduleResolution": "NodeNext",
10
+ "lib": [
11
+ "ESNext",
12
+ "ES2024",
13
+ "ES2023",
14
+ "ES2022",
15
+ "ES2021",
16
+ "ES2020",
17
+ "ES2019",
18
+ "ES2018",
19
+ "ES2017",
20
+ "ES2016",
21
+ "ES2015"
22
+ ],
23
+ "types": [
24
+ "node",
25
+ "klaw",
26
+ "prompts",
27
+ "eslint"
28
+ ],
29
+ "strict": true,
30
+ "noImplicitAny": true,
31
+ "strictNullChecks": true,
32
+ "strictFunctionTypes": true,
33
+ "strictBindCallApply": true,
34
+ "strictPropertyInitialization": true,
35
+ "noImplicitThis": true,
36
+ "useUnknownInCatchVariables": true,
37
+ "alwaysStrict": true,
38
+ "noUnusedLocals": true,
39
+ "noUnusedParameters": true,
40
+ "exactOptionalPropertyTypes": true,
41
+ "noImplicitReturns": true,
42
+ "noFallthroughCasesInSwitch": true,
43
+ "noUncheckedIndexedAccess": true,
44
+ "noImplicitOverride": true,
45
+ "noPropertyAccessFromIndexSignature": true,
46
+ "esModuleInterop": true,
47
+ "forceConsistentCasingInFileNames": true,
48
+ "allowSyntheticDefaultImports": true
49
+ },
50
+ "include": [
51
+ "src/**/*"
52
+ ]
53
+ }