Answer the question
In order to leave comments, you need to log in
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']
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question