injector.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const { copySync, renameSync, existsSync, mkdirSync } = require('fs-extra'),
  2. path = require('path')
  3. class TauriInjector {
  4. constructor(appPaths) {
  5. this.appPaths = appPaths
  6. }
  7. configDir() {
  8. return path.resolve(__dirname, '..')
  9. }
  10. injectTemplate() {
  11. if (existsSync(this.appPaths.tauriDir)) {
  12. console.log(`Tauri dir (${this.appPaths.tauriDir}) not empty.`)
  13. return false
  14. }
  15. mkdirSync(this.appPaths.tauriDir)
  16. copySync(path.resolve(__dirname, '../templates/rust'), this.appPaths.tauriDir)
  17. const files = require('fast-glob').sync(['**/_*'], {
  18. cwd: this.appPaths.tauriDir
  19. })
  20. for (const rawPath of files) {
  21. const targetRelativePath = rawPath.split('/').map(name => {
  22. // dotfiles are ignored when published to npm, therefore in templates
  23. // we need to use underscore instead (e.g. "_gitignore")
  24. if (name.charAt(0) === '_' && name.charAt(1) !== '_') {
  25. return `.${name.slice(1)}`
  26. }
  27. if (name.charAt(0) === '_' && name.charAt(1) === '_') {
  28. return `${name.slice(1)}`
  29. }
  30. return name
  31. }).join('/')
  32. renameSync(this.appPaths.resolve.tauri(rawPath), this.appPaths.resolve.tauri(targetRelativePath))
  33. }
  34. return true
  35. }
  36. }
  37. module.exports = TauriInjector