/
index.js
262 lines (248 loc) · 9.92 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const { spawnSync } = require('child_process');
/**
* A scope enumeration of Target Constants
*
* @public
* @const
* @returns {string} Returns `Environment Variable Target (USER or MACHINE)`.
*/
const Target = {
USER: "USER",
MACHINE: "MACHINE"
};
/**
* A enumeration to decide whether to receive values in long form or not
* If this is set to no, all the values will be displayed as is.
* If set to yes then, the values enclosed inside % symbol will take its long form
* for e.g. %userprofile% will be C:\Users\UserName
* for e.g. %systemroot% will be C:\Windows
*
* @public
* @const
* @returns {string} Returns `Expanded Form Value (FULL or SHORT)`.
*/
const ExpandedForm = {
YES: "FULL",
NO: "SHORT"
};
/**
* A enumeration to decide whether to create a new Environment Variable or update an existing one with new value appended to it.
*
* @public
* @const
* @returns {string} Returns `Set Operation Type (NEW or APPEND)`.
*/
const SetOperationType = {
NEW: "NEW",
APPEND: "APPEND"
};
/**
* A enumeration to decide what kind of operation to be performed on Environment Variable Value.
*
* @public
* @const
* @returns {string} Returns string if OPTIMIZED option is selected and returns array if BROKEN or DUPLICATES option is selected.
*/
const ExtOperationType = {
BROKEN: "BROKEN",
DUPLICATES: "DUPLICATES",
OPTIMIZE: "OPTIMIZE"
};
function checkForTargetValidity(options) {
if ('target' in options) {
switch (options.target) {
case Target.USER:
return Target.USER;
case Target.MACHINE:
return Target.MACHINE;
default:
return Target.USER;
}
} else {
return Target.USER;
}
}
function checkForExpandedFormValidity(options) {
if ('expandedForm' in options) {
switch (options.expandedForm) {
case ExpandedForm.YES:
return ExpandedForm.YES;
case ExpandedForm.NO:
return ExpandedForm.NO;
default:
return ExpandedForm.NO;
}
} else {
return ExpandedForm.NO;
}
}
function checkForSetOperationTypeValidity(options) {
if ('setOperationType' in options) {
switch (options.setOperationType) {
case SetOperationType.NEW:
return SetOperationType.NEW;
case SetOperationType.APPEND:
return SetOperationType.APPEND;
default:
return SetOperationType.APPEND;
}
} else {
return SetOperationType.APPEND;
}
}
function checkForExtOperationTypeValidity(options) {
if ('extOperationType' in options) {
switch (options.extOperationType) {
case ExtOperationType.BROKEN:
return ExtOperationType.BROKEN;
case ExtOperationType.DUPLICATES:
return ExtOperationType.DUPLICATES;
case ExtOperationType.OPTIMIZE:
return ExtOperationType.OPTIMIZE;
default:
return ExtOperationType.OPTIMIZE;
}
} else {
return ExtOperationType.OPTIMIZE;
}
}
/**
* The main class which acts as the core of module
* containing 4 static functions for managing Windows Environment Variable
*
* @public
* @class
*/
class Env {
/**
* This function either reads the Environment Variable value by its name
* and if name is not specified it reads all the Environment Variables
* under the specified target (User or System).
*
* @public
* @static
* @function
* @param {Object} [options={}] The Options object
* @param {Object} [options.target=Target.USER] Specifies the target to look into (User or System).
* @param {Object} [options.expandedForm=ExpandedForm.NO] If set to yes then the values enclosed inside % symbol will take its long form for e.g. %userprofile% will be C:\Users\UserName
* @param {Object} [options.name] It can be any name like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
* @return It returns string if name is specified or array if name is not specified
* @return {Array} [array] returns array of JS Objects containing keys like:
* name: Name of the environment variable inside specified target.
* value: Value of the environment variable of that name.
*/
static get(options) {
if (!options) options={};
let target = checkForTargetValidity(options);
let expandedForm = checkForExpandedFormValidity(options);
let command = ('name' in options) ? `${__dirname}\\libs\\get-env.exe ${target} ${expandedForm} "${options.name}"` : `${__dirname}\\libs\\get-env.exe ${target} ${expandedForm}`;
let child = spawnSync("cmd.exe", ["/c", command], { shell: true });
if ('name' in options) {
return child.stdout.toString().replace(/\r\n/g,'');
} else {
let retValue = child.stdout.toString().replace(/\r/g,'');
let array = retValue.split('\n');
array.pop();
let newArray = [];
for (let arr of array) {
let temp = arr.split("=");
newArray.push({
name: temp[0],
value: temp[1]
});
}
return newArray;
}
}
/**
* Requires Admin Privileges (set-env.exe is the program it tries to run)
* This function creates a new Environment Variable or it updates the existing with the passed value.
*
* @public
* @static
* @function
* @param {Object} [options={}] The Options object
* @param {Object} [options.target=Target.USER] Specifies the target to look into (User or System).
* @param {Object} [options.setOperationType=SetOperationType.APPEND] If set to SetOperationType.Append it just adds a new value to the existing value and if set to SetOperationType.NEW, a new Environment Variable is created overwriting the existing one if it exists.
* @param {Object} [options.name] It can be any name like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
* @param {Object} [options.value] The value to be appended or set for the Environment Variable.
* @return {number} the exit code of method execution is returned either 0 (success) or 1 (failure)
*/
static set(options) {
if (!options) options={};
let target = checkForTargetValidity(options);
let setOperationType = checkForSetOperationTypeValidity(options);
let name = options.name;
let value = options.value;
if(!name || !value) throw new Error("Name and value field are mandatory!");
value = value.replace(/%/g, '^%');
if (!value.endsWith(";")) value = value+";";
let child = spawnSync("cmd.exe", ["/c", `${__dirname}\\libs\\set-env.exe ${target} ${setOperationType} "${name}" "${value}"`], { shell:true });
return child.status;
}
/**
* Requires Admin Privileges (del-env.exe is the program it tries to run)
* This function deletes an existing Environment Variable or it deletes a particular value from it.
*
* @public
* @static
* @function
* @param {Object} [options={}] The Options object
* @param {Object} [options.target=Target.USER] Specifies the target to look into (User or System).
* @param {Object} [options.name] It can be any name like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
* @param {Object} [options.value] If the value is specified it deletes only this value from the existing value and if not specified then the entire Environment Variable is deleted
* @return {number} the exit code of method execution is returned either 0 (success) or 1 (failure)
*/
static del(options) {
if (!options) options={};
let target = checkForTargetValidity(options);
let name = options.name;
if(!name) throw new Error("Name field is mandatory!");
let command = `${__dirname}\\libs\\del-env.exe ${target} "${name}"`;
if ('value' in options) {
command = `${__dirname}\\libs\\del-env.exe ${target} "${name}" "${options.value.replace(/%/g, '^%')}"`
}
let child = spawnSync("cmd.exe", ["/c", command], { shell: true });
return child.status;
}
/**
* This function operates on a single Environment Variable at a time.
* It can be used to find:
* broken paths: (the path which no longer exits on your File System)
* duplicate values: duplicate paths are returned
* optimized value: value which removes any redundancies (duplicate values) and broken paths.
*
* @public
* @static
* @function
* @param {Object} [options={}] The Options object
* @param {Object} [options.target=Target.USER] Specifies the target to look into (User or System).
* @param {Object} [options.extOperationType=ExtOperationType.OPTIMIZE] the type of operation you want to perform.
* @param {Object} [options.name] It can be any name like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
* @return It returns string if optimize option is specified.
* @return {Array} [array] broken paths or duplicate paths are returned.
*/
static ext(options) {
if (!options) options={};
let target = checkForTargetValidity(options);
let exOperationType = checkForExtOperationTypeValidity(options);
let name = options.name;
if(!name) throw new Error("Name field is mandatory!");
let child = spawnSync("cmd.exe", ["/c", `${__dirname}\\libs\\ext-env.exe ${target} "${name}" ${exOperationType}`], { shell: true });
if (exOperationType === ExtOperationType.BROKEN || exOperationType === ExtOperationType.DUPLICATES) {
let retValue = child.stdout.toString().replace(/\r/g,'');
let array = retValue.split('\n');
array.pop();
return array;
} else {
return child.stdout.toString();
}
}
}
module.exports = {
Env,
Target,
ExpandedForm,
SetOperationType,
ExtOperationType
};