W
W
Wasya UK2018-10-04 15:19:53
JavaScript
Wasya UK, 2018-10-04 15:19:53

How to check files synchronously in Node?

Can't read file names synchronously (problem via callback). How to do such operations correctly?

const fs = require('fs');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

function findFiles(dir, regExp) {
  return fs.readdir(dir, (err, files) => {
    if (err) throw err;
    return files.filter(file => regExp.test(file));
  });
}

module.exports = dirname => {
  let HTMLFiles = findFiles(path.resolve(dirname, 'examples'), /\.m?js$/);

  return {
    plugins: HTMLFiles.map(file => new HtmlWebpackPlugin({
      inject: false,
      template: path.resolve(dirname, `examples/${file}`)
    }))
  };
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-10-04
@dmc1989

function findFiles(dir, regExp) {
  const files = fs.readdirSync(dir);

  return files.filter(file => regExp.test(file));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question