shellIntegration.service.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as path from 'path'
  2. import * as fs from 'mz/fs'
  3. import { Registry } from 'rage-edit-tmp'
  4. import { exec } from 'mz/child_process'
  5. import { Injectable } from '@angular/core'
  6. import { ElectronService } from './electron.service'
  7. import { HostAppService, Platform } from './hostApp.service'
  8. @Injectable()
  9. export class ShellIntegrationService {
  10. private automatorWorkflows = ['Open Terminus here.workflow', 'Paste path into Terminus.workflow']
  11. private automatorWorkflowsLocation: string
  12. private automatorWorkflowsDestination: string
  13. private registryKeys = [
  14. {
  15. path: 'HKCU\\Software\\Classes\\Directory\\Background\\shell\\Open Terminus here',
  16. command: 'open "%V"'
  17. },
  18. {
  19. path: 'HKCU\\Software\\Classes\\*\\shell\\Paste path into Terminus',
  20. command: 'paste "%V"'
  21. },
  22. ]
  23. constructor (
  24. private electron: ElectronService,
  25. private hostApp: HostAppService,
  26. ) {
  27. if (this.hostApp.platform === Platform.macOS) {
  28. this.automatorWorkflowsLocation = path.join(
  29. path.dirname(path.dirname(this.electron.app.getPath('exe'))),
  30. 'Resources',
  31. 'extras',
  32. 'automator-workflows',
  33. )
  34. this.automatorWorkflowsDestination = path.join(process.env.HOME, 'Library', 'Services')
  35. }
  36. this.updatePaths()
  37. }
  38. async updatePaths (): Promise<void> {
  39. // Update paths in case of an update
  40. if (this.hostApp.platform === Platform.Windows) {
  41. if (await this.isInstalled()) {
  42. await this.install()
  43. }
  44. }
  45. }
  46. async isInstalled (): Promise<boolean> {
  47. if (this.hostApp.platform === Platform.macOS) {
  48. return await fs.exists(path.join(this.automatorWorkflowsDestination, this.automatorWorkflows[0]))
  49. } else if (this.hostApp.platform === Platform.Windows) {
  50. return await Registry.has(this.registryKeys[0].path)
  51. }
  52. return true
  53. }
  54. async install () {
  55. if (this.hostApp.platform === Platform.macOS) {
  56. for (let wf of this.automatorWorkflows) {
  57. await exec(`cp -r "${this.automatorWorkflowsLocation}/${wf}" "${this.automatorWorkflowsDestination}"`)
  58. }
  59. } else if (this.hostApp.platform === Platform.Windows) {
  60. for (let registryKey of this.registryKeys) {
  61. await Registry.set(registryKey.path, 'Icon', this.electron.app.getPath('exe'))
  62. await Registry.set(registryKey.path + '\\command', '', this.electron.app.getPath('exe') + ' ' + registryKey.command)
  63. }
  64. }
  65. }
  66. }