A
A
Alex2019-02-25 14:00:25
gulp.js
Alex, 2019-02-25 14:00:25

How to get a list of files in a directory using gulp and write the name as a line to another file?

I need to generate a single file with an array of existing files in a subdirectory that will later be imported and used at runtime.

— files
——file1.txt
——file2.txt
—src
——file_names.js

// file_names.js
module.exports = ['file1', 'file2']

As I understand it, in the gulp task, you need to get all the files in the directory as an array, pull out their names, form a suitable line of code and write it to the file I need. But I have no idea what and how, I'm not familiar with gulp.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2019-02-25
@Kozack

Everything turned out to be much easier.

async function task() {
  const files = await fs.readdir(path.resolve(__dirname, './files'))
  const code = `
    module.exports = ${JSON.stringify(files)}
  `
  return fs.writeFile(path.resolve(__dirname, dir.build + '/file_names.js'), code)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question