readFiles.js 668 B

123456789101112131415161718192021222324
  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. const globby = require('globby')
  4. const { isBinaryFileSync } = require('isbinaryfile')
  5. const normalizeFilePaths = require('./normalizeFilePaths')
  6. module.exports = async function readFiles (context) {
  7. const files = await globby(['**'], {
  8. cwd: context,
  9. onlyFiles: true,
  10. gitignore: true,
  11. ignore: ['**/node_modules/**', '**/.git/**'],
  12. dot: true
  13. })
  14. const res = {}
  15. for (const file of files) {
  16. const name = path.resolve(context, file)
  17. res[file] = isBinaryFileSync(name)
  18. ? fs.readFileSync(name)
  19. : fs.readFileSync(name, 'utf-8')
  20. }
  21. return normalizeFilePaths(res)
  22. }