Skip to content

Commit

Permalink
Move parsers into gatsby directory
Browse files Browse the repository at this point in the history
Combine remaining parsers into a single one,
move into gatsby directory for ease of importing.
  • Loading branch information
Chalarangelo committed Aug 26, 2020
1 parent 15f80cd commit 0fdceeb
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 94 deletions.
2 changes: 1 addition & 1 deletion content/sources/30blog
16 changes: 7 additions & 9 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ const env = require('./.build/env').default;
const {
createPages,
onCreateWebpackConfig,
parseRequirements,
} = require(`./src/gatsby`);
const {
parseRequirables,
parseTemplates,
} = require(`./src/build/parsers`);
const paths = require(`./src/config/paths`);

const requirables = parseRequirables(paths.contentPath);
console.log(`${green('success')} parse requirables`);

const templates = parseTemplates(env === 'DEVELOPMENT' ? paths.devTemplates : paths.templates, paths.templatesPath);
console.log(`${green('success')} parse templates`);
const { requirables, templates} = parseRequirements(
env === 'DEVELOPMENT' ? paths.devTemplates : paths.templates,
paths.templatesPath,
paths.contentPath
);
console.log(`${green('success')} parse requirements`);

exports.createPages = createPages(templates, requirables);

Expand Down
7 changes: 0 additions & 7 deletions src/build/parsers/index.js

This file was deleted.

35 changes: 0 additions & 35 deletions src/build/parsers/parseRequirables.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/build/parsers/parseTemplates.js

This file was deleted.

26 changes: 0 additions & 26 deletions src/build/parsers/parseTemplates.test.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/gatsby/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import parseRequirements from './parseRequirements';
import createPages from './createPages';
import onCreateWebpackConfig from './onCreateWebpackConfig';

export {
parseRequirements,
createPages,
onCreateWebpackConfig,
};
41 changes: 41 additions & 0 deletions src/gatsby/parseRequirements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import glob from 'glob';
import path from 'path';

/**
* Combines the given data JSONs, using the data files
* of the content, returning an array of objects from the files.
* @param {string} contentDir - The path to the content directory.
*/
export const parseRequirables = contentDir =>
glob.sync(`${contentDir}/**/index.json`).map(file =>
glob.sync(`${file.slice(0, file.lastIndexOf('/'))}/!(index).json`)
.reduce((acc, dataFile) => {
acc.context = { ...acc.context, ...require(path.resolve(dataFile)) };
return acc;
}, { ...require(path.resolve(file)), context: {} }));

/**
* Combines the given list of templates into an object.
* @param {array} templates - An array of templates.
* @param {string} templatesDir - The path to the template directory.
*/
export const parseTemplates = (templates, templatesDir) =>
templates.reduce((acc, tmpl) => {
acc[tmpl.name] = path.resolve(`${templatesDir}/${tmpl.path}`);
return acc;
}, {});

/* istanbul ignore next */
/**
* Returns an object containing templates and requirables created by
* combining the given template and content parameters.
* @param {array} templates - An array of templates.
* @param {string} templatesDir - The path to the template directory.
* @param {string} contentDir - The path to the content directory.
*/
const parseRequirements = (templates, templatesDir, contentDir) => ({
templates: parseTemplates(templates, templatesDir),
requirables: parseRequirables(contentDir),
});

export default parseRequirements;
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import parseRequirables from './parseRequirables';
import { parseRequirables, parseTemplates } from './parseRequirements';

import glob from 'glob';
import path from 'path';

jest.mock('glob');
jest.mock('path');


const templates = [
{
'name': 'SnippetPage',
'path': 'snippetPage/index.jsx',
},
{
'name': 'SearchPage',
'path': 'searchPage/index.jsx',
},
];

const mockRequirables = {
'stdRequirable': {
meta: {
Expand Down Expand Up @@ -66,10 +78,18 @@ jest.mock('ltdRequirable.json',

describe('parseRequirables', () => {
it('returns the array of the resulting requirables', () => {
console.log();
expect(parseRequirables('my-content-dir')).toEqual([
{...mockRequirables['stdRequirable'], context: mockRequirables['stdRequirable'] },
{...mockRequirables['ltdRequirable'], context: mockRequirables['ltdRequirable']},
]);
});
});

describe('parseTemplates', () => {
it('returns an object with the appropriate structure and data', () => {
expect(parseTemplates(templates, 'my-templates-dir')).toEqual({
'SnippetPage': 'my-templates-dir/snippetPage/index.jsx',
'SearchPage': 'my-templates-dir/searchPage/index.jsx',
});
});
});

0 comments on commit 0fdceeb

Please sign in to comment.