From 6a9fba8a1c75feaaad529e3002afc6c4da4e1285 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Tue, 25 Jul 2023 08:36:24 +1200 Subject: [PATCH 1/3] Add workaround for globs in some cases, for thecodrr/fdir#92 --- src/generators/collections.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/generators/collections.js b/src/generators/collections.js index 3e2a5749..6698c738 100644 --- a/src/generators/collections.js +++ b/src/generators/collections.js @@ -6,10 +6,20 @@ import log from '../util/logger.js'; import { parseFile } from '../parsers/parser.js'; async function getCollectionFilePaths(collectionConfig, source) { - const crawler = new fdir() + let crawler = new fdir() .withBasePath() .filter((filePath, isDirectory) => !isDirectory && !filePath.includes('/_defaults.')); + let crawlDirectory = join(source, collectionConfig.path); + + // Work around for https://github.com/thecodrr/fdir/issues/92 + // Globbing on `.` doesn't work, so we crawl using the absolute CWD + // and get the relative paths of results instead of the base paths. + if (crawlDirectory === '.' || crawlDirectory === './') { + crawler = crawler.withRelativePaths(); + crawlDirectory = process.cwd(); + } + const glob = typeof collectionConfig.glob === 'string' ? [collectionConfig.glob] : collectionConfig.glob; @@ -19,7 +29,7 @@ async function getCollectionFilePaths(collectionConfig, source) { } return crawler - .crawl(join(source, collectionConfig.path)) + .crawl(crawlDirectory) .withPromise(); } From 64d33f62296064e21dc06d035787310b1a67c8b5 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Tue, 25 Jul 2023 08:45:43 +1200 Subject: [PATCH 2/3] Add test for globbing from the root directory --- tests/generators/expected/root-glob.json | 45 ++++++++++++++++++++++++ tests/generators/fixtures/root-glob.json | 23 ++++++++++++ tests/generators/info.test.js | 1 + 3 files changed, 69 insertions(+) create mode 100644 tests/generators/expected/root-glob.json create mode 100644 tests/generators/fixtures/root-glob.json diff --git a/tests/generators/expected/root-glob.json b/tests/generators/expected/root-glob.json new file mode 100644 index 00000000..1ef83ce4 --- /dev/null +++ b/tests/generators/expected/root-glob.json @@ -0,0 +1,45 @@ +{ + "source": ".", + "collections_config": { + "everything": { + "path": ".", + "glob": [ + "tests/generators/fixtures/standard/**/nested/*" + ], + "output": true, + "url": "hello/{title|slugify}" + } + }, + "_comments": {}, + "_options": {}, + "_structures": {}, + "_select_data": {}, + "generator": {}, + "source_editor": {}, + "paths": { + "uploads": "assets/uploads" + }, + "base_url": "", + "time": "2000-11-22T00:00:00.000Z", + "cloudcannon": { + "name": "cloudcannon-reader", + "version": "0.0.1" + }, + "version": "0.0.3", + "data": {}, + "collections": { + "everything": [ + { + "date": "2020-07-22T00:00:00.000Z", + "title": "Egg", + "categories": [ + "tips" + ], + "author_staff_member": "betty", + "path": "tests/generators/fixtures/standard/_posts/nested/2020-07-22-egg.md", + "collection": "everything", + "url": "hello/egg" + } + ] + } +} \ No newline at end of file diff --git a/tests/generators/fixtures/root-glob.json b/tests/generators/fixtures/root-glob.json new file mode 100644 index 00000000..eb915186 --- /dev/null +++ b/tests/generators/fixtures/root-glob.json @@ -0,0 +1,23 @@ +{ + "source": ".", + "collections_config": { + "everything": { + "path": ".", + "glob": [ + "tests/generators/fixtures/standard/**/nested/*" + ], + "output": true, + "url": "hello/{title|slugify}" + } + }, + "_comments": {}, + "_options": {}, + "_structures": {}, + "_select_data": {}, + "generator": {}, + "source_editor": {}, + "paths": { + "uploads": "assets/uploads" + }, + "base_url": "" +} \ No newline at end of file diff --git a/tests/generators/info.test.js b/tests/generators/info.test.js index c27b1e07..9048b66c 100644 --- a/tests/generators/info.test.js +++ b/tests/generators/info.test.js @@ -30,5 +30,6 @@ async function runTest(t, key) { test('Generate JSON info', async (t) => runTest(t, 'standard')); test('Generate JSON info with custom source', async (t) => runTest(t, 'custom-source')); test('Generate JSON info with globs', async (t) => runTest(t, 'globs')); +test('Generate JSON info with a root glob', async (t) => runTest(t, 'root-glob')); test('Generate JSON info with null fields', async (t) => runTest(t, 'null-fields')); test('Generate JSON info with nested collections', async (t) => runTest(t, 'nested-collections')); From f667b6072729b11a1f8e14cfa1903dac1b789ce3 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Tue, 25 Jul 2023 08:50:57 +1200 Subject: [PATCH 3/3] Scope the workaround tighter to the corner-case --- src/generators/collections.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/collections.js b/src/generators/collections.js index 6698c738..07fd7016 100644 --- a/src/generators/collections.js +++ b/src/generators/collections.js @@ -15,7 +15,7 @@ async function getCollectionFilePaths(collectionConfig, source) { // Work around for https://github.com/thecodrr/fdir/issues/92 // Globbing on `.` doesn't work, so we crawl using the absolute CWD // and get the relative paths of results instead of the base paths. - if (crawlDirectory === '.' || crawlDirectory === './') { + if ((crawlDirectory === '.' || crawlDirectory === './') && collectionConfig.glob) { crawler = crawler.withRelativePaths(); crawlDirectory = process.cwd(); }