@content-collections/core 0.1.1 → 0.2.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.
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ type CollectionRequest<TName extends string, TShape extends ZodRawShape, TSchema
25
25
  typeName?: string;
26
26
  schema: (z: Z) => TShape;
27
27
  transform?: (context: Context, data: TSchema) => TTransformResult;
28
- directory: string;
28
+ directory: string | string[];
29
29
  include: string | string[];
30
30
  onSuccess?: (documents: Array<TDocument>) => void | Promise<void>;
31
31
  };
package/dist/index.js CHANGED
@@ -11,6 +11,9 @@ function generateTypeName(name) {
11
11
  function isDefined(value) {
12
12
  return value !== void 0 && value !== null;
13
13
  }
14
+ function orderByPath(a, b) {
15
+ return a.path.localeCompare(b.path);
16
+ }
14
17
 
15
18
  // src/config.ts
16
19
  function defineCollection(collection) {
@@ -36,7 +39,7 @@ import path from "path";
36
39
  // package.json
37
40
  var package_default = {
38
41
  name: "@content-collections/core",
39
- version: "0.1.0",
42
+ version: "0.1.2",
40
43
  type: "module",
41
44
  main: "dist/index.cjs",
42
45
  types: "./dist/index.d.ts",
@@ -199,8 +202,8 @@ function createCollector(emitter, baseDirectory = ".") {
199
202
  return null;
200
203
  }
201
204
  }
202
- async function resolveCollection(collection) {
203
- const collectionDirectory = path2.join(baseDirectory, collection.directory);
205
+ async function resolveDirectory(collection, directory) {
206
+ const collectionDirectory = path2.join(baseDirectory, directory);
204
207
  const filePaths = await fg(collection.include, {
205
208
  cwd: collectionDirectory,
206
209
  onlyFiles: true,
@@ -209,10 +212,22 @@ function createCollector(emitter, baseDirectory = ".") {
209
212
  const promises = filePaths.map(
210
213
  (filePath) => collectFile(collectionDirectory, filePath)
211
214
  );
212
- const files = await Promise.all(promises);
215
+ return Promise.all(promises);
216
+ }
217
+ async function resolveCollection(collection) {
218
+ let files = [];
219
+ if (typeof collection.directory === "string") {
220
+ files = await resolveDirectory(collection, collection.directory);
221
+ } else {
222
+ const promises = collection.directory.map(
223
+ (directory) => resolveDirectory(collection, directory)
224
+ );
225
+ const resolved = await Promise.all(promises);
226
+ files = resolved.flat();
227
+ }
213
228
  return {
214
229
  ...collection,
215
- files: files.filter(isDefined)
230
+ files: files.filter(isDefined).sort(orderByPath)
216
231
  };
217
232
  }
218
233
  async function collect(unresolvedCollections) {
@@ -423,13 +438,26 @@ function createTransformer(emitter) {
423
438
  import micromatch from "micromatch";
424
439
  import path4 from "path";
425
440
  function createSynchronizer(readCollectionFile, collections, baseDirectory = ".") {
426
- function findCollection(filePath) {
441
+ function findCollectionAndDirectory(filePath) {
427
442
  const resolvedFilePath = path4.resolve(filePath);
428
- return collections.find((collection) => {
429
- return resolvedFilePath.startsWith(
430
- path4.resolve(baseDirectory, collection.directory)
431
- );
432
- });
443
+ for (const collection of collections) {
444
+ const directories = [];
445
+ if (typeof collection.directory === "string") {
446
+ directories.push(collection.directory);
447
+ } else {
448
+ directories.push(...collection.directory);
449
+ }
450
+ for (const directory of directories) {
451
+ const resolvedDirectory = path4.resolve(baseDirectory, directory);
452
+ if (resolvedFilePath.startsWith(resolvedDirectory)) {
453
+ return {
454
+ collection,
455
+ directory
456
+ };
457
+ }
458
+ }
459
+ }
460
+ return null;
433
461
  }
434
462
  function createRelativePath(collectionPath, filePath) {
435
463
  const resolvedCollectionPath = path4.resolve(baseDirectory, collectionPath);
@@ -441,11 +469,12 @@ function createSynchronizer(readCollectionFile, collections, baseDirectory = "."
441
469
  return relativePath;
442
470
  }
443
471
  function resolve(filePath) {
444
- const collection = findCollection(filePath);
445
- if (!collection) {
472
+ const collectionAndDirectory = findCollectionAndDirectory(filePath);
473
+ if (!collectionAndDirectory) {
446
474
  return null;
447
475
  }
448
- const relativePath = createRelativePath(collection.directory, filePath);
476
+ const { collection, directory } = collectionAndDirectory;
477
+ const relativePath = createRelativePath(directory, filePath);
449
478
  if (!micromatch.isMatch(relativePath, collection.include)) {
450
479
  return null;
451
480
  }
@@ -475,15 +504,28 @@ function createSynchronizer(readCollectionFile, collections, baseDirectory = "."
475
504
  const index = collection.files.findIndex(
476
505
  (file2) => file2.path === relativePath
477
506
  );
478
- const file = await readCollectionFile(
479
- path4.join(baseDirectory, collection.directory),
480
- relativePath
481
- );
507
+ const directories = [];
508
+ if (typeof collection.directory === "string") {
509
+ directories.push(collection.directory);
510
+ } else {
511
+ directories.push(...collection.directory);
512
+ }
513
+ let file = null;
514
+ for (const directory of directories) {
515
+ file = await readCollectionFile(
516
+ path4.join(baseDirectory, directory),
517
+ relativePath
518
+ );
519
+ if (file) {
520
+ break;
521
+ }
522
+ }
482
523
  if (!file) {
483
524
  return false;
484
525
  }
485
526
  if (index === -1) {
486
527
  collection.files.push(file);
528
+ collection.files.sort(orderByPath);
487
529
  } else {
488
530
  collection.files[index] = file;
489
531
  }
@@ -499,7 +541,7 @@ function createSynchronizer(readCollectionFile, collections, baseDirectory = "."
499
541
  import path5 from "path";
500
542
 
501
543
  // src/watcher.ts
502
- import watcher from "@parcel/watcher";
544
+ import * as watcher from "@parcel/watcher";
503
545
  async function createWatcher(emitter, paths, sync, build2) {
504
546
  const onChange = async (error, events) => {
505
547
  if (error) {
@@ -613,9 +655,16 @@ async function createBuilder(configurationPath, options = {
613
655
  });
614
656
  }
615
657
  async function watch() {
616
- const paths = resolved.map(
617
- (collection) => path5.join(baseDirectory, collection.directory)
618
- );
658
+ const paths = [];
659
+ for (const collection of resolved) {
660
+ if (typeof collection.directory === "string") {
661
+ paths.push(path5.join(baseDirectory, collection.directory));
662
+ } else {
663
+ paths.push(
664
+ ...collection.directory.map((dir) => path5.join(baseDirectory, dir))
665
+ );
666
+ }
667
+ }
619
668
  const watcher2 = await createWatcher(emitter, paths, sync, build2);
620
669
  return watcher2;
621
670
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@content-collections/core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "types": "./dist/index.d.ts",