fabrique 0.2.4 → 0.2.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fabrique",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "bin": "./src/index.js",
@@ -1,6 +1,8 @@
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';
3
4
  import { cmd } from '../helpers/cmd.js';
5
+ import { exploreDirectoryFiles } from '../helpers/explore-directory.js';
4
6
 
5
7
  /**
6
8
  * Builds the lib.
@@ -78,6 +80,10 @@ async function buildScss(sourcePath, destinationPath) {
78
80
  await buildScssIndexFile(destinationPath);
79
81
  }
80
82
 
83
+ function generateExportEsmLine(path) {
84
+ return `export * from './${path.replaceAll('\\', '/').slice(0, -3)}.js';\n`;
85
+ }
86
+
81
87
  /**
82
88
  * Builds the typescript index file used to export all public APIs.
83
89
  *
@@ -87,25 +93,26 @@ async function buildScss(sourcePath, destinationPath) {
87
93
  async function buildTypescriptIndexFile(cwd = process.cwd()) {
88
94
  console.log('Building typescript index file...');
89
95
 
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');
96
+ let content = '';
97
+
98
+ for await (const path of exploreDirectoryFiles(cwd, {
99
+ relativeTo: cwd,
100
+ pick: (path, { isFile }) => {
101
+ if (isFile) {
102
+ 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')
108
+ );
109
+ } else {
110
+ return !path.endsWith('.private') && !path.endsWith('.protected');
111
+ }
112
+ },
113
+ })) {
114
+ content += generateExportEsmLine(path);
115
+ }
109
116
 
110
117
  if (content === '') {
111
118
  throw new Error('Nothing exported.');
@@ -126,22 +133,22 @@ async function buildTypescriptIndexFile(cwd = process.cwd()) {
126
133
  async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
127
134
  console.log('Building typescript protected index file...');
128
135
 
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');
136
+ let content = '';
137
+
138
+ for await (const path of exploreDirectoryFiles(cwd, {
139
+ relativeTo: cwd,
140
+ pick: (path, { isFile }) => {
141
+ if (isFile) {
142
+ return (
143
+ path.endsWith('.protected.ts') || (path.endsWith('.ts') && path.includes('.protected/'))
144
+ );
145
+ } else {
146
+ return !path.endsWith('.private');
147
+ }
148
+ },
149
+ })) {
150
+ content += generateExportEsmLine(path);
151
+ }
145
152
 
146
153
  if (content === '') {
147
154
  return null;
@@ -175,13 +182,26 @@ async function compileTypescript(cwd = process.cwd()) {
175
182
  async function copyTypescriptFiles(sourcePath, destinationPath) {
176
183
  console.log('Copying typescript files...');
177
184
 
178
- for await (const path of glob('./**/!(*.spec|*.test).ts', { cwd: sourcePath })) {
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
+ })) {
179
195
  await cp(join(sourcePath, path), join(destinationPath, path));
180
196
  }
181
197
  }
182
198
 
199
+ function generateExportScssLine(path) {
200
+ return `@forward './${path.slice(0, -5)}';\n`;
201
+ }
202
+
183
203
  /**
184
- * Builds the typescript index file used to export all public APIs.
204
+ * Builds the scss index file used to export all public styles.
185
205
  *
186
206
  * @param {string} cwd
187
207
  * @return {Promise<void>}
@@ -189,12 +209,24 @@ async function copyTypescriptFiles(sourcePath, destinationPath) {
189
209
  async function buildScssIndexFile(cwd = process.cwd()) {
190
210
  console.log('Building scss index file...');
191
211
 
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');
212
+ let content = '';
213
+
214
+ for await (const path of exploreDirectoryFiles(cwd, {
215
+ relativeTo: cwd,
216
+ pick: (path, { isFile }) => {
217
+ if (isFile) {
218
+ return (
219
+ path.endsWith('.scss') &&
220
+ !path.endsWith('.protected.scss') &&
221
+ !path.endsWith('.private.scss')
222
+ );
223
+ } else {
224
+ return !path.endsWith('.private') && !path.endsWith('.protected');
225
+ }
226
+ },
227
+ })) {
228
+ content += generateExportScssLine(path);
229
+ }
198
230
 
199
231
  if (content === '') {
200
232
  console.log('=> No scss file to export.');
@@ -214,7 +246,16 @@ async function buildScssIndexFile(cwd = process.cwd()) {
214
246
  async function copyScssFiles(sourcePath, destinationPath) {
215
247
  console.log('Copying scss files...');
216
248
 
217
- for await (const path of glob('./**/!(*.private).scss', { cwd: sourcePath })) {
249
+ for await (const path of exploreDirectoryFiles(sourcePath, {
250
+ relativeTo: sourcePath,
251
+ pick: (path, { isFile }) => {
252
+ if (isFile) {
253
+ return path.endsWith('.scss');
254
+ } else {
255
+ return true;
256
+ }
257
+ },
258
+ })) {
218
259
  // await cp(join(sourcePath, path), join(destinationPath, path));
219
260
  await writeFile(
220
261
  join(destinationPath, path),
@@ -0,0 +1,30 @@
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 {Generator<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, { isFile: dirent.isFile(), isDirectory: dirent.isDirectory() }) ??
21
+ true
22
+ ) {
23
+ if (dirent.isFile()) {
24
+ yield relativePath;
25
+ } else if (dirent.isDirectory()) {
26
+ yield* exploreDirectoryFiles(path, options);
27
+ }
28
+ }
29
+ }
30
+ }