-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
293 lines (275 loc) · 10.4 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* @file Entry point for the library. Exposes the external facing function that accepts the input defined in the API documentation.
* @author TheJaredWilcurt
*/
'use strict';
const exec = require('child_process').execSync;
const fs = require('fs');
const path = require('path');
const parseRawData = require('./src/parse-raw-data.js');
/**
* OPTIONAL: console.error is called by default.
*
* Your own custom logging function called with helpful warning/error
* messages from the internal validators.
*
* @typedef {Function} CUSTOMLOGGER
* @callback {Function} CUSTOMLOGGER
* @param {string} message The human readable warning/error message
* @param {object} [error] Sometimes an error or options object is passed
* @return {void}
*/
/**
* @typedef {object} SHORTCUTPROPERITES
* @property {string} FullName 'C:\\Users\\Owner\\Desktop\\DaVinci Resolve.lnk'
* @property {string} Arguments '--foo=bar'
* @property {string} Description 'Video Editor'
* @property {string} Hotkey 'CTRL+SHIFT+F10'
* @property {string} IconLocation 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\ResolveIcon.exe,0'
* @property {string} RelativePath ''
* @property {string} TargetPath 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\Resolve.exe'
* @property {string} WindowStyle '1'
* @property {string} WorkingDirectory 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\'
*/
/**
* A generic function for errors/warnings. It will either call the passed in customLoger,
* or use console.error to notify the user of this library of errors or validation warnings.
*
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @param {string} message A human readable message describing an error/warning
* @param {any} error A programmatic error message or object, may be undefined
*/
function throwError (customLogger, message, error) {
if (typeof(customLogger) === 'function') {
customLogger(message, error);
} else {
console.error(
'________________________________\n' +
'Get-Windows-Shortcut-Properties:\n' +
message,
error
);
}
}
/**
* Generic type validation function. Ensures value passed in is an array
* that contains at least one string, and only strings.
*
* @param {any} arr A value to be validated as an array of strings
* @return {boolean} true = valid
*/
function isArrayOfStrings (arr) {
let isValidArray = false;
if (Array.isArray(arr) && arr.length) {
isValidArray = arr.every(function (item) {
return typeof(item) === 'string';
});
}
return isValidArray;
}
/**
* Generic type validation function. Ensures value passed in is an array
* that contains at least one object, and only objects.
*
* @param {any} arr A value to be validated as an array of objects
* @return {boolean} true = valid
*/
function isArrayOfObjects (arr) {
let isValidArray = false;
if (Array.isArray(arr) && arr.length) {
isValidArray = arr.every(function (item) {
return !Array.isArray(item) && typeof(item) === 'object';
});
}
return isValidArray;
}
/**
* If a customLogger is passed in, ensures it is a valid function.
*
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @return {boolean} True if valid, false if invalid
*/
function customLoggerIsValid (customLogger) {
if (customLogger && typeof(customLogger) !== 'function') {
throwError(customLogger, 'The customLogger must be a function or undefined');
return false;
}
return true;
}
/**
* Validates that shortcutProperties and customLogger are the correct expected types.
*
* @param {SHORTCUTPROPERITES[]} shortcutProperties Array of objects, each representing a successful or failed shortcut property
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @return {boolean} True if valid, false if invalid
*/
function translateInputsAreValid (shortcutProperties, customLogger) {
let valid = true;
valid = customLoggerIsValid(customLogger);
if (!isArrayOfObjects(shortcutProperties)) {
throwError(customLogger, 'The shortcutProperties must be an array of objects');
valid = false;
}
return valid;
}
/**
* Validates that the filePath and customLogger are the correct expected types.
* Validates that this Windows specific library is actually being ran on Windows.
*
* @param {(string|string[])} filePath String or array of strings for the filepaths to shortcut files
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @return {boolean} True if valid, false if invalid
*/
function syncInputsAreValid (filePath, customLogger) {
let valid = true;
valid = customLoggerIsValid(customLogger);
if (process.platform !== 'win32') {
throwError(customLogger, 'Platform is not Windows');
valid = false;
}
if (
!filePath ||
(
Array.isArray(filePath) &&
!isArrayOfStrings(filePath)
) ||
(
!Array.isArray(filePath) &&
typeof(filePath) !== 'string'
)
) {
throwError(customLogger, 'First argument must be a String or Array of strings');
valid = false;
}
return valid;
}
/**
* Normalizes a file path and ensures it exists and ends with .lnk or .url.
* Warns and returns false if filePath does not meet these requirements.
*
* @param {string} filePath Path to a .lnk or .url Windows shortcut file
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @return {string} The normalized full path to a Windows shortcut that is known to exist
*/
function normalizeFile (filePath, customLogger) {
const normalizedFile = path.normalize(path.resolve(filePath));
if (
!filePath ||
typeof(filePath) !== 'string' ||
(
!filePath.endsWith('.lnk') &&
!filePath.endsWith('.url')
) ||
!fs.existsSync(normalizedFile)
) {
throwError(customLogger, 'File path must point to a .lnk or .url file that exists');
return false;
}
return normalizedFile;
}
/**
* Creates strings of PowerShell commands for each filePath to get the file properties.
* Stores strings in a returned Array.
*
* @param {string[]} filePaths Array of strings for the filepaths to shortcut files
* @param {CUSTOMLOGGER} customLogger Optional function to handle logging human readable errors/warnings
* @return {string[]} Array of strings of PowerShell commands to get shortcut properties
*/
function generateCommands (filePaths, customLogger) {
const commands = [];
for (let filePath of filePaths) {
const normalizedFile = normalizeFile(filePath, customLogger);
if (normalizedFile) {
// Escape (') and (’) in the file path for PowerShell syntax
const safeFilePath = normalizedFile
.replace(/'/g, '\'\'')
.replace(/’/g, '’’');
const command = [
'(New-Object -COM WScript.Shell).CreateShortcut(\'',
safeFilePath,
'\');'
].join('');
commands.push(command);
}
}
return commands;
}
module.exports = {
/**
* Retrieves the details of OS based Windows shortcuts.
*
* @example
* const output = getWindowsShortcutProperties.sync([
* '../Sublime Text.lnk',
* 'C:\\Users\\Public\\Desktop\\Firefox.lnk'
* ]);
*
* @param {(string|string[])} filePath String or array of strings for the filepaths to shortcut files
* @param {CUSTOMLOGGER} customLogger Optional function to handle logging human readable errors/warnings
* @return {SHORTCUTPROPERITES[]} Array of objects or undefined, each representing a successful or failed shortcut property
*/
sync: function (filePath, customLogger) {
if (!syncInputsAreValid(filePath, customLogger)) {
return;
}
if (typeof(filePath) === 'string') {
filePath = [filePath];
}
const commands = generateCommands(filePath, customLogger).join('');
if (!commands || !commands.length) {
return;
}
const command = 'powershell.exe -command "' + commands + '"';
try {
const rawData = exec(command);
const parsed = parseRawData(rawData);
return parsed;
} catch (err) {
if (err) {
throwError(customLogger, 'Failed to run powershell command to get shortcut properties', err);
return;
}
}
},
/**
* Translates the official Microsoft shortcut property names to something more human readable and familiar to JavaScript developers.
*
* @param {SHORTCUTPROPERITES[]} shortcutProperties Array of objects, each representing a successful or failed shortcut property
* @param {CUSTOMLOGGER} customLogger User provided function to handle logging human readable errors/warnings
* @return {boolean} True if valid, false if invalid
*/
translate: function (shortcutProperties, customLogger) {
if (!translateInputsAreValid(shortcutProperties, customLogger)) {
return;
}
const windowModes = {
1: 'normal',
3: 'maximized',
7: 'minimized'
};
const translatedProperties = shortcutProperties.map(function (shortcut) {
const translatedShortcut = {
// 'C:\\Users\\Owner\\Desktop\\DaVinci Resolve.lnk',
filePath: shortcut.FullName || '',
// '--foo=bar',
arguments: shortcut.Arguments || '',
// 'Video Editor',
comment: shortcut.Description || '',
// 'CTRL+SHIFT+F10',
hotkey: shortcut.Hotkey || '',
// 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\ResolveIcon.exe,0',
icon: shortcut.IconLocation || '',
// '',
relativePath: shortcut.RelativePath || '',
// 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\Resolve.exe',
targetPath: shortcut.TargetPath || '',
// '1',
windowMode: windowModes[shortcut.WindowStyle] || 'normal',
// 'C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\',
workingDirectory: shortcut.WorkingDirectory || ''
};
return translatedShortcut;
});
return translatedProperties;
}
};