UNPKG

3.18 kBJavaScriptView Raw
1'use strict'
2Object.defineProperty(exports, '__esModule', { value: true })
3const tslib_1 = require('tslib')
4const Token = require('./token')
5// export abstract class Token {
6// public pre: string
7// public post: string
8// public _tokens: Token[] = []
9// constructor (opts: Options) {
10// this.pre = pre
11// this.post = post
12// }
13// get content(): string { return [this.pre, this._content, this.post, ...this.elements.map(e => e.content)].join('') }
14// protected get _content() { return '' }
15// }
16class Prop extends Token {
17 constructor(opts) {
18 super(opts)
19 this.type = 'prop'
20 this.name = opts.name
21 this.value = opts.value
22 }
23 get _content() {
24 return `${this.name} ${this.value}`
25 }
26}
27exports.Prop = Prop
28class MachineBase extends Token {
29 constructor(_a = {}) {
30 var { login, password, account } = _a,
31 opts = tslib_1.__rest(_a, ['login', 'password', 'account'])
32 super(opts)
33 if (login) this.login = login
34 if (password) this.password = password
35 if (account) this.account = account
36 }
37 get login() {
38 return this.getProp('login')
39 }
40 get password() {
41 return this.getProp('password')
42 }
43 get account() {
44 return this.getProp('account')
45 }
46 set login(v) {
47 this.setProp('login', v)
48 }
49 set password(v) {
50 this.setProp('password', v)
51 }
52 set account(v) {
53 this.setProp('account', v)
54 }
55 addProp(prop) {
56 this.elements.push(prop)
57 }
58 getProp(name) {
59 const p = this.elements.find(p => p.name === name)
60 return p && p.value
61 }
62 setProp(name, value) {
63 if (!value) {
64 this.elements = this.elements.filter(p => p.name === name)
65 return
66 }
67 let p = this.elements.find(p => p.name === name)
68 if (p) {
69 p.value = value
70 } else {
71 p = new Prop({ name, value })
72 this.elements.push(p)
73 }
74 console.log(this.elements)
75 }
76}
77exports.MachineBase = MachineBase
78class Machine extends MachineBase {
79 constructor(_a) {
80 var { host } = _a,
81 opts = tslib_1.__rest(_a, ['host'])
82 super(opts)
83 this.type = 'machine'
84 this.host = host
85 }
86 get _content() {
87 return `machine ${this.host}`
88 }
89}
90exports.Machine = Machine
91class DefaultMachine extends MachineBase {
92 constructor() {
93 super(...arguments)
94 this.type = 'default'
95 }
96 get _content() {
97 return 'default'
98 }
99}
100exports.DefaultMachine = DefaultMachine
101function machinesProxy(tokens = []) {
102 return new Proxy(
103 {},
104 {
105 get: (_, host) => {
106 if (typeof host !== 'string') return tokens[host]
107 return tokens.find(m => m.type === 'machine' && m.host === host)
108 },
109 set: (_, host, value) => {
110 tokens.push(new Machine(Object.assign({}, value, { host })))
111 return true
112 },
113 has: (_, host) => {
114 if (typeof host !== 'string') return !!tokens[host]
115 return !!tokens.find(m => m.type === 'machine' && m.host === host)
116 },
117 deleteProperty: (_, host) => {
118 const idx = tokens.findIndex(m => m.type === 'machine' && m.host === host)
119 if (idx === -1) return false
120 tokens.splice(idx, 1)
121 return true
122 },
123 },
124 )
125}
126exports.machinesProxy = machinesProxy