-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmls.js
132 lines (125 loc) · 4.16 KB
/
vmls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const {execSync} = require('child_process');
const path = require('path');
const createObjectByArrayKey = (key, obj, dir) => {
if (key.length == 0) {
obj.config = dir;
return obj;
}
else {
let _key = key.shift();
obj[_key] = obj[_key] || {};
return createObjectByArrayKey(key, obj[_key], dir);
}
}
const vmlistParse = line => {
let [name, value] = line.split(' = ');
let index = parseInt(name.substring(6, name.indexOf('.'))) - 1;
let prop = name.substring(name.indexOf('.') + 1);
value = value.substr(1, value.length - 2);
if (prop === 'config' && value.indexOf('folder') === 0) {
value = 'folder';
}
return {index, prop, value};
}
module.exports = {
'.encoding': "windows-1252",
get encoding() {
return this['.encoding'];
},
set encoding(value) {
this['.encoding'] = value;
},
get lists(){
return this['.lists'];
},
'.lists': [],
insertFolder({name, parent = 0}) {
if (name == null) throw Error(`folderInsert require name`);
let vmlist = {
config: 'folder' + (this['.lists'].length + 1).toString(),
Type: 2,
DisplayName: name,
ParentID: parent,
ItemID: this['.lists'].length + 1
};
this['.lists'].push(vmlist);
return vmlist;
},
insertVm({name, parent = 0, vmxPath}) {
if (vmxPath == null || name == null) throw Error(`vmInsert require name and vmxPath`);
let vmlist = {
config: vmxPath,
DisplayName: name,
ParentID: parent,
ItemID: this['.lists'].length + 1
};
this['.lists'].push(vmlist);
return vmlist;
},
toString(){
let content = [];
content.push(`.encoding = ${this.encoding}`);
this['.lists'].forEach((vmlist, index) => {
for (let key in vmlist) {
content.push(`vmlist${index + 1}.${key} = "${vmlist[key]}"`);
}
});
return content.join('\n');
},
parse(content){
content = content.split('\n');
if (content[0].charAt(0) == '.') {
let line = content.shift();
let [key, value] = line.split(' = ');
this.encoding = value.substr(1, value.length - 2);
}
for (let i = 0; i < content.length; i ++){
let line = content[i];
if (line.indexOf('vmlist') !== 0) break;
let {index, prop, value} = vmlistParse(line);
this['.lists'][index] = this['.lists'][index] || {};
this['.lists'][index][prop] = value;
};
return this;
},
minimal(){
this['.lists'] = this['.lists'].map(vmlist=>{
let {config, DisplayName, ParentID, ItemID} = vmlist;
if (config == ''){
return {config};
}
else {
return {config, DisplayName, ParentID, ItemID};
}
});
return this;
},
scanVm(scanDir) {
let lists = execSync(`dir /s /b ${scanDir} | find ".vmx" | find /v ".vmxf"`).toString().trim().split('\r\n');
let vmlsObject = {};
lists.forEach(fullDir => {
if (fullDir.indexOf(scanDir + '$RECYCLE.BIN') === 0) return;
let _dir = fullDir.substr(scanDir.length).split(path.sep);
let _file = _dir.pop();
createObjectByArrayKey(_dir, vmlsObject, fullDir);
});
const insertFolderByObject = (obj, parent = 0) => {
Object.keys(obj).forEach(name => {
if (typeof obj[name].config == 'string'){
this.insertVm({
name,
vmxPath: obj[name].config,
parent
});
return
}
let vmlist = this.lists.find(vmlist => vmlist.DisplayName === name);
if (vmlist == null) {
vmlist = this.insertFolder({name, parent});
}
insertFolderByObject(obj[name], vmlist.ItemID);
});
}
insertFolderByObject(vmlsObject, 0);
}
}