template.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { copySync, existsSync, removeSync, readFileSync } = require('fs-extra')
  2. const { resolve, join, normalize } = require('path')
  3. const copyTemplates = require('./helpers/copy-templates')
  4. const logger = require('./helpers/logger')
  5. const log = logger('app:tauri', 'green')
  6. const warn = logger('app:tauri (template)', 'red')
  7. const injectConfFile = (injectPath, { force, logging }) => {
  8. const path = join(injectPath, '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, tauriPath }) => {
  26. const dir = normalize(join(injectPath, 'src-tauri'))
  27. if (existsSync(dir) && force !== 'template' && force !== 'all') {
  28. warn(`Tauri dir (${dir}) not empty.
  29. Run \`tauri init --force template\` to overwrite.`)
  30. if (!force) return false
  31. }
  32. let tauriDep
  33. if (tauriPath) {
  34. tauriDep = `{ path = "${resolve(process.cwd(), tauriPath, 'tauri')}" }`
  35. } else {
  36. const toml = require('@tauri-apps/toml')
  37. const tomlPath = join(__dirname, '../../tauri/Cargo.toml')
  38. const tomlFile = readFileSync(tomlPath)
  39. const tomlContents = toml.parse(tomlFile)
  40. tauriDep = `{ version = "${tomlContents.package.version}" }`
  41. }
  42. try {
  43. removeSync(dir)
  44. copyTemplates({
  45. source: resolve(__dirname, './templates/src-tauri'),
  46. scope: {
  47. tauriDep
  48. },
  49. target: dir
  50. })
  51. } catch (e) {
  52. if (logging) console.log(e)
  53. return false
  54. }
  55. }
  56. /**
  57. *
  58. * @param {string} injectPath
  59. * @param {string} type ['conf'|'template'|'all']
  60. * @param {string|boolean} [force=false] - One of[false|'conf'|'template'|'all']
  61. * @param {boolean} [logging=false]
  62. * @param {string} [tauriPath=null]
  63. * @returns {boolean}
  64. */
  65. const inject = (injectPath, type, { force = false, logging = false, tauriPath = null }) => {
  66. if (typeof type !== 'string' || typeof injectPath !== 'string') {
  67. warn('- internal error. Required params missing.')
  68. return false
  69. }
  70. if (type === 'conf' || type === 'all') {
  71. injectConfFile(injectPath, { force, logging })
  72. }
  73. if (type === 'template' || type === 'all') {
  74. injectTemplate(injectPath, { force, logging, tauriPath })
  75. }
  76. return true
  77. }
  78. module.exports = {
  79. inject
  80. }