app-paths.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { existsSync } from 'fs'
  5. import { join, normalize, resolve, sep, isAbsolute } from 'path'
  6. import logger from './logger'
  7. import chalk from 'chalk'
  8. const warn = logger('tauri', chalk.red)
  9. function resolvePath(basePath: string, dir: string): string {
  10. return dir && isAbsolute(dir) ? dir : resolve(basePath, dir)
  11. }
  12. const getAppDir = (): string => {
  13. let dir = process.cwd()
  14. let count = 0
  15. // only go up three folders max
  16. while (dir.length > 0 && !dir.endsWith(sep) && count <= 2) {
  17. if (existsSync(join(dir, 'src-tauri', 'tauri.conf.json'))) {
  18. return dir
  19. }
  20. count++
  21. dir = normalize(join(dir, '..'))
  22. }
  23. warn(
  24. "Couldn't find recognize the current folder as a part of a Tauri project"
  25. )
  26. process.exit(1)
  27. }
  28. const appDir = getAppDir()
  29. const tauriDir = resolve(appDir, 'src-tauri')
  30. const resolveDir = {
  31. app: (dir: string) => resolvePath(appDir, dir),
  32. tauri: (dir: string) => resolvePath(tauriDir, dir)
  33. }
  34. export { appDir, tauriDir, resolveDir as resolve }