Objective:
So I'm using Eleventy as an SSG and want to create multiple folders and copy some files into these folders, before Eleventy is doing the building or site generation.
A JSON file is for the array of site titles (folder names).
And I chose to make use the fs-extra module instead of the Node.js fs module.
Inside .eleventy.js (config file):
const fse = require('fs-extra');
const arrayFromJSON = require('./_data/sites.json');
const arrayTitles = arrayFromJSON.titles;
eleventyConfig.on("beforeBuild", () => {
// Create site folders and copy layout and frontmatter data to the folders
arrayTitles.forEach(element =>
fse.copySync(`sitesource/index.njk`, `src/_sites/${element}/${element}.njk`, { recursive: true }));
arrayTitles.forEach(element =>
fse.copySync(`sitesource/fm.11tydata.js`, `src/_sites/${element}/${element}.11tydata.js`, { recursive: true }));
});
I've been looking around for ideas and examples to combine these two actions into one, where it's only needed once to call arrayTitles.forEach. But I haven't found it.
Is this possible, and anyone with suggestions to improve this code?
I may be confused, but why not simply use one forEach?
eleventyConfig.on("beforeBuild", () => {
// Create site folders and copy layout and frontmatter data to the folders
arrayTitles.forEach(element =>
fse.copySync(`sitesource/index.njk`, `src/_sites/${element}/${element}.njk`, { recursive: true });
fse.copySync(`sitesource/fm.11tydata.js`, `src/_sites/${element}/${element}.11tydata.js`, { recursive: true });
);
});