tauri-init.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const parseArgs = require('minimist')
  2. const inquirer = require('inquirer')
  3. const {
  4. resolve
  5. } = require('path')
  6. const {
  7. readFileSync,
  8. writeFileSync
  9. } = require('fs')
  10. const {
  11. merge
  12. } = require('lodash')
  13. const toml = require('@tauri-apps/toml')
  14. /**
  15. * @type {object}
  16. * @property {boolean} h
  17. * @property {boolean} help
  18. * @property {string|boolean} f
  19. * @property {string|boolean} force
  20. * @property {boolean} l
  21. * @property {boolean} log
  22. * @property {boolean} d
  23. * @property {boolean} directory
  24. */
  25. const argv = parseArgs(process.argv.slice(2), {
  26. alias: {
  27. h: 'help',
  28. f: 'force',
  29. l: 'log',
  30. d: 'directory',
  31. t: 'tauri-path',
  32. A: 'app-name',
  33. W: 'window-title',
  34. D: 'dist-dir',
  35. P: 'dev-path'
  36. },
  37. boolean: ['h', 'l', 'ci']
  38. })
  39. if (argv.help) {
  40. console.log(`
  41. Description
  42. Inits the Tauri template. If Tauri cannot find the tauri.conf.json
  43. it will create one.
  44. Usage
  45. $ tauri init
  46. Options
  47. --help, -h Displays this message
  48. --ci Skip prompts
  49. --force, -f Force init to overwrite [conf|template|all]
  50. --log, -l Logging [boolean]
  51. --directory, -d Set target directory for init
  52. --tauri-path, -t Path of the Tauri project to use (relative to the cwd)
  53. --app-name, -A Name of your Tauri application
  54. --window-title, -W Window title of your Tauri application
  55. --dist-dir, -D Web assets location, relative to <project-dir>/src-tauri
  56. --dev-path, -P Url of your dev server
  57. `)
  58. process.exit(0)
  59. }
  60. let appName = argv.A
  61. if (!appName) {
  62. try {
  63. const packageJson = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json')).toString())
  64. appName = packageJson.displayName || packageJson.name
  65. } catch {}
  66. }
  67. if (argv.ci) {
  68. runInit()
  69. } else {
  70. inquirer
  71. .prompt([{
  72. type: 'input',
  73. name: 'appName',
  74. message: 'What is your app name?',
  75. default: appName
  76. }, {
  77. type: 'input',
  78. name: 'tauri.window.title',
  79. message: 'What should the window title be?',
  80. default: 'Tauri App',
  81. when: () => !argv.W
  82. },
  83. {
  84. type: 'input',
  85. name: 'build.distDir',
  86. message: 'Where are your web assets (HTML/CSS/JS) located, relative to the "<current dir>/src-tauri" folder that will be created?',
  87. default: '../dist',
  88. when: () => !argv.D
  89. },
  90. {
  91. type: 'input',
  92. name: 'build.devPath',
  93. message: 'What is the url of your dev server?',
  94. default: 'http://localhost:4000',
  95. when: () => !argv.P
  96. }
  97. ])
  98. .then(answers => {
  99. runInit(answers)
  100. })
  101. .catch(error => {
  102. if (error.isTtyError) {
  103. // Prompt couldn't be rendered in the current environment
  104. console.log(
  105. 'It appears your terminal does not support interactive prompts. Using default values.'
  106. )
  107. runInit()
  108. } else {
  109. // Something else when wrong
  110. console.error('An unknown error occurred:', error)
  111. }
  112. })
  113. }
  114. function runInit(config = {}) {
  115. const {
  116. appName,
  117. ...configOptions
  118. } = config
  119. const init = require('../dist/api/init')
  120. const directory = argv.d || process.cwd()
  121. init({
  122. directory,
  123. force: argv.f || null,
  124. logging: argv.l || null,
  125. tauriPath: argv.t || null,
  126. customConfig: merge(configOptions, {
  127. build: {
  128. distDir: argv.D,
  129. devPath: argv.p
  130. },
  131. tauri: {
  132. window: {
  133. title: argv.w
  134. }
  135. }
  136. })
  137. })
  138. if (appName || argv.A) {
  139. const manifestPath = resolve(directory, 'src-tauri/Cargo.toml')
  140. const cargoManifest = toml.parse(readFileSync(manifestPath).toString())
  141. let binName = (appName || argv.A).replace(/ /g, '-')
  142. cargoManifest.package.name = binName
  143. cargoManifest.package['default-run'] = binName
  144. if (cargoManifest.bin && cargoManifest.bin.length) {
  145. cargoManifest.bin[0].name = binName
  146. }
  147. writeFileSync(manifestPath, toml.stringify(cargoManifest))
  148. }
  149. }