fabrique 0.2.4 → 0.3.3

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.
@@ -5,4 +5,4 @@ npmRegistryServer: "http://localhost:4873"
5
5
  unsafeHttpWhitelist:
6
6
  - localhost
7
7
 
8
- yarnPath: .yarn/releases/yarn-4.6.0.cjs
8
+ yarnPath: .yarn/releases/yarn-4.7.0.cjs
@@ -40,7 +40,7 @@ yarn install
40
40
  - `fb:test`: runs the tests using `vitest`.
41
41
  - `fb:prod`: builds the lib in `prod` mode.
42
42
  - builds and publishes the lib on npm as a _prod_ version.
43
- - `fb:dev`: builds the lib in `dev` .
43
+ - `fb:dev`: builds the lib in `dev` mode.
44
44
  - builds and publishes the lib on a local `verdaccio` with a `dev` tag.
45
- - `fb:rc`: builds the lib in `rc` mode, and publishes it on a local `verdaccio`.
45
+ - `fb:rc`: builds the lib in `rc` mode.
46
46
  - builds and publishes the lib on npm with a `rc` tag.
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2024 {{author}}
3
+ Copyright (c) 2025 {{author}}
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -5,6 +5,7 @@
5
5
 
6
6
  ## {{libname}}
7
7
 
8
+ {{description}}
8
9
 
9
10
  ## 📦 Installation
10
11
 
@@ -13,3 +14,7 @@ yarn add {{libname}}
13
14
  # or
14
15
  npm install {{libname}} --save
15
16
  ```
17
+
18
+ ## 📜 Documentation
19
+
20
+ TODO
@@ -1,6 +1,9 @@
1
- import { cp, glob, readFile, rm, writeFile } from 'node:fs/promises';
1
+ import { cp, readFile, rm, writeFile } from 'node:fs/promises';
2
2
  import { isAbsolute, join } from 'node:path';
3
+ import process from 'node:process';
4
+ import { ROOT_PATH } from '../constants/root-path.constant.js';
3
5
  import { cmd } from '../helpers/cmd.js';
6
+ import { exploreDirectoryFiles } from '../helpers/explore-directory.js';
4
7
 
5
8
  /**
6
9
  * Builds the lib.
@@ -8,9 +11,8 @@ import { cmd } from '../helpers/cmd.js';
8
11
  * @return {Promise<void>}
9
12
  */
10
13
  export async function build({ mode = 'prod' } = {}) {
11
- const rootPath = '.';
12
- const sourcePath = './src';
13
- const destinationPath = './dist';
14
+ const sourcePath = join(ROOT_PATH, 'src');
15
+ const destinationPath = join(ROOT_PATH, 'dist');
14
16
 
15
17
  await removeDestination(destinationPath);
16
18
 
@@ -18,7 +20,7 @@ export async function build({ mode = 'prod' } = {}) {
18
20
  const [withProtected] = await Promise.all([
19
21
  buildTypescript(sourcePath),
20
22
  buildScss(sourcePath, destinationPath),
21
- copyOtherFiles(rootPath, destinationPath),
23
+ copyOtherFiles(ROOT_PATH, destinationPath),
22
24
  ]);
23
25
 
24
26
  await buildPackageJsonFile(destinationPath, {
@@ -43,6 +45,49 @@ async function removeDestination(destinationPath) {
43
45
  await rm(destinationPath, { recursive: true, force: true });
44
46
  }
45
47
 
48
+ /**
49
+ * Returns true if `path` is a `protected` directory.
50
+ *
51
+ * @param {string} path
52
+ * @return boolean
53
+ */
54
+ function isProtectedDirectory(path) {
55
+ return path.endsWith('.protected');
56
+ }
57
+
58
+ /**
59
+ * Returns true if `path` is inside a `protected` directory.
60
+ *
61
+ * @param {string} path
62
+ * @return boolean
63
+ */
64
+ function isInProtectedDirectory(path) {
65
+ return path.includes('.protected/');
66
+ }
67
+
68
+ /**
69
+ * Returns true if `path` is a `private` directory.
70
+ *
71
+ * @param {string} path
72
+ * @return boolean
73
+ */
74
+
75
+ function isPrivateDirectory(path) {
76
+ return path.endsWith('.private');
77
+ }
78
+
79
+ /**
80
+ * Returns true if `path` is inside a `private` directory.
81
+ *
82
+ * @param {string} path
83
+ * @return boolean
84
+ */
85
+ function isInPrivateDirectory(path) {
86
+ return path.includes('.private/');
87
+ }
88
+
89
+ /* TYPESCRIPT */
90
+
46
91
  /**
47
92
  * Builds the typescript part.
48
93
  *
@@ -66,52 +111,97 @@ async function buildTypescript(sourcePath) {
66
111
  return typescriptProtectedIndexFilePath !== null;
67
112
  }
68
113
 
114
+ function generateExportEsmLine(path) {
115
+ return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';\n`;
116
+ }
117
+
69
118
  /**
70
- * Builds the scss part.
119
+ * Returns true if `path` is a `typescript` file.
71
120
  *
72
- * @param {string} sourcePath
73
- * @param {string} destinationPath
74
- * @return {Promise<void>}
121
+ * @param {string} path
122
+ * @return boolean
75
123
  */
