template.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const { copySync, renameSync, existsSync, mkdirSync, removeSync } = require('fs-extra')
  2. const { resolve, join, normalize } = require('path')
  3. const logger = require('./helpers/logger')
  4. const log = logger('app:tauri', 'green')
  5. const warn = logger('app:tauri (template)', 'red')
  6. const injectConfFile = (injectPath, force, logging, directory) => {
  7. const dir = normalize(join(injectPath, '..'))
  8. const path = join(dir, 'tauri.conf.js')
  9. if (existsSync(path) && force !== 'conf' && force !== 'all') {
  10. warn(`tauri.conf.js found in ${path}
  11. Run \`tauri init --force conf\` to overwrite.`)
  12. if (!force) return false
  13. } else {
  14. try {
  15. removeSync(path)
  16. copySync(resolve(__dirname, './templates/tauri.conf.js'), path)
  17. } catch (e) {
  18. if (logging) console.log(e)
  19. return false
  20. } finally {
  21. if (logging) log('Successfully wrote tauri.conf.js')
  22. }
  23. }
  24. }
  25. const injectTemplate = (injectPath, force, logging, directory) => {
  26. if (existsSync(injectPath) && force !== 'template' && force !== 'all') {
  27. warn(`Tauri dir (${injectPath}) not empty.
  28. Run \`tauri init --force template\` to overwrite.`)
  29. if (!force) return false
  30. }
  31. try {
  32. removeSync(injectPath)
  33. mkdirSync(injectPath)
  34. copySync(resolve(__dirname, './templates/rust'), injectPath)
  35. } catch (e) {
  36. if (logging) console.log(e)
  37. return false
  38. }
  39. const files = require('fast-glob').sync(['**/_*'], {
  40. cwd: injectPath
  41. })
  42. for (const rawPath of files) {
  43. const targetRelativePath = rawPath.split('/').map(name => {
  44. // dotfiles are ignored when published to npm, therefore in templates
  45. // we need to use underscore instead (e.g. "_gitignore")
  46. if (name.charAt(0) === '_' && name.charAt(1) !== '_') {
  47. return `.${name.slice(1)}`
  48. }
  49. if (name.charAt(0) === '_' && name.charAt(1) === '_') {
  50. return `${name.slice(1)}`
  51. }
  52. return name
  53. }).join('/')
  54. try {
  55. renameSync(join(injectPath, rawPath), join(injectPath, targetRelativePath))
  56. } catch (e) {
  57. if (logging) console.log(e)
  58. return false
  59. } finally {
  60. if (logging) log('Successfully wrote tauri template files')
  61. }
  62. }
  63. }
  64. /**
  65. *
  66. * @param {string} injectPath
  67. * @param {string} type ['conf'|'template'|'all']
  68. * @param {string|boolean} [force=false] - One of[false|'conf'|'template'|'all']
  69. * @param {boolean} [logging=false]
  70. * @param {string} directory
  71. * @returns {boolean}
  72. */
  73. const inject = (injectPath, type, force = false, logging = false, directory) => {
  74. if (typeof type !== 'string' || typeof injectPath !== 'string') {
  75. warn('- internal error. Required params missing.')
  76. return false
  77. }
  78. if (type === 'conf' || type === 'all') {
  79. injectConfFile(injectPath, force, logging, directory)
  80. }
  81. if (type === 'template' || type === 'all') {
  82. injectTemplate(injectPath, force, logging, directory)
  83. }
  84. return true
  85. }
  86. module.exports = {
  87. inject
  88. }