fabrique 0.2.5 → 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,7 @@
1
1
  import { cp, readFile, rm, writeFile } from 'node:fs/promises';
2
2
  import { isAbsolute, join } from 'node:path';
3
3
  import process from 'node:process';
4
+ import { ROOT_PATH } from '../constants/root-path.constant.js';
4
5
  import { cmd } from '../helpers/cmd.js';
5
6
  import { exploreDirectoryFiles } from '../helpers/explore-directory.js';
6
7
 
@@ -10,9 +11,8 @@ import { exploreDirectoryFiles } from '../helpers/explore-directory.js';
10
11
  * @return {Promise<void>}
11
12
  */
12
13
  export async function build({ mode = 'prod' } = {}) {
13
- const rootPath = '.';
14
- const sourcePath = './src';
15
- const destinationPath = './dist';
14
+ const sourcePath = join(ROOT_PATH, 'src');
15
+ const destinationPath = join(ROOT_PATH, 'dist');
16
16
 
17
17
  await removeDestination(destinationPath);
18
18
 
@@ -20,7 +20,7 @@ export async function build({ mode = 'prod' } = {}) {
20
20
  const [withProtected] = await Promise.all([
21
21
  buildTypescript(sourcePath),
22
22
  buildScss(sourcePath, destinationPath),
23
- copyOtherFiles(rootPath, destinationPath),
23
+ copyOtherFiles(ROOT_PATH, destinationPath),
24
24
  ]);
25
25
 
26
26
  await buildPackageJsonFile(destinationPath, {
@@ -45,6 +45,49 @@ async function removeDestination(destinationPath) {
45
45
  await rm(destinationPath, { recursive: true, force: true });
46
46
  }
47
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
+
48
91
  /**
49
92
  * Builds the typescript part.
50
93
  *
@@ -68,46 +111,86 @@ async function buildTypescript(sourcePath) {
68
111
  return typescriptProtectedIndexFilePath !== null;
69
112
  }
70
113
 
114
+ function generateExportEsmLine(path) {
115
+ return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';\n`;
116
+ }
117
+
71
118
  /**
72
- * Builds the scss part.
119
+ * Returns true if `path` is a `typescript` file.
73
120
  *
74
- * @param {string} sourcePath
75
- * @param {string} destinationPath
76
- * @return {Promise<void>}
121
+ * @param {string} path
122
+ * @return boolean
77
123
  */
78
- async function buildScss(sourcePath, destinationPath) {
79
- await copyScssFiles(sourcePath, destinationPath);
80
- await buildScssIndexFile(destinationPath);
124
+ function isTsFile(path) {
125
+ return path.endsWith('.ts');
81
126
  }
82
127
 
83
- function generateExportEsmLine(path) {
84
- return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';\n`;
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');
85
166
  }
86
167
 
87
168
  /**
88
169
  * Builds the typescript index file used to export all public APIs.
89
170
  *
90
- * @param {string} cwd
171
+ * @param {string} sourcePath
91
172
  * @return {Promise<string>}
92
173
  */
93
- async function buildTypescriptIndexFile(cwd = process.cwd()) {
174
+ async function buildTypescriptIndexFile(sourcePath) {
94
175
  console.log('Building typescript index file...');
95
176
 
96
177
  let content = '';
97
178
 
98
- for await (const path of exploreDirectoryFiles(cwd, {
99
- relativeTo: cwd,
179
+ for await (const path of exploreDirectoryFiles(sourcePath, {
180
+ relativeTo: sourcePath,
100
181
  pick: (path, { isFile }) => {
101
182
  if (isFile) {
102
183
  return (
103
- path.endsWith('.ts') &&
104
- !path.endsWith('.spec.ts') &&
105
- !path.endsWith('.test.ts') &&
106
- !path.endsWith('.private.ts') &&
107
- !path.endsWith('.protected.ts')
184
+ isTsFile(path) &&
185
+ !(
186
+ isTsTestOrSpecFile(path) ||
187
+ isTsBenchFile(path) ||
188
+ isTsProtectedFile(path) ||
189
+ isTsPrivateFile(path)
190
+ )
108
191
  );
109
192
  } else {
110
- return !path.endsWith('.private') && !path.endsWith('.protected');
193
+ return !(isProtectedDirectory(path) || isPrivateDirectory(path));
111
194
  }
112
195
  },
113
196
  })) {
@@ -118,7 +201,7 @@ async function buildTypescriptIndexFile(cwd = process.cwd()) {
118
201
  throw new Error('Nothing exported.');
119
202
  }
120
203
 
121
- const indexFilePath = join(cwd, 'index.ts');
204
+ const indexFilePath = join(sourcePath, 'index.ts');
122
205
  await writeFile(indexFilePath, content + '\n');
123
206
 
124
207
  return indexFilePath;
@@ -127,23 +210,26 @@ async function buildTypescriptIndexFile(cwd = process.cwd()) {
127
210
  /**
128
211
  * Builds the typescript index file used to export all protected APIs.
129
212
  *
130
- * @param {string} cwd
213
+ * @param {string} sourcePath
131
214
  * @return {Promise<string | null>}
132
215
  */
133
- async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
216
+ async function buildTypescriptProtectedIndexFile(sourcePath) {
134
217
  console.log('Building typescript protected index file...');
135
218
 
136
219
  let content = '';
137
220
 
138
- for await (const path of exploreDirectoryFiles(cwd, {
139
- relativeTo: cwd,
221
+ for await (const path of exploreDirectoryFiles(sourcePath, {
222
+ relativeTo: sourcePath,
140
223
  pick: (path, { isFile }) => {
141
224
  if (isFile) {
142
225
  return (
143
- path.endsWith('.protected.ts') || (path.endsWith('.ts') && path.includes('.protected/'))
226
+ isTsProtectedFile(path) ||
227
+ (isInProtectedDirectory(path) &&
228
+ isTsFile(path) &&
229
+ !(isTsTestOrSpecFile(path) || isTsBenchFile(path) || isTsPrivateFile(path)))
144
230
  );
145
231
  } else {
146
- return !path.endsWith('.private');
232
+ return !isPrivateDirectory(path);
147
233
  }
148
234
  },
149
235
  })) {
@@ -154,7 +240,7 @@ async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
154
240
  return null;
155
241
  }
156
242
 
157
- const indexFilePath = join(cwd, 'index.protected.ts');
243
+ const indexFilePath = join(sourcePath, 'index.protected.ts');
158
244
  await writeFile(indexFilePath, content + '\n');
159
245
 
160
246
  return indexFilePath;
@@ -163,13 +249,13 @@ async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
163
249
  /**
164
250
  * Compiles the typescript files.
165
251
  *
166
- * @param {string | undefined } cwd
252
+ * @param {string | undefined } rootPath
167
253
  * @return {Promise<void>}
168
254
  */
169
- async function compileTypescript(cwd = process.cwd()) {
255
+ async function compileTypescript(rootPath = process.cwd()) {
170
256
  console.log('Compiling typescript...');
171
257
 
172
- await cmd('tsc', ['-p', './tsconfig.build.json'], { cwd });
258
+ await cmd('tsc', ['-p', './tsconfig.build.json'], { cwd: rootPath });
173
259
  }
174
260
 
175
261
  /**
@@ -182,46 +268,102 @@ async function compileTypescript(cwd = process.cwd()) {
182
268
  async function copyTypescriptFiles(sourcePath, destinationPath) {
183
269
  console.log('Copying typescript files...');
184
270
 
185
- for await (const path of exploreDirectoryFiles(sourcePath, {
186
- relativeTo: sourcePath,
187
- pick: (path, { isFile }) => {
188
- if (isFile) {
189
- return path.endsWith('.ts') && !path.endsWith('.spec.ts') && !path.endsWith('.test.ts');
190
- } else {
191
- return true;
192
- }
193
- },
194
- })) {
195
- await cp(join(sourcePath, path), join(destinationPath, path));
196
- }
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
+ // }
289
+ }
290
+
291
+ /**
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.
305
+ *
306
+ * @param {string} sourcePath
307
+ * @param {string} destinationPath
308
+ * @return {Promise<void>}
309
+ */
310
+ async function buildScss(sourcePath, destinationPath) {
311
+ await copyScssFiles(sourcePath, destinationPath);
312
+ await buildScssIndexFile(destinationPath);
197
313
  }
198
314
 
199
315
  function generateExportScssLine(path) {
200
316
  return `@forward './${path.slice(0, -5)}';\n`;
201
317
  }
202
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
+
203
349
  /**
204
350
  * Builds the scss index file used to export all public styles.
205
351
  *
206
- * @param {string} cwd
352
+ * @param {string} sourcePath
207
353
  * @return {Promise<void>}
208
354
  */
209
- async function buildScssIndexFile(cwd = process.cwd()) {
355
+ async function buildScssIndexFile(sourcePath) {
210
356
  console.log('Building scss index file...');
211
357
 
212
358
  let content = '';
213
359
 
214
- for await (const path of exploreDirectoryFiles(cwd, {
215
- relativeTo: cwd,
360
+ for await (const path of exploreDirectoryFiles(sourcePath, {
361
+ relativeTo: sourcePath,
216
362
  pick: (path, { isFile }) => {
217
363
  if (isFile) {
218
- return (
219
- path.endsWith('.scss') &&
220
- !path.endsWith('.protected.scss') &&
221
- !path.endsWith('.private.scss')
222
- );
364
+ return isScssFile(path) && !(isScssProtectedFile(path) && isScssPrivateFile(path));
223
365
  } else {
224
- return !path.endsWith('.private') && !path.endsWith('.protected');
366
+ return !(isProtectedDirectory(path) || isPrivateDirectory(path));
225
367
  }
226
368
  },
227
369
  })) {
@@ -231,7 +373,7 @@ async function buildScssIndexFile(cwd = process.cwd()) {
231
373
  if (content === '') {
232
374
  console.log('=> No scss file to export.');
233
375
  } else {
234
- const indexFilePath = join(cwd, 'index.scss');
376
+ const indexFilePath = join(sourcePath, 'index.scss');
235
377
  await writeFile(indexFilePath, content + '\n');
236
378
  }
237
379
  }
@@ -250,7 +392,7 @@ async function copyScssFiles(sourcePath, destinationPath) {
250
392
  relativeTo: sourcePath,
251
393
  pick: (path, { isFile }) => {
252
394
  if (isFile) {
253
- return path.endsWith('.scss');
395
+ return isScssFile(path);
254
396
  } else {
255
397
  return true;
256
398
  }
@@ -312,6 +454,8 @@ function fixScssFilePath(path) {
312
454
  return path;
313
455
  }
314
456
 
457
+ /* OTHER */
458
+
315
459
  /**
316
460
  * Copies other files.
317
461
  *
@@ -331,26 +475,16 @@ async function copyOtherFiles(rootPath, destinationPath) {
331
475
  );
332
476
  }
333
477
 
334
- /**
335
- * Removes the index file.
336
- *
337
- * @param {string} indexFilePath
338
- * @return {Promise<void>}
339
- */
340
- async function removeTypescriptIndexFile(indexFilePath) {
341
- await rm(indexFilePath);
342
- }
343
-
344
478
  /**
345
479
  * Generates the package.json to publish.
346
480
  *
347
481
  * @param {string} destinationPath
348
- * @param {{ cwd?: string; mode?: 'dev' | 'rc' | 'prod', withProtected?: boolean }} options
482
+ * @param {{ rootPath?: string; mode?: 'dev' | 'rc' | 'prod', withProtected?: boolean }} options
349
483
  * @return {Promise<void>}
350
484
  */
351
485
  async function buildPackageJsonFile(
352
486
  destinationPath,
353
- { cwd = process.cwd(), mode = 'prod', withProtected = false } = {},
487
+ { rootPath = process.cwd(), mode = 'prod', withProtected = false } = {},
354
488
  ) {
355
489
  console.log('Building package.json...');
356
490
 
@@ -359,7 +493,7 @@ async function buildPackageJsonFile(
359
493
  /**
360
494
  * @type any
361
495
  */
362
- const pkg = JSON.parse(await readFile(join(cwd, fileName), { encoding: 'utf8' }));
496
+ const pkg = JSON.parse(await readFile(join(rootPath, fileName), { encoding: 'utf8' }));
363
497
 
364
498
  const indexTypesPath = './index.d.ts';
365
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.
@@ -6,7 +6,7 @@ import process from 'node:process';
6
6
  * Runs "command" into the terminal.
7
7
  * @param {string} path
8
8
  * @param {{relativeTo?: string; pick?: (path: string, type: { isFile: boolean; isDirectory: boolean; }) => boolean; }?} options
9
- * @return {Generator<string>}
9
+ * @return {AsyncGenerator<string>}
10
10
  */
11
11
  export async function* exploreDirectoryFiles(path, options) {
12
12
  const dir = await opendir(resolve(process.cwd(), path));
@@ -17,7 +17,10 @@ export async function* exploreDirectoryFiles(path, options) {
17
17
  options?.relativeTo === undefined ? path : relative(options.relativeTo, path);
18
18
 
19
19
  if (
20
- options?.pick(relativePath, { isFile: dirent.isFile(), isDirectory: dirent.isDirectory() }) ??
20
+ options?.pick?.(relativePath, {
21
+ isFile: dirent.isFile(),
22
+ isDirectory: dirent.isDirectory(),
23
+ }) ??
21
24
  true
22
25
  ) {
23
26
  if (dirent.isFile()) {
@@ -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
  }