Skip to content

Commit

Permalink
Merge pull request #3 from CloudCannon/fix/glob-working-dir
Browse files Browse the repository at this point in the history
Fix an edge case when globbing the working dir
  • Loading branch information
rphillips-cc authored Jul 24, 2023
2 parents 4d43a19 + f667b60 commit 54b9566
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/generators/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === './') && collectionConfig.glob) {
crawler = crawler.withRelativePaths();
crawlDirectory = process.cwd();
}

const glob = typeof collectionConfig.glob === 'string'
? [collectionConfig.glob]
: collectionConfig.glob;
Expand All @@ -19,7 +29,7 @@ async function getCollectionFilePaths(collectionConfig, source) {
}

return crawler
.crawl(join(source, collectionConfig.path))
.crawl(crawlDirectory)
.withPromise();
}

Expand Down
45 changes: 45 additions & 0 deletions tests/generators/expected/root-glob.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
23 changes: 23 additions & 0 deletions tests/generators/fixtures/root-glob.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
1 change: 1 addition & 0 deletions tests/generators/info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

0 comments on commit 54b9566

Please sign in to comment.