Skip to content

Commit

Permalink
Merge 0c0c394 into a97c7d7
Browse files Browse the repository at this point in the history
  • Loading branch information
tharders committed Jun 4, 2021
2 parents a97c7d7 + 0c0c394 commit fad81d4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 51 deletions.
55 changes: 23 additions & 32 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,35 @@ const getConfig = async (args) => {
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN,
});
let contentfulCliOptions = {};
try {
// get configuration from contentful rc file (created by the contentful cli command)
const explorer = cosmiconfig('contentful');
const explorerResult = await explorer.search();
if (explorerResult !== null) {
const { config } = explorerResult || {};
const { managementToken, activeSpaceId, activeEnvironmentId, host } = config || {};
contentfulCliOptions = removeEmpty({
spaceId: activeSpaceId,
managementToken: managementToken,
environmentId: activeEnvironmentId,
host,
});
}
} catch (error) {
console.log('Error:', error.message);
console.log(error);
// get configuration from contentful rc file (created by the contentful cli command)
const cgConfig = cosmiconfig('contentful');
const cgConfigResult = await cgConfig.search();
if (cgConfigResult !== null) {
const { config } = cgConfigResult || {};
const { managementToken, activeSpaceId, activeEnvironmentId, host } = config || {};
contentfulCliOptions = removeEmpty({
spaceId: activeSpaceId,
managementToken: managementToken,
environmentId: activeEnvironmentId,
host,
});
}

let configFileOptions = {};
try {
// get configuration from contentful-ssg rc file
const explorer = cosmiconfig('contentful-ssg');
const explorerResult = await explorer.search();
if (explorerResult !== null) {
const { config, filepath } = explorerResult || {};
// get configuration from contentful-ssg rc file
const cssgConfig = cosmiconfig('contentful-ssg');
const cssgConfigResult = await cssgConfig.search();
if (cssgConfigResult !== null) {
const { config, filepath } = cssgConfigResult || {};

configFileOptions = {
directory: path.resolve(path.dirname(filepath || ''), args.directory || 'content'),
...(config || {}),
};
configFileOptions = {
directory: path.resolve(path.dirname(filepath || ''), args.directory || 'content'),
...(config || {}),
};

if (configFileOptions.directory && !path.isAbsolute(configFileOptions.directory)) {
configFileOptions.directory = path.resolve(path.dirname(filepath || ''), configFileOptions.directory);
}
if (configFileOptions.directory && !path.isAbsolute(configFileOptions.directory)) {
configFileOptions.directory = path.resolve(path.dirname(filepath || ''), configFileOptions.directory);
}
} catch (error) {
console.log('Error:', error.message);
}

return mergeOptions(defaultOptions, contentfulCliOptions, environmentOptions, configFileOptions, args || {});
Expand Down
49 changes: 40 additions & 9 deletions lib/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const dlv = require('dlv');
const ignore = require('ignore');
const findUp = require('find-up');
const mm = require('micromatch');
const UpdaterRenderer = require('listr-update-renderer');
const fs = require('fs-extra');
const { getContent, getContentTypeId, getContentId, getFieldSettings, convertToMap } = require('./contentful');
const { mapAsync, forEachAsync } = require('./array');
Expand Down Expand Up @@ -70,6 +69,43 @@ const outputStats = (stats, locale) => {
console.log(`\n Saved ${chalk.cyan(cnt.reduce((acc, num) => acc + num, 0))} entries`);
};

/**
* This is a very simple listr renderer which does not swallow log output from
* the configured helper functions.
*/
class CustomListrRenderer {
constructor(tasks) {
this._tasks = tasks;
}

static get nonTTY() {
return false;
}

static subscribeTasks(tasks, indent = '') {
for (const task of tasks) {
task.subscribe((event) => {
if (event.type === 'STATE' && task.isPending()) {
console.log(`${indent} ${chalk.yellow('\u279E')} ${task.title}`);
}
if (event.type === 'SUBTASKS' && task.subtasks.length > 0) {
CustomListrRenderer.subscribeTasks(task.subtasks, indent + ' ');
}
});
}
}

render() {
CustomListrRenderer.subscribeTasks(this._tasks);
}

end(err) {
if (!err) {
console.log(` ${chalk.green('✔')} all tasks done!`);
}
}
}

/**
* Dump contentful objects to files
* @param {Object} config
Expand Down Expand Up @@ -290,16 +326,11 @@ const dump = async (config) => {
},
},
],
{ collapse: true, renderer: UpdaterRenderer }
{ collapse: true, renderer: CustomListrRenderer }
);

try {
const ctx = await tasks.run();
outputStats(ctx.stats, ctx.defauleLocale);
} catch (error) {
console.error(error);
}

const ctx = await tasks.run();
outputStats(ctx.stats, ctx.defauleLocale);
console.log('\n---------------------------------------------');
};

Expand Down
9 changes: 3 additions & 6 deletions lib/transform/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ const mapDateField = (fieldContent) => {
* @param {*} options
*/
const mapReferenceField = (fieldContent, options) => {
const {
assets = [],
entries = [],
mapAssetLink: customMapAssetLink,
mapEntryLink: customMapEntryLink,
} = options || {};
const { assets = [], entries = [], mapAssetLink: customMapAssetLink, mapEntryLink: customMapEntryLink } =
options || {};

if (isAssetLink(fieldContent)) {
const asset = assets.get(getContentId(fieldContent));
Expand Down Expand Up @@ -257,6 +253,7 @@ const mapEntry = async (entry, options) => {

module.exports.mapEntry = mapEntry;
module.exports.mapField = mapField;
module.exports.mapRichTextNodes = mapRichTextNodes;
module.exports.mapRichTextField = mapRichTextField;
module.exports.mapReferenceField = mapReferenceField;
module.exports.mapDateField = mapDateField;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"js-yaml": "^4.1.0",
"js-yaml-js-types": "^1.0.0",
"listr": "^0.14.3",
"listr-update-renderer": "^0.5.0",
"merge-options": "^3.0.4",
"micromatch": "^4.0.4",
"pkg-up": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion test/localize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ describe('Localize', () => {
title: 'FuBK',
description: 'Dummy image',
file: {
url: '//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
url:
'//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
details: {
size: 120093,
image: {
Expand Down
6 changes: 4 additions & 2 deletions test/mapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ describe('Mapper - mapEntry', () => {
location: { lon: 13.422140718124993, lat: 52.47504074424066 },
media: {
mimeType: 'image/svg+xml',
url: '//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
url:
'//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
title: 'FuBK',
description: 'Dummy image',
width: 768,
Expand All @@ -323,7 +324,8 @@ describe('Mapper - mapEntry', () => {
mediaList: [
{
mimeType: 'image/svg+xml',
url: '//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
url:
'//images.ctfassets.net/gpdredy5px7h/3t1t8PDynjpXbAzv6zOVQq/7f4143c74191766d87f86d0035d91d28/FuBK_testcard_vectorized.svg',
title: 'FuBK',
description: 'Dummy image',
width: 768,
Expand Down

0 comments on commit fad81d4

Please sign in to comment.