tauri.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env node
  2. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  3. // SPDX-License-Identifier: Apache-2.0
  4. // SPDX-License-Identifier: MIT
  5. import chalk from 'chalk'
  6. import updateNotifier from 'update-notifier'
  7. import { findUpSync } from 'find-up'
  8. import { existsSync, readFileSync } from 'fs'
  9. import { resolve as resolvePath, dirname } from 'path'
  10. import { createRequire } from 'module'
  11. const require = createRequire(import.meta.url)
  12. const pkg = require('../package.json')
  13. const cmds = ['deps']
  14. const rustCliCmds = ['dev', 'build', 'init', 'info', 'sign']
  15. const cmd = process.argv[2]
  16. /**
  17. * @description This is the bootstrapper that in turn calls subsequent
  18. * Tauri Commands
  19. *
  20. * @param {string|array} command
  21. */
  22. const tauri = async function (command) {
  23. // notifying updates.
  24. if (!process.argv.some((arg) => arg === '--no-update-notifier')) {
  25. updateNotifier({
  26. pkg,
  27. updateCheckInterval: 0
  28. }).notify()
  29. }
  30. if (typeof command === 'object') {
  31. // technically we just care about an array
  32. command = command[0]
  33. }
  34. const help =
  35. !command || command === '-h' || command === '--help' || command === 'help'
  36. if (help) {
  37. console.log(`
  38. ${chalk.cyan(`
  39. :oooodddoooo; ;oddl, ,ol, ,oc, ,ldoooooooc, ,oc,
  40. ';;;cxOx:;;;' ;xOxxko' :kx: lkd, :xkl;;;;:okx: lkd,
  41. 'dOo' 'oOd;:xkc :kx: lkd, :xx: ;xkc lkd,
  42. 'dOo' ckx: lkx; :kx: lkd, :xx: :xkc lkd,
  43. 'dOo' ;xkl ,dko' :kx: lkd, :xx:.....xko, lkd,
  44. 'dOo' 'oOd, :xkc :kx: lkd, :xx:,;cokko' lkd,
  45. 'dOo' ckk: lkx; :kx: lkd, :xx: ckkc lkd,
  46. 'dOo' ;xOl lko; :xkl;,....;oOd, :xx: :xkl' lkd,
  47. 'okl' 'kd' 'xx' 'dxxxddddxxo' :dd; ;dxc 'xo'`)}
  48. ${chalk.yellow('Description')}
  49. This is the Tauri CLI
  50. ${chalk.yellow('Usage')}
  51. $ tauri ${[...rustCliCmds, ...cmds].join('|')}
  52. ${chalk.yellow('Options')}
  53. --help, -h Displays this message
  54. --version, -v Displays the Tauri CLI version
  55. `)
  56. process.exit(0)
  57. // eslint-disable-next-line no-unreachable
  58. return false // do this for node consumers and tests
  59. } else if (command === '-v' || command === '--version') {
  60. console.log(`${pkg.version}`)
  61. return false // do this for node consumers and tests
  62. } else if (cmds.includes(command)) {
  63. if (process.argv && process.env.NODE_ENV !== 'test') {
  64. process.argv.splice(2, 1)
  65. }
  66. console.log(`[tauri]: running ${command}`)
  67. await import(`./tauri-${command}.js`)
  68. } else {
  69. const { runOnRustCli } = await import('../dist/helpers/rust-cli.js')
  70. if (process.argv && process.env.NODE_ENV !== 'test') {
  71. process.argv.splice(0, 3)
  72. }
  73. let cwd = null
  74. const pkgJsonPath = findUpSync('package.json')
  75. if (pkgJsonPath) {
  76. const packageJson = JSON.parse(readFileSync(pkgJsonPath).toString())
  77. if ('tauri' in packageJson) {
  78. const { tauri: tauriConfig } = packageJson
  79. if (tauriConfig.appPath) {
  80. cwd = resolvePath(dirname(pkgJsonPath), tauriConfig.appPath)
  81. console.log(cwd)
  82. if (!existsSync(cwd)) {
  83. console.error(
  84. `Configured appPath in package.json '${cwd}' does not exist.`
  85. )
  86. process.exit(1)
  87. }
  88. }
  89. }
  90. }
  91. ;(
  92. await runOnRustCli(
  93. command,
  94. (process.argv || []).filter((v) => v !== '--no-update-notifier'),
  95. { cwd }
  96. )
  97. ).promise
  98. .then(() => {
  99. if (
  100. command === 'init' &&
  101. !process.argv.some((arg) => arg === '--ci' || arg === 'plugin')
  102. ) {
  103. return import('../dist/api/dependency-manager.js').then(
  104. ({ installDependencies }) => installDependencies()
  105. )
  106. }
  107. })
  108. .catch(() => process.exit(1))
  109. }
  110. }
  111. export default tauri
  112. // on test we use the module.exports
  113. if (process.env.NODE_ENV !== 'test') {
  114. tauri(cmd).catch((e) => {
  115. throw e
  116. })
  117. }