-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
24 lines (21 loc) · 1.1 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
const os = require("os");
// to detect on with os user had used path.resolve(...)
const is_posix_os = (os.platform() !== "win32");
const version = os.release();
// For some windows version (Windows 10 v1803), it is not useful to escape spaces in path
// https://docs.microsoft.com/en-us/windows/release-information/
const windows_version_regex = /(\d+\.\d+)\.(\d+)/;
const should_not_escape = (major_release = "", os_build = "") =>
/1\d+\.\d+/.test(major_release) && Number(os_build) >= 17134.1184;
module.exports = function (given_path) {
return (is_posix_os)
// for posix path, escape is simple
? given_path.replace(/(\s+)/g, '\\$1')
// for windows, it depend of the build
: (should_not_escape(...windows_version_regex.exec(version).splice(1)))
// on major version, no need to escape anymore
// https://support.microsoft.com/en-us/help/4467268/url-encoded-unc-paths-not-url-decoded-in-windows-10-version-1803-later
? given_path
// on older version, replace space with symbol %20
: given_path.replace(/(\s+)/g, '%20');
};