Skip to content

Commit

Permalink
Added safety checks in case fields are null
Browse files Browse the repository at this point in the history
  • Loading branch information
NJKode committed Dec 15, 2022
1 parent cb35d72 commit 4ca9a92
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/generators/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import log from '../util/logger.js';
import { parseFile } from '../parsers/parser.js';

export async function generateCollections(collectionsConfig = {}, options) {
collectionsConfig = collectionsConfig ?? {};
const source = join('.', options?.source || '');

return await Object.keys(collectionsConfig).reduce(async (memo, key) => {
Expand Down
3 changes: 2 additions & 1 deletion src/generators/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { stat } from 'fs/promises';
import { join } from 'path';
import { parseFile } from '../parsers/parser.js';

export async function generateData(dataConfig = {}, options) {
export async function generateData(dataConfig, options) {
dataConfig = dataConfig ?? {};
const source = join('.', options?.source || '');

return await Object.keys(dataConfig).reduce(async (memo, key) => {
Expand Down
20 changes: 19 additions & 1 deletion src/generators/info.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { generateCollections } from './collections.js';
import { generateData } from './data.js';

const sanitizeConfig = function (config) {
const isObject = (obj) => obj === Object(obj);
const defaults = {
source: '',
base_url: '',
'base-url': '',
};

let sanitizedConfig = config || {};
sanitizedConfig = isObject(sanitizedConfig) ? sanitizedConfig : {};

Object.keys(sanitizedConfig).forEach((configKey) => {
sanitizedConfig[configKey] = (sanitizedConfig[configKey] || defaults[configKey]) ?? {};
});
return sanitizedConfig;
};

export async function generateInfo(config, options) {
const source = config.source?.replace(/^\/+|\/+$/g, '').replace(/\/+/g, '/') || '';
const sanitizedConfig = sanitizeConfig(config);

return {
...config,
...sanitizedConfig,
time: new Date().toISOString(),
cloudcannon: {
name: 'cloudcannon-reader',
Expand Down
23 changes: 23 additions & 0 deletions tests/generators/expected/null-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"source": "",
"data_config": {},
"collections_config": {},
"_comments": {},
"_options": {},
"_structures": {},
"_select_data": {},
"generator": {},
"source_editor": {},
"paths": {
"uploads": null
},
"base_url": "",
"time": "2000-11-22T00:00:00.000Z",
"cloudcannon": {
"name": "cloudcannon-reader",
"version": "0.0.1"
},
"version": "0.0.3",
"data": {},
"collections": {}
}
15 changes: 15 additions & 0 deletions tests/generators/fixtures/null-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"source": null,
"data_config": null,
"collections_config": null,
"_comments": null,
"_options": null,
"_structures": null,
"_select_data": null,
"generator": null,
"source_editor": null,
"paths": {
"uploads": null
},
"base_url": null
}
1 change: 1 addition & 0 deletions tests/generators/info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ 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 null fields', async (t) => runTest(t, 'null-fields'));

0 comments on commit 4ca9a92

Please sign in to comment.