tauri-init.js 3.5 KB

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