Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda