-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
177 lines (150 loc) · 4.93 KB
/
Copy pathutils.js
File metadata and controls
177 lines (150 loc) · 4.93 KB
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
// --------------------------------------------------------------------------- //
import { parse } from 'path';
import { lstatSync } from 'fs';
// --------------------------------------------------------------------------- //
/**
* Returns true if path leads to a file.
*
* Includes files without extensions.
* Touches the file system and needs error handling.
*
* @function isFile
* @param {string} _path - The path to be checked.
* @return {boolean} True if path is a file, false if it is a directory.
*/
const isFile = _path => lstatSync(_path).isFile();
// re-write to distinguish error from folder
// } catch (err) {
// // lstatSync throws an error if path doesn't exist
// console.error('isFile(): path does not exist', err);
// // should prob not return false.. (indicates _path is a folder)
// throw Error(err);
// }
// };
// --------------------------------------------------------------------------- //
/**
* Returns true if value exists and is a string.
* *
* @function isString
* @param {string} _path - The value to be checked.
* @return {boolean} True if string, false if not.
*/
const isString = _path => typeof _path === 'string' || _path instanceof String;
// --------------------------------------------------------------------------- //
/**
* Returns true if value exists and is a cased letter.
* Cleverly checks if both-cases of a char are different.
* From: https://stackoverflow.com/a/32567789
*
* @function isLetter
* @param {string} letter - The value to be checked.
* @return {boolean} True if a cased letter, false if not.
*/
const isLetter = letter => letter.toLowerCase() != letter.toUpperCase();
// --------------------------------------------------------------------------- //
/**
* Returns true if path root contains the operating system.
*
* Root may be represented as '/' or 'C:\'.
*
* @function inOS
* @param {string} _path - The path to be checked.
* @return {boolean} True if path is in OS, false if not.
*/
const inOS = _path => {
const osRoot = '/';
const pathRoot = parse(_path).root;
try {
return pathRoot && pathRoot[0] === osRoot;
} catch (err) {
console.error('inOS error:', err);
// should prob not return false.. (indicates _path is not in OS)
return false;
}
};
// --------------------------------------------------------------------------- //
/**
* Checks if path contains windows system directories.
*
* Assumes input to be a POSIX path on the operating system drive.
*
* @function isSystemPath
* @param {string} _path - The path to be checked.
* @return {boolean} Returns true for system paths, false if not.
*
* */
const isSystemPath = _path => {
// could shift this entire section to glob options...
// ...but then couldn't pass in cwd
// this function expects cleaned input from --path and process.cwd()
// would only be called if path is on C:\ drive
// requires inOS() = true and cleanSysRoot() and posixPath()
// console.log('isSystemPath _path:', _path);
// added trim() to catch oddities with whitespace
if (_path.trim() === '/') {
console.error('altering files from root level of system is forbidden');
return true;
}
// forbidden path segments
const forbiddenPaths = [
'/jim/Application Data',
'/jim/AppData',
'/jim/Local Settings'
];
for (const forbiddenPath of forbiddenPaths) {
if (_path.includes(forbiddenPath)) {
console.error(
`altering files inside '${forbiddenPath}' system path is forbidden`
);
return true;
}
}
// forbidden root paths
const forbiddenRootPaths = [
'/Windows',
'/ProgramData',
'/Program Files',
'/Program Files (x86)',
'/Recovery',
'/PerfLogs',
'/$Recycle.Bin',
'/$RECYCLE.BIN',
'/Documents and Settings',
'/System Volume Information'
];
for (const forbiddenRootPath of forbiddenRootPaths) {
if (_path.startsWith(forbiddenRootPath)) {
console.error(
`altering files inside '${forbiddenRootPath}' root system path is forbidden`
);
return true;
}
}
// is not a root system path
return false;
};
// --------------------------------------------------------------------------- //
/**
* Returns true if path is safe to edit.
*
* If not in OS or if in OS but not in a system path
*
* Stops editing of windows operating system files.
* Allows editing across different letter drives.
* Wraps other functions from this module in conditional logic.
*
* @function isSafeWinPath
* @param {string} _path - The path to be checked.
* @return {boolean} True if path is safe, false if not.
* */
const isSafeWinPath = _path =>
!inOS(_path) || (inOS(_path) && !isSystemPath(_path));
// --------------------------------------------------------------------------- //
/**
* Windows filesystem utility functions.
*
* @module Utils
*
* */
export { isFile, isString, isLetter, inOS, isSystemPath, isSafeWinPath };
// --------------------------------------------------------------------------- //