Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an edge case when globbing the working dir #3

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));