info.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import toml from '@tauri-apps/toml'
  2. import chalk from 'chalk'
  3. import { sync as spawn } from 'cross-spawn'
  4. import fs from 'fs'
  5. import os from 'os'
  6. import path from 'path'
  7. import { appDir, tauriDir } from '../helpers/app-paths'
  8. import { TauriConfig } from './../types/config'
  9. import { CargoToml } from './../types/cargo'
  10. import nonWebpackRequire from '../helpers/non-webpack-require'
  11. import { version } from '../../package.json'
  12. interface DirInfo {
  13. path: string
  14. name: string
  15. type?: 'folder' | 'file'
  16. children?: DirInfo[]
  17. }
  18. /* eslint-disable security/detect-non-literal-fs-filename */
  19. function dirTree(filename: string, recurse = true): DirInfo {
  20. const stats = fs.lstatSync(filename)
  21. const info: DirInfo = {
  22. path: filename,
  23. name: path.basename(filename)
  24. }
  25. if (stats.isDirectory()) {
  26. info.type = 'folder'
  27. if (recurse) {
  28. info.children = fs.readdirSync(filename).map(function(child: string) {
  29. return dirTree(filename + '/' + child, false)
  30. })
  31. }
  32. } else {
  33. info.type = 'file'
  34. }
  35. return info
  36. }
  37. function getVersion(
  38. command: string,
  39. args: string[] = [],
  40. formatter?: (output: string) => string
  41. ): string {
  42. try {
  43. const child = spawn(command, [...args, '--version'])
  44. if (child.status === 0) {
  45. const output = String(child.output[1])
  46. return chalk
  47. .green(formatter === undefined ? output : formatter(output))
  48. .replace('\n', '')
  49. }
  50. return chalk.red('Not installed')
  51. } catch (err) {
  52. return chalk.red('Not installed')
  53. }
  54. }
  55. interface Info {
  56. section?: boolean
  57. key: string
  58. value?: string
  59. }
  60. function printInfo(info: Info): void {
  61. console.log(
  62. `${info.section ? '\n' : ''}${info.key}${
  63. info.value === undefined ? '' : ' - ' + info.value
  64. }`
  65. )
  66. }
  67. function printAppInfo(tauriDir: string): void {
  68. printInfo({ key: 'App', section: true })
  69. try {
  70. const tomlPath = path.join(tauriDir, 'Cargo.toml')
  71. const tomlFile = fs.readFileSync(tomlPath).toString()
  72. const tomlContents = toml.parse(tomlFile) as any as CargoToml
  73. const tauriVersion = (): string => {
  74. const tauri = tomlContents.dependencies.tauri
  75. if (tauri) {
  76. if (typeof tauri === 'string') {
  77. return chalk.green(tauri)
  78. }
  79. if (tauri.version) {
  80. return chalk.green(tauri.version)
  81. }
  82. if (tauri.path) {
  83. try {
  84. const tauriTomlPath = path.resolve(
  85. tauriDir,
  86. tauri.path,
  87. 'Cargo.toml'
  88. )
  89. const tauriTomlFile = fs.readFileSync(tauriTomlPath).toString()
  90. const tauriTomlContents = toml.parse(tauriTomlFile) as any as CargoToml
  91. return chalk.green(
  92. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  93. `${tauriTomlContents.package.version} (from source)`
  94. )
  95. } catch (_) {}
  96. }
  97. }
  98. return chalk.red('unknown')
  99. }
  100. printInfo({ key: ' tauri', value: tauriVersion() })
  101. } catch (_) {}
  102. try {
  103. const tauriMode = (config: TauriConfig): string => {
  104. if (config.tauri.embeddedServer) {
  105. return chalk.green(
  106. config.tauri.embeddedServer.active ? 'embedded-server' : 'no-server'
  107. )
  108. }
  109. return chalk.red('unset')
  110. }
  111. const configPath = path.join(tauriDir, 'tauri.conf.json')
  112. const config = nonWebpackRequire(configPath) as TauriConfig
  113. printInfo({ key: ' mode', value: tauriMode(config) })
  114. printInfo({
  115. key: ' build-type',
  116. value:
  117. config.tauri.bundle && config.tauri.bundle.active ? 'bundle' : 'build'
  118. })
  119. printInfo({
  120. key: ' CSP',
  121. value: config.tauri.security ? config.tauri.security.csp : 'unset'
  122. })
  123. printInfo({
  124. key: ' Windows',
  125. value: config.tauri.edge && config.tauri.edge.active ? 'Edge' : 'MSHTML'
  126. })
  127. printInfo({
  128. key: ' distDir',
  129. value: config.build
  130. ? chalk.green(config.build.distDir)
  131. : chalk.red('unset')
  132. })
  133. printInfo({
  134. key: ' devPath',
  135. value: config.build
  136. ? chalk.green(config.build.devPath)
  137. : chalk.red('unset')
  138. })
  139. } catch (_) {}
  140. }
  141. module.exports = () => {
  142. printInfo({
  143. key: 'Operating System',
  144. value: chalk.green(
  145. `${os.type()}(${os.release()}) - ${os.platform()}/${os.arch()}`
  146. ),
  147. section: true
  148. })
  149. printInfo({ key: 'Node.js environment', section: true })
  150. printInfo({ key: ' Node.js', value: chalk.green(process.version.slice(1)) })
  151. printInfo({
  152. key: ' tauri.js',
  153. value: chalk.green(version)
  154. })
  155. printInfo({ key: 'Rust environment', section: true })
  156. printInfo({
  157. key: ' rustc',
  158. value: getVersion('rustc', [], output => output.split(' ')[1])
  159. })
  160. printInfo({
  161. key: ' cargo',
  162. value: getVersion('cargo', [], output => output.split(' ')[1])
  163. })
  164. printInfo({ key: ' tauri-bundler', value: getVersion('cargo', ['tauri-bundler']) })
  165. printInfo({ key: 'Global packages', section: true })
  166. printInfo({ key: ' NPM', value: getVersion('npm') })
  167. printInfo({ key: ' yarn', value: getVersion('yarn') })
  168. printInfo({ key: 'App directory structure', section: true })
  169. const tree = dirTree(appDir)
  170. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  171. for (const artifact of tree.children || []) {
  172. if (artifact.type === 'folder') {
  173. console.log(`/${artifact.name}`)
  174. }
  175. }
  176. printAppInfo(tauriDir)
  177. }
  178. /* eslint-enable security/detect-non-literal-fs-filename */