Skip to content

Commit

Permalink
flatten refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 2, 2022
1 parent 0a1283b commit 7df62ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion greenwood.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
}
},
greenwoodPluginIncludeHTML(),
...greenwoodPluginRendererPuppeteer()
greenwoodPluginRendererPuppeteer()
],
markdown: {
plugins: [
Expand Down
54 changes: 29 additions & 25 deletions packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath, pathToFileURL, URL } from 'url';
// get and "tag" all plugins provided / maintained by the @greenwood/cli
// and include as the default set, with all user plugins getting appended
const greenwoodPluginsBasePath = fileURLToPath(new URL('../plugins', import.meta.url));
const PLUGINS_FLATTENED_DEPTH = 2;

const greenwoodPlugins = (await Promise.all([
path.join(greenwoodPluginsBasePath, 'copy'),
Expand All @@ -14,16 +15,16 @@ const greenwoodPlugins = (await Promise.all([
].map(async (pluginDirectory) => {
const files = await fs.promises.readdir(pluginDirectory);

return (await Promise.all(files.map(async(file) => {
return await Promise.all(files.map(async(file) => {
const importPaTh = pathToFileURL(`${pluginDirectory}${path.sep}${file}`);
const pluginImport = await import(importPaTh);
const plugin = pluginImport[Object.keys(pluginImport)[0]];

return Array.isArray(plugin)
? plugin
: [plugin];
}))).flat();
}).flat())).flat().map((plugin) => {
}));
}))).flat(PLUGINS_FLATTENED_DEPTH).map((plugin) => {
return {
isGreenwoodDefaultPlugin: true,
...plugin
Expand Down Expand Up @@ -99,39 +100,42 @@ const readAndMergeConfig = async() => {
}

if (plugins && plugins.length > 0) {
plugins.forEach(plugin => {
const flattened = (Array.isArray(plugin) ? plugin : [plugin]).flat();
const flattened = plugins.flat(PLUGINS_FLATTENED_DEPTH);

flattened.forEach(plugin => {
if (!plugin.type || pluginTypes.indexOf(plugin.type) < 0) {
reject(`Error: greenwood.config.js plugins must be one of type "${pluginTypes.join(', ')}". got "${plugin.type}" instead.`);
}

if (!plugin.provider || typeof plugin.provider !== 'function') {
const providerTypeof = typeof plugin.provider;
flattened.forEach(plugin => {
if (!plugin.type || pluginTypes.indexOf(plugin.type) < 0) {
reject(`Error: greenwood.config.js plugins must be one of type "${pluginTypes.join(', ')}". got "${plugin.type}" instead.`);
}

reject(`Error: greenwood.config.js plugins provider must be a function. got ${providerTypeof} instead.`);
}
if (!plugin.provider || typeof plugin.provider !== 'function') {
const providerTypeof = typeof plugin.provider;

if (!plugin.name || typeof plugin.name !== 'string') {
const nameTypeof = typeof plugin.name;
reject(`Error: greenwood.config.js plugins provider must be a function. got ${providerTypeof} instead.`);
}

reject(`Error: greenwood.config.js plugins must have a name. got ${nameTypeof} instead.`);
}
});
if (!plugin.name || typeof plugin.name !== 'string') {
const nameTypeof = typeof plugin.name;

customConfig.plugins = [
...customConfig.plugins,
...flattened
];
reject(`Error: greenwood.config.js plugins must have a name. got ${nameTypeof} instead.`);
}
});

// if user provided a custom renderer, filter out Greenwood's default renderer
if (customConfig.plugins.filter(plugin => plugin.type === 'renderer').length > 1) {
const customRendererPlugins = flattened.filter(plugin => plugin.type === 'renderer').length;

if (customRendererPlugins === 1) {
customConfig.plugins = customConfig.plugins.filter((plugin) => {
return plugin.type !== 'renderer' || plugin.type === 'renderer' && !plugin.isGreenwoodDefaultPlugin;
return plugin.type !== 'renderer';
});
} else if (customRendererPlugins > 1) {
console.warn('More than one custom renderer plugin detected. Please make sure you are only loading one.');
console.debug(plugins.filter(plugin => plugin.type === 'renderer'));
}

customConfig.plugins = [
...customConfig.plugins,
...flattened
];
}

if (devServer && Object.keys(devServer).length > 0) {
Expand Down

0 comments on commit 7df62ca

Please sign in to comment.