shell.ts 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { invokeTauriCommand } from './helpers/tauri'
  2. /**
  3. * spawns a process
  4. *
  5. * @param command the name of the cmd to execute e.g. 'mkdir' or 'node'
  6. * @param [args] command args
  7. * @return promise resolving to the stdout text
  8. */
  9. async function execute (
  10. command: string,
  11. args?: string | string[]
  12. ): Promise<string> {
  13. if (typeof args === 'object') {
  14. Object.freeze(args)
  15. }
  16. return invokeTauriCommand<string>({
  17. __tauriModule: 'Shell',
  18. message: {
  19. cmd: 'execute',
  20. command,
  21. args: typeof args === 'string' ? [args] : args
  22. }
  23. })
  24. }
  25. /**
  26. * opens a path or URL with the system's default app,
  27. * or the one specified with `openWith`
  28. *
  29. * @param path the path or URL to open
  30. * @param openWith the app to open the file or URL with
  31. */
  32. async function open (path: string, openWith?: string): Promise<void> {
  33. return invokeTauriCommand({
  34. __tauriModule: 'Shell',
  35. message: {
  36. cmd: 'open',
  37. path,
  38. with: openWith
  39. }
  40. })
  41. }
  42. export { execute, open }