tauri-init.js 4.1 KB

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