info.ts 4.8 KB

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