tauri-init.js 3.9 KB

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