Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
How to use a method multiple times in one?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 });
    );
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!