76
- async function buildScss(sourcePath, destinationPath) {
77
- await copyScssFiles(sourcePath, destinationPath);
78
- await buildScssIndexFile(destinationPath);
124
+ function isTsFile(path) {
125
+ return path.endsWith('.ts');
126
+ }
127
+
128
+ /**
129
+ * Returns true if `path` is a typescript `test or spec` file.
130
+ *
131
+ * @param {string} path
132
+ * @return boolean
133
+ */
134
+ function isTsTestOrSpecFile(path) {
135
+ return path.endsWith('.test.ts') || path.endsWith('.spec.ts');
136
+ }
137
+
138
+ /**
139
+ * Returns true if `path` is a typescript `bench` file.
140
+ *
141
+ * @param {string} path
142
+ * @return boolean
143
+ */
144
+ function isTsBenchFile(path) {
145
+ return path.endsWith('.bench.ts');
146
+ }
147
+
148
+ /**
149
+ * Returns true if `path` is a typescript `protected` file.
150
+ *
151
+ * @param {string} path
152
+ * @return boolean
153
+ */
154
+ function isTsProtectedFile(path) {
155
+ return path.endsWith('.protected.ts');
156
+ }
157
+
158
+ /**
159
+ * Returns true if `path` is a typescript `private` file.
160
+ *
161
+ * @param {string} path
162
+ * @return boolean
163
+ */
164
+ function isTsPrivateFile(path) {
165
+ return path.endsWith('.private.ts');
79
166
  }
80
167
 
81
168
  /**
82
169
  * Builds the typescript index file used to export all public APIs.
83
170
  *
84
- * @param {string} cwd
171
+ * @param {string} sourcePath
85
172
  * @return {Promise<string>}
86
173
  */
87
- async function buildTypescriptIndexFile(cwd = process.cwd()) {
174
+ async function buildTypescriptIndexFile(sourcePath) {
88
175
  console.log('Building typescript index file...');
89
176
 
90
- const content = (
91
- await Array.fromAsync(
92
- glob('./**/*.ts', {
93
- cwd,
94
- exclude: (path) => {
95
- return (
96
- path.endsWith('.spec.ts') ||
97
- path.endsWith('.test.ts') ||
98
- path.includes('.private') ||
99
- path.includes('.protected')
100
- );
101
- },
102
- }),
103
- )
104
- )
105
- .map((path) => {
106
- return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';`;
107
- })
108
- .join('\n');
177
+ let content = '';
178
+
179
+ for await (const path of exploreDirectoryFiles(sourcePath, {
180
+ relativeTo: sourcePath,
181
+ pick: (path, { isFile }) => {
182
+ if (isFile) {
183
+ return (
184
+ isTsFile(path) &&
185
+ !(
186
+ isTsTestOrSpecFile(path) ||
187
+ isTsBenchFile(path) ||
188
+ isTsProtectedFile(path) ||
189
+ isTsPrivateFile(path)
190
+ )
191
+ );
192
+ } else {
193
+ return !(isProtectedDirectory(path) || isPrivateDirectory(path));
194
+ }
195
+ },
196
+ })) {
197
+ content += generateExportEsmLine(path);
198
+ }
109
199
 
110
200
  if (content === '') {
111
201
  throw new Error('Nothing exported.');
112
202
  }
113
203
 
114
- const indexFilePath = join(cwd, 'index.ts');
204
+ const indexFilePath = join(sourcePath, 'index.ts');
115
205
  await writeFile(indexFilePath, content + '\n');
116
206
 
117
207
  return indexFilePath;
@@ -120,34 +210,37 @@ async function buildTypescriptIndexFile(cwd = process.cwd()) {
120
210
  /**
121
211
  * Builds the typescript index file used to export all protected APIs.
122
212
  *
123
- * @param {string} cwd
213
+ * @param {string} sourcePath
124
214
  * @return {Promise<string | null>}
125
215
  */
126
- async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
216
+ async function buildTypescriptProtectedIndexFile(sourcePath) {
127
217
  console.log('Building typescript protected index file...');
128
218
 
129
- const content = (
130
- await Array.fromAsync(
131
- glob('{./**/*.protected/**/*.ts,./**/*.protected.ts,}', {
132
- cwd,
133
- exclude: (path) => {
134
- return (
135
- path.endsWith('.spec.ts') || path.endsWith('.test.ts') || path.includes('.private')
136
- );
137
- },
138
- }),
139
- )
140
- )
141
- .map((path) => {
142
- return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';`;
143
- })
144
- .join('\n');
219
+ let content = '';
220
+
221
+ for await (const path of exploreDirectoryFiles(sourcePath, {
222
+ relativeTo: sourcePath,
223
+ pick: (path, { isFile }) => {
224
+ if (isFile) {
225
+ return (
226
+ isTsProtectedFile(path) ||
227
+ (isInProtectedDirectory(path) &&
228
+ isTsFile(path) &&
229
+ !(isTsTestOrSpecFile(path) || isTsBenchFile(path) || isTsPrivateFile(path)))
230
+ );
231
+ } else {
232
+ return !isPrivateDirectory(path);
233
+ }
234
+ },
235
+ })) {
236
+ content += generateExportEsmLine(path);
237
+ }
145
238
 
146
239
  if (content === '') {
147
240
  return null;
148
241
  }
149
242
 
150
- const indexFilePath = join(cwd, 'index.protected.ts');
243
+ const indexFilePath = join(sourcePath, 'index.protected.ts');
151
244
  await writeFile(indexFilePath, content + '\n');
152
245
 
153
246
  return indexFilePath;
@@ -156,13 +249,13 @@ async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
156
249
  /**
157
250
  * Compiles the typescript files.
158
251
  *
159
- * @param {string | undefined } cwd
252
+ * @param {string | undefined } rootPath
160
253
  * @return {Promise<void>}
161
254
  */
162
- async function compileTypescript(cwd = process.cwd()) {
255
+ async function compileTypescript(rootPath = process.cwd()) {
163
256
  console.log('Compiling typescript...');
164
257
 
165
- await cmd('tsc', ['-p', './tsconfig.build.json'], { cwd });
258
+ await cmd('tsc', ['-p', './tsconfig.build.json'], { cwd: rootPath });
166
259
  }
167
260
 
168
261
  /**
@@ -175,31 +268,112 @@ async function compileTypescript(cwd = process.cwd()) {
175
268
  async function copyTypescriptFiles(sourcePath, destinationPath) {
176
269
  console.log('Copying typescript files...');
177
270
 
178
- for await (const path of glob('./**/!(*.spec|*.test).ts', { cwd: sourcePath })) {
179
- await cp(join(sourcePath, path), join(destinationPath, path));
180
- }
271
+ throw 'TODO';
272
+ // for await (const path of exploreDirectoryFiles(sourcePath, {
273
+ // relativeTo: sourcePath,
274
+ // pick: (path, { isFile }) => {
275
+ // if (isFile) {
276
+ // return (
277
+ // path.endsWith('.ts') &&
278
+ // !path.endsWith('.spec.ts') &&
279
+ // !path.endsWith('.test.ts') &&
280
+ // !path.endsWith('.bench.ts')
281
+ // );
282
+ // } else {
283
+ // return true;
284
+ // }
285
+ // },
286
+ // })) {
287
+ // await cp(join(sourcePath, path), join(destinationPath, path));
288
+ // }
181
289
  }
182
290
 
183
291
  /**
184
- * Builds the typescript index file used to export all public APIs.
292
+ * Removes the index file.
293
+ *
294
+ * @param {string} indexFilePath
295
+ * @return {Promise<void>}
296
+ */
297
+ async function removeTypescriptIndexFile(indexFilePath) {
298
+ await rm(indexFilePath);
299
+ }
300
+
301
+ /* SCSS*/
302
+
303
+ /**
304
+ * Builds the scss part.
185
305
  *
186
- * @param {string} cwd
306
+ * @param {string} sourcePath
307
+ * @param {string} destinationPath
187
308
  * @return {Promise<void>}
188
309
  */
189
- async function buildScssIndexFile(cwd = process.cwd()) {
310
+ async function buildScss(sourcePath, destinationPath) {
311
+ await copyScssFiles(sourcePath, destinationPath);
312
+ await buildScssIndexFile(destinationPath);
313
+ }
314
+
315
+ function generateExportScssLine(path) {
316
+ return `@forward './${path.slice(0, -5)}';\n`;
317
+ }
318
+
319
+ /**
320
+ * Returns true if `path` is a `scss` file.
321
+ *
322
+ * @param {string} path
323
+ * @return boolean
324
+ */
325
+ function isScssFile(path) {
326
+ return path.endsWith('.scss');
327
+ }
328
+
329
+ /**
330
+ * Returns true if `path` is a scss `protected` file.
331
+ *
332
+ * @param {string} path
333
+ * @return boolean
334
+ */
335
+ function isScssProtectedFile(path) {
336
+ return path.endsWith('.protected.scss');
337
+ }
338
+
339
+ /**
340
+ * Returns true if `path` is a scss `private` file.
341
+ *
342
+ * @param {string} path
343
+ * @return boolean
344
+ */
345
+ function isScssPrivateFile(path) {
346
+ return path.endsWith('.private.scss');
347
+ }
348
+
349
+ /**
350
+ * Builds the scss index file used to export all public styles.
351
+ *
352
+ * @param {string} sourcePath
353
+ * @return {Promise<void>}
354
+ */
355
+ async function buildScssIndexFile(sourcePath) {
190
356
  console.log('Building scss index file...');
191
357
 
192
- // TODO exclude directories too
193
- const content = (await Array.fromAsync(glob('./**/!(*.private).scss', { cwd })))
194
- .map((path) => {
195
- return `@forward './${path.slice(0, -5)}';`;
196
- })
197
- .join('\n');
358
+ let content = '';
359
+
360
+ for await (const path of exploreDirectoryFiles(sourcePath, {
361
+ relativeTo: sourcePath,
362
+ pick: (path, { isFile }) => {
363
+ if (isFile) {
364
+ return isScssFile(path) && !(isScssProtectedFile(path) && isScssPrivateFile(path));
365
+ } else {
366
+ return !(isProtectedDirectory(path) || isPrivateDirectory(path));
367
+ }
368
+ },
369
+ })) {
370
+ content += generateExportScssLine(path);
371
+ }
198
372
 
199
373
  if (content === '') {
200
374
  console.log('=> No scss file to export.');
201
375
  } else {
202
- const indexFilePath = join(cwd, 'index.scss');
376
+ const indexFilePath = join(sourcePath, 'index.scss');
203
377
  await writeFile(indexFilePath, content + '\n');
204
378
  }
205
379
  }
@@ -214,7 +388,16 @@ async function buildScssIndexFile(cwd = process.cwd()) {
214
388
  async function copyScssFiles(sourcePath, destinationPath) {
215
389
  console.log('Copying scss files...');
216
390
 
217
- for await (const path of glob('./**/!(*.private).scss', { cwd: sourcePath })) {
391
+ for await (const path of exploreDirectoryFiles(sourcePath, {
392
+ relativeTo: sourcePath,
393
+ pick: (path, { isFile }) => {
394
+ if (isFile) {
395
+ return isScssFile(path);
396
+ } else {
397
+ return true;
398
+ }
399
+ },
400
+ })) {
218
401
  // await cp(join(sourcePath, path), join(destinationPath, path));
219
402
  await writeFile(
220
403
  join(destinationPath, path),
@@ -271,6 +454,8 @@ function fixScssFilePath(path) {
271
454
  return path;
272
455
  }
273
456
 
457
+ /* OTHER */
458
+
274
459
  /**
275
460
  * Copies other files.
276
461
  *
@@ -290,26 +475,16 @@ async function copyOtherFiles(rootPath, destinationPath) {
290
475
  );
291
476
  }
292
477
 
293
- /**
294
- * Removes the index file.
295
- *
296
- * @param {string} indexFilePath
297
- * @return {Promise<void>}
298
- */
299
- async function removeTypescriptIndexFile(indexFilePath) {
300
- await rm(indexFilePath);
301
- }
302
-
303
478
  /**
304
479
  * Generates the package.json to publish.
305
480
  *
306
481
  * @param {string} destinationPath
307
- * @param {{ cwd?: string; mode?: 'dev' | 'rc' | 'prod', withProtected?: boolean }} options
482
+ * @param {{ rootPath?: string; mode?: 'dev' | 'rc' | 'prod', withProtected?: boolean }} options
308
483
  * @return {Promise<void>}
309
484
  */
310
485
  async function buildPackageJsonFile(
311
486
  destinationPath,
312
- { cwd = process.cwd(), mode = 'prod', withProtected = false } = {},
487
+ { rootPath = process.cwd(), mode = 'prod', withProtected = false } = {},
313
488
  ) {
314
489
  console.log('Building package.json...');
315
490
 
@@ -318,7 +493,7 @@ async function buildPackageJsonFile(
318
493
  /**
319
494
  * @type any
320
495
  */
321
- const pkg = JSON.parse(await readFile(join(cwd, fileName), { encoding: 'utf8' }));
496
+ const pkg = JSON.parse(await readFile(join(rootPath, fileName), { encoding: 'utf8' }));
322
497
 
323
498
  const indexTypesPath = './index.d.ts';
324
499
 
@@ -1,4 +1,5 @@
1
- import { join } from 'node:path';
1
+ import { join, resolve } from 'node:path';
2
+ import { ROOT_PATH } from '../constants/root-path.constant.js';
2
3
  import { cmd } from '../helpers/cmd.js';
3
4
 
4
5
  /**
@@ -7,7 +8,7 @@ import { cmd } from '../helpers/cmd.js';
7
8
  * @return {Promise<void>}
8
9
  */
9
10
  export async function publish({ mode = 'prod' } = {}) {
10
- const packagePath = './dist';
11
+ const packagePath = join(ROOT_PATH, 'dist');
11
12
 
12
13
  console.log('Publishing library...');
13
14
 
@@ -55,7 +56,7 @@ export async function publish({ mode = 'prod' } = {}) {
55
56
  args.push('--tag', options.tag);
56
57
  }
57
58
 
58
- await cmd('npm', args, { cwd: join(process.cwd(), packagePath) });
59
+ await cmd('npm', args, { cwd: resolve(packagePath) });
59
60
 
60
61
  console.log('Library published with success !');
61
62
  }
@@ -0,0 +1,4 @@
1
+ import { join } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+
4
+ export const ROOT_PATH = join(fileURLToPath(import.meta.url), '../../..');
@@ -1,4 +1,5 @@
1
1
  import { spawn } from 'node:child_process';
2
+ import process from 'node:process';
2
3
 
3
4
  /**
4
5
  * Runs "command" into the terminal.
@@ -0,0 +1,33 @@
1
+ import { opendir } from 'node:fs/promises';
2
+ import { join, relative, resolve } from 'node:path';
3
+ import process from 'node:process';
4
+
5
+ /**
6
+ * Runs "command" into the terminal.
7
+ * @param {string} path
8
+ * @param {{relativeTo?: string; pick?: (path: string, type: { isFile: boolean; isDirectory: boolean; }) => boolean; }?} options
9
+ * @return {AsyncGenerator<string>}
10
+ */
11
+ export async function* exploreDirectoryFiles(path, options) {
12
+ const dir = await opendir(resolve(process.cwd(), path));
13
+
14
+ for await (const dirent of dir) {
15
+ const path = join(dirent.parentPath, dirent.name);
16
+ const relativePath =
17
+ options?.relativeTo === undefined ? path : relative(options.relativeTo, path);
18
+
19
+ if (
20
+ options?.pick?.(relativePath, {
21
+ isFile: dirent.isFile(),
22
+ isDirectory: dirent.isDirectory(),
23
+ }) ??
24
+ true
25
+ ) {
26
+ if (dirent.isFile()) {
27
+ yield relativePath;
28
+ } else if (dirent.isDirectory()) {
29
+ yield* exploreDirectoryFiles(path, options);
30
+ }
31
+ }
32
+ }
33
+ }
@@ -1,6 +1,6 @@
1
1
  export namespace testTools {
2
2
  export function sleep(t: number): Promise<void> {
3
- return new Promise((_) => setTimeout(_, t));
3
+ return new Promise((resolve) => setTimeout(resolve, t));
4
4
  }
5
5
 
6
6
  export function polyfillRequestIdleCallback(): void {
@@ -15,4 +15,12 @@ export namespace testTools {
15
15
  clearTimeout(handle);
16
16
  };
17
17
  }
18
+
19
+ export function gc(): void {
20
+ if (typeof (globalThis as any).gc === 'function') {
21
+ return (globalThis as any).gc!();
22
+ } else {
23
+ throw new Error('Missing `gc` function. Did you `--expose-gc` ?');
24
+ }
25
+ }
18
26
  }
@@ -8,6 +8,8 @@
8
8
  "fb:format": "prettier --write --ignore-unknown --config ./.prettierrc \"src/**/*.{js,jsx,ts,tsx,html,css,scss}\"",
9
9
  "fb:test": "vitest",
10
10
  "fb:test:coverage": "vitest run --coverage",
11
+ "fb:bench": "vitest bench",
12
+ "fb:typedoc": "typedoc --options typedoc.json --tsconfig tsconfig.json",
11
13
  "fb:prod": "node fabrique/actions/cli/build-and-publish.cli.js --mode prod",
12
14
  "fb:dev": "node fabrique/actions/cli/build-and-publish.cli.js --mode dev",
13
15
  "fb:rc": "node fabrique/actions/cli/build-and-publish.cli.js --mode rc"
@@ -22,10 +24,13 @@
22
24
  "url": "{{giturl}}"
23
25
  },
24
26
  "devDependencies": {
25
- "prettier": "^3.4.2",
27
+ "@types/node": "^22.13.11",
28
+ "@vitest/coverage-istanbul": "^3.0.9",
29
+ "prettier": "^3.5.3",
26
30
  "prettier-plugin-organize-imports": "^4.1.0",
27
- "typescript": "^5.7.2",
28
- "vitest": "^2.1.8"
31
+ "typedoc": "^0.28.1",
32
+ "typescript": "5.8.2",
33
+ "vitest": "^3.0.9"
29
34
  },
30
- "packageManager": "yarn@4.6.0"
35
+ "packageManager": "yarn@4.7.0"
31
36
  }
@@ -3,15 +3,10 @@
3
3
  "compilerOptions": {
4
4
  "declaration": true,
5
5
  "declarationDir": "./dist",
6
- "downlevelIteration": false,
7
- "emitDecoratorMetadata": false,
8
- "importHelpers": false,
9
- "noEmitHelpers": false,
10
- "noLib": false,
11
- "outDir": "dist",
12
- "preserveConstEnums": false,
13
- "pretty": true,
14
- "removeComments": false,
15
- "sourceMap": true
16
- }
6
+ "outDir": "dist"
7
+ },
8
+ "include": [
9
+ "src/index.ts",
10
+ "src/index.protected.ts"
11
+ ]
17
12
  }