1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | class Base {
|
5 | constructor({ pre, post } = {}) {
|
6 | this.pre = pre || '';
|
7 | this.post = post || '';
|
8 | }
|
9 | addToken(token) {
|
10 | this._tokens = this._tokens || [];
|
11 | this._tokens.push(token);
|
12 | }
|
13 | get content() {
|
14 | return [this.pre, this._content, this.post, ...(this._tokens || []).map(e => e.content)].join('');
|
15 | }
|
16 | get _content() {
|
17 | return '';
|
18 | }
|
19 | }
|
20 | exports.Base = Base;
|
21 | class Prop extends Base {
|
22 | constructor(opts) {
|
23 | super(opts);
|
24 | this.type = 'prop';
|
25 | this.name = opts.name;
|
26 | this.value = opts.value;
|
27 | }
|
28 | get _content() {
|
29 | return `${this.name} ${this.value}`;
|
30 | }
|
31 | }
|
32 | exports.Prop = Prop;
|
33 | class MachineBase extends Base {
|
34 | constructor(_a = {}) {
|
35 | var { login, password, account } = _a, opts = tslib_1.__rest(_a, ["login", "password", "account"]);
|
36 | super(opts);
|
37 | this._tokens = [];
|
38 | if (password)
|
39 | this.password = password;
|
40 | if (account)
|
41 | this.account = account;
|
42 | if (login)
|
43 | this.login = login;
|
44 | }
|
45 | get login() {
|
46 | return this.getProp('login');
|
47 | }
|
48 | get password() {
|
49 | return this.getProp('password');
|
50 | }
|
51 | get account() {
|
52 | return this.getProp('account');
|
53 | }
|
54 | set login(v) {
|
55 | this.setProp('login', v);
|
56 | }
|
57 | set password(v) {
|
58 | this.setProp('password', v);
|
59 | }
|
60 | set account(v) {
|
61 | this.setProp('account', v);
|
62 | }
|
63 | getProp(name) {
|
64 | const p = this._tokens.find(p => p.type === 'prop' && p.name === name);
|
65 | return p && p.value;
|
66 | }
|
67 | setProp(name, value) {
|
68 | if (!value) {
|
69 | this._tokens = this._tokens.filter(p => p.type === 'prop' && p.name !== name);
|
70 | return;
|
71 | }
|
72 | let p = this._tokens.find(p => p.type === 'prop' && p.name === name);
|
73 | if (p) {
|
74 | p.value = value;
|
75 | }
|
76 | else {
|
77 | this._tokens.unshift(new Prop({ name, value, pre: this.newPropPre(), post: this.newPropPost() }));
|
78 | }
|
79 | }
|
80 | get _props() {
|
81 | return this._tokens.filter(p => p.type === 'prop');
|
82 | }
|
83 | newPropPre() {
|
84 | return this._props[0] ? this._props[0].pre : this.isMultiline() ? ' ' : ' ';
|
85 | }
|
86 | newPropPost() {
|
87 | return this.isMultiline() ? '\n' : '';
|
88 | }
|
89 | isMultiline() {
|
90 | if (!this._tokens.length)
|
91 | return true;
|
92 | return this._tokens.reduce((c, p) => c + p.content.split('\n').length - 1, this.post.split('\n').length - 1) > 1;
|
93 | }
|
94 | }
|
95 | exports.MachineBase = MachineBase;
|
96 | class Machine extends MachineBase {
|
97 | constructor(_a) {
|
98 | var { host } = _a, opts = tslib_1.__rest(_a, ["host"]);
|
99 | super(opts);
|
100 | this.type = 'machine';
|
101 | this.host = host;
|
102 | }
|
103 | get _content() {
|
104 | return `machine ${this.host}`;
|
105 | }
|
106 | }
|
107 | exports.Machine = Machine;
|
108 | class DefaultMachine extends MachineBase {
|
109 | constructor() {
|
110 | super(...arguments);
|
111 | this.type = 'default';
|
112 | }
|
113 | get _content() {
|
114 | return 'default';
|
115 | }
|
116 | }
|
117 | exports.DefaultMachine = DefaultMachine;
|
118 | function machinesProxy(tokens = []) {
|
119 | return new Proxy({}, {
|
120 | get: (_, host) => {
|
121 | if (typeof host !== 'string')
|
122 | return tokens[host];
|
123 | return tokens.find(m => m.type === 'machine' && m.host === host);
|
124 | },
|
125 | set: (_, host, v) => {
|
126 | let idx = tokens.findIndex(m => m.type === 'machine' && m.host === host);
|
127 | if (v) {
|
128 | let newMachine = new Machine(Object.assign({}, v, { host, post: '\n' }));
|
129 | if (idx === -1) {
|
130 | if (tokens.length === 1 && tokens[0].type === 'newline')
|
131 | tokens.splice(0, 1);
|
132 | else
|
133 | tokens.push({ type: 'newline', content: '\n' });
|
134 | tokens.push(newMachine);
|
135 | }
|
136 | else
|
137 | tokens[idx] = newMachine;
|
138 | }
|
139 | else if (idx !== -1) {
|
140 | tokens.splice(idx, 1);
|
141 | }
|
142 | return true;
|
143 | },
|
144 | has: (_, host) => {
|
145 | if (typeof host !== 'string')
|
146 | return !!tokens[host];
|
147 | return !!tokens.find(m => m.type === 'machine' && m.host === host);
|
148 | },
|
149 | deleteProperty: (_, host) => {
|
150 | const idx = tokens.findIndex(m => m.type === 'machine' && m.host === host);
|
151 | if (idx === -1)
|
152 | return true;
|
153 | tokens.splice(idx, 1);
|
154 | return true;
|
155 | },
|
156 | });
|
157 | }
|
158 | exports.machinesProxy = machinesProxy;
|