environmentEditor.component.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  2. import { Component, Output, Input } from '@angular/core'
  3. import { Subject } from 'rxjs'
  4. /** @hidden */
  5. @Component({
  6. selector: 'environment-editor',
  7. templateUrl:'./environmentEditor.component.pug',
  8. styleUrls: ['./environmentEditor.component.scss'],
  9. })
  10. export class EnvironmentEditorComponent {
  11. @Output() modelChange = new Subject<any>()
  12. vars: { key: string, value: string }[] = []
  13. private cachedModel: any
  14. @Input() get model (): any {
  15. return this.cachedModel
  16. }
  17. set model (value) {
  18. this.vars = Object.entries(value).map(([k, v]) => ({ key: k, value: v as string }))
  19. this.cachedModel = this.getModel()
  20. }
  21. getModel () {
  22. const model = {}
  23. for (const pair of this.vars) {
  24. model[pair.key] = pair.value
  25. }
  26. return model
  27. }
  28. emitUpdate () {
  29. this.cachedModel = this.getModel()
  30. this.modelChange.next(this.cachedModel)
  31. }
  32. addEnvironmentVar () {
  33. this.vars.push({ key: '', value: '' })
  34. }
  35. removeEnvironmentVar (key: string) {
  36. this.vars = this.vars.filter(x => x.key !== key)
  37. this.emitUpdate()
  38. }
  39. shouldShowExample (): boolean {
  40. return !this.vars.find(v => v.key.toLowerCase() === 'path')
  41. }
  42. addExample (): void {
  43. const value = process.platform === 'win32' ? 'C:\\Program Files\\Custom:%PATH%' : '/opt/custom:$PATH'
  44. this.vars.push({ key: 'PATH', value })
  45. this.emitUpdate()
  46. }
  47. }