copy-templates.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // forked from https://github.com/quasarframework/quasar/blob/master/app/lib/app-extension/Extension.js
  2. function renderFolders ({ source, target, scope }) {
  3. const
  4. fs = require('fs-extra')
  5. const { join, resolve } = require('path')
  6. const fglob = require('fast-glob')
  7. const isBinary = require('isbinaryfile').isBinaryFileSync
  8. const compileTemplate = require('lodash.template')
  9. const files = fglob.sync(['**/*'], {
  10. cwd: source
  11. })
  12. for (const rawPath of files) {
  13. const targetRelativePath = rawPath.split('/').map(name => {
  14. // dotfiles are ignored when published to npm, therefore in templates
  15. // we need to use underscore instead (e.g. "_gitignore")
  16. if (name.charAt(0) === '_' && name.charAt(1) !== '_') {
  17. return `.${name.slice(1)}`
  18. }
  19. if (name.charAt(0) === '_' && name.charAt(1) === '_') {
  20. return `${name.slice(1)}`
  21. }
  22. return name
  23. }).join('/')
  24. const targetPath = join(target, targetRelativePath)
  25. const sourcePath = resolve(source, rawPath)
  26. fs.ensureFileSync(targetPath)
  27. if (isBinary(sourcePath)) {
  28. fs.copyFileSync(sourcePath, targetPath)
  29. } else {
  30. const rawContent = fs.readFileSync(sourcePath, 'utf-8')
  31. const template = compileTemplate(rawContent, {
  32. interpolate: /<%=([\s\S]+?)%>/g
  33. })
  34. fs.writeFileSync(targetPath, template(scope), 'utf-8')
  35. }
  36. }
  37. }
  38. module.exports = renderFolders