tauri-init.js 4.1 KB

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