Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libpath.js
4150 views
1
/**
2
* @license
3
* Copyright 2013 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
addToLibrary({
8
$PATH: {
9
isAbs: (path) => path.charAt(0) === '/',
10
// split a filename into [root, dir, basename, ext], unix version
11
// 'root' is just a slash, or nothing.
12
splitPath: (filename) => {
13
var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
14
return splitPathRe.exec(filename).slice(1);
15
},
16
normalizeArray: (parts, allowAboveRoot) => {
17
// if the path tries to go above the root, `up` ends up > 0
18
var up = 0;
19
for (var i = parts.length - 1; i >= 0; i--) {
20
var last = parts[i];
21
if (last === '.') {
22
parts.splice(i, 1);
23
} else if (last === '..') {
24
parts.splice(i, 1);
25
up++;
26
} else if (up) {
27
parts.splice(i, 1);
28
up--;
29
}
30
}
31
// if the path is allowed to go above the root, restore leading ..s
32
if (allowAboveRoot) {
33
for (; up; up--) {
34
parts.unshift('..');
35
}
36
}
37
return parts;
38
},
39
normalize: (path) => {
40
var isAbsolute = PATH.isAbs(path),
41
trailingSlash = path.slice(-1) === '/';
42
// Normalize the path
43
path = PATH.normalizeArray(path.split('/').filter((p) => !!p), !isAbsolute).join('/');
44
if (!path && !isAbsolute) {
45
path = '.';
46
}
47
if (path && trailingSlash) {
48
path += '/';
49
}
50
return (isAbsolute ? '/' : '') + path;
51
},
52
dirname: (path) => {
53
var result = PATH.splitPath(path),
54
root = result[0],
55
dir = result[1];
56
if (!root && !dir) {
57
// No dirname whatsoever
58
return '.';
59
}
60
if (dir) {
61
// It has a dirname, strip trailing slash
62
dir = dir.slice(0, -1);
63
}
64
return root + dir;
65
},
66
// This differs from node's path.basename in that it returns '/' for '/'
67
// rather than the empty string.
68
basename: (path) => path && path.match(/([^\/]+|\/)\/*$/)[1],
69
join: (...paths) => PATH.normalize(paths.join('/')),
70
join2: (l, r) => PATH.normalize(l + '/' + r),
71
},
72
// The FS-using parts are split out into a separate object, so simple path
73
// usage does not require the FS.
74
$PATH_FS__deps: [
75
'$PATH',
76
'$FS',
77
#if WASMFS
78
// In WasmFS, FS.cwd() is implemented via a call into wasm, so we need to
79
// add a dependency on that.
80
'_wasmfs_get_cwd',
81
#endif
82
],
83
$PATH_FS: {
84
resolve: (...args) => {
85
var resolvedPath = '',
86
resolvedAbsolute = false;
87
for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
88
var path = (i >= 0) ? args[i] : FS.cwd();
89
// Skip empty and invalid entries
90
if (typeof path != 'string') {
91
throw new TypeError('Arguments to path.resolve must be strings');
92
} else if (!path) {
93
return ''; // an invalid portion invalidates the whole thing
94
}
95
resolvedPath = path + '/' + resolvedPath;
96
resolvedAbsolute = PATH.isAbs(path);
97
}
98
// At this point the path should be resolved to a full absolute path, but
99
// handle relative paths to be safe (might happen when process.cwd() fails)
100
resolvedPath = PATH.normalizeArray(resolvedPath.split('/').filter((p) => !!p), !resolvedAbsolute).join('/');
101
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
102
},
103
relative: (from, to) => {
104
from = PATH_FS.resolve(from).slice(1);
105
to = PATH_FS.resolve(to).slice(1);
106
function trim(arr) {
107
var start = 0;
108
for (; start < arr.length; start++) {
109
if (arr[start] !== '') break;
110
}
111
var end = arr.length - 1;
112
for (; end >= 0; end--) {
113
if (arr[end] !== '') break;
114
}
115
if (start > end) return [];
116
return arr.slice(start, end - start + 1);
117
}
118
var fromParts = trim(from.split('/'));
119
var toParts = trim(to.split('/'));
120
var length = Math.min(fromParts.length, toParts.length);
121
var samePartsLength = length;
122
for (var i = 0; i < length; i++) {
123
if (fromParts[i] !== toParts[i]) {
124
samePartsLength = i;
125
break;
126
}
127
}
128
var outputParts = [];
129
for (var i = samePartsLength; i < fromParts.length; i++) {
130
outputParts.push('..');
131
}
132
outputParts = outputParts.concat(toParts.slice(samePartsLength));
133
return outputParts.join('/');
134
}
135
}
136
});
137
138