@gesslar/toolkit 3.12.3 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/package.json +2 -2
- package/src/{index.js → node/index.js} +11 -10
- package/src/{lib → node/lib}/DirectoryObject.js +47 -88
- package/src/{lib → node/lib}/FileObject.js +78 -179
- package/src/{lib/FS.js → node/lib/FileSystem.js} +7 -7
- package/src/{lib → node/lib}/Glog.js +2 -2
- package/src/{lib → node/lib}/Logger.js +1 -1
- package/src/{lib → node/lib}/Sass.js +1 -1
- package/src/{lib → node/lib}/Tantrum.js +1 -1
- package/src/{lib → node/lib}/TempDirectoryObject.js +9 -9
- package/src/{lib → node/lib}/Util.js +1 -1
- package/src/node/lib/VDirectoryObject.js +198 -0
- package/src/node/lib/VFileObject.js +61 -0
- package/src/{lib → node/lib}/Valid.js +11 -5
- package/src/lib/CappedDirectoryObject.js +0 -276
- package/src/types/browser/index.d.ts +0 -13
- package/src/types/browser/index.d.ts.map +0 -1
- package/src/types/browser/lib/Collection.d.ts +0 -248
- package/src/types/browser/lib/Collection.d.ts.map +0 -1
- package/src/types/browser/lib/Data.d.ts +0 -250
- package/src/types/browser/lib/Data.d.ts.map +0 -1
- package/src/types/browser/lib/Disposer.d.ts +0 -33
- package/src/types/browser/lib/Disposer.d.ts.map +0 -1
- package/src/types/browser/lib/HTML.d.ts +0 -40
- package/src/types/browser/lib/HTML.d.ts.map +0 -1
- package/src/types/browser/lib/Notify.d.ts +0 -60
- package/src/types/browser/lib/Notify.d.ts.map +0 -1
- package/src/types/browser/lib/Promised.d.ts +0 -119
- package/src/types/browser/lib/Promised.d.ts.map +0 -1
- package/src/types/browser/lib/Sass.d.ts +0 -63
- package/src/types/browser/lib/Sass.d.ts.map +0 -1
- package/src/types/browser/lib/Tantrum.d.ts +0 -52
- package/src/types/browser/lib/Tantrum.d.ts.map +0 -1
- package/src/types/browser/lib/Time.d.ts +0 -42
- package/src/types/browser/lib/Time.d.ts.map +0 -1
- package/src/types/browser/lib/TypeSpec.d.ts +0 -90
- package/src/types/browser/lib/TypeSpec.d.ts.map +0 -1
- package/src/types/browser/lib/Util.d.ts +0 -62
- package/src/types/browser/lib/Util.d.ts.map +0 -1
- package/src/types/browser/lib/Valid.d.ts +0 -33
- package/src/types/browser/lib/Valid.d.ts.map +0 -1
- package/src/types/browser/lib/vendor/dompurify.esm.d.ts +0 -29
- package/src/types/browser/lib/vendor/dompurify.esm.d.ts.map +0 -1
- package/src/types/index.d.ts +0 -20
- package/src/types/index.d.ts.map +0 -1
- package/src/types/lib/Cache.d.ts +0 -27
- package/src/types/lib/Cache.d.ts.map +0 -1
- package/src/types/lib/CappedDirectoryObject.d.ts +0 -144
- package/src/types/lib/CappedDirectoryObject.d.ts.map +0 -1
- package/src/types/lib/DirectoryObject.d.ts +0 -288
- package/src/types/lib/DirectoryObject.d.ts.map +0 -1
- package/src/types/lib/FS.d.ts +0 -188
- package/src/types/lib/FS.d.ts.map +0 -1
- package/src/types/lib/FileObject.d.ts +0 -245
- package/src/types/lib/FileObject.d.ts.map +0 -1
- package/src/types/lib/Glog.d.ts +0 -228
- package/src/types/lib/Glog.d.ts.map +0 -1
- package/src/types/lib/Logger.d.ts +0 -46
- package/src/types/lib/Logger.d.ts.map +0 -1
- package/src/types/lib/Notify.d.ts +0 -54
- package/src/types/lib/Notify.d.ts.map +0 -1
- package/src/types/lib/Sass.d.ts +0 -9
- package/src/types/lib/Sass.d.ts.map +0 -1
- package/src/types/lib/Tantrum.d.ts +0 -9
- package/src/types/lib/Tantrum.d.ts.map +0 -1
- package/src/types/lib/TempDirectoryObject.d.ts +0 -42
- package/src/types/lib/TempDirectoryObject.d.ts.map +0 -1
- package/src/types/lib/Term.d.ts +0 -127
- package/src/types/lib/Term.d.ts.map +0 -1
- package/src/types/lib/Util.d.ts +0 -100
- package/src/types/lib/Util.d.ts.map +0 -1
- package/src/types/lib/Valid.d.ts +0 -33
- package/src/types/lib/Valid.d.ts.map +0 -1
- /package/src/{lib → node/lib}/Cache.js +0 -0
- /package/src/{lib → node/lib}/Notify.js +0 -0
- /package/src/{lib → node/lib}/Term.js +0 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file VDirectoryObject.js
|
|
3
|
+
* @description Abstract base class for directory objects that are constrained
|
|
4
|
+
* to a specific directory tree (the "cap"). This provides security by ensuring
|
|
5
|
+
* all operations remain within the capped directory hierarchy.
|
|
6
|
+
*
|
|
7
|
+
* This class is not intended to be instantiated directly. Use subclasses like
|
|
8
|
+
* TempDirectoryObject that define specific caps.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import path from "node:path"
|
|
12
|
+
|
|
13
|
+
import DirectoryObject from "./DirectoryObject.js"
|
|
14
|
+
import FileSystem from "./FileSystem.js"
|
|
15
|
+
import Valid from "./Valid.js"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* VDirectoryObject extends DirectoryObject with constraints that ensure
|
|
19
|
+
* all operations are restricted to a specific directory tree (the "cap").
|
|
20
|
+
*
|
|
21
|
+
* All path operations are validated to ensure they remain within the
|
|
22
|
+
* cap directory hierarchy for security.
|
|
23
|
+
*
|
|
24
|
+
* @augments DirectoryObject
|
|
25
|
+
*/
|
|
26
|
+
export default class VDirectoryObject extends DirectoryObject {
|
|
27
|
+
#real
|
|
28
|
+
#cap
|
|
29
|
+
#cappedParentPath
|
|
30
|
+
#cappedParent
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Constructs a VDirectoryObject instance.
|
|
34
|
+
*
|
|
35
|
+
* Without a parent, creates a new cap at the specified real directory location
|
|
36
|
+
* with virtual path "/". With a parent, the path is resolved relative to the
|
|
37
|
+
* parent's virtual path (absolute paths treated as cap-relative).
|
|
38
|
+
*
|
|
39
|
+
* @param {string} [directory="."] - Real directory path when no parent (becomes cap), or relative/absolute virtual path when parent provided
|
|
40
|
+
* @param {VDirectoryObject?} [parent] - Optional parent capped directory
|
|
41
|
+
* @throws {Sass} If parent is provided but not a VDirectoryObject
|
|
42
|
+
* @example
|
|
43
|
+
* // Create new capped directory at current directory
|
|
44
|
+
* const cwd = new VDirectoryObject()
|
|
45
|
+
* // Virtual path: "/", Real path: process.cwd(), Cap: itself
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Create new capped directory
|
|
49
|
+
* const cache = new VDirectoryObject("/home/user/.cache")
|
|
50
|
+
* // Virtual path: "/", Real path: /home/user/.cache, Cap: itself
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Create subdirectory with parent
|
|
54
|
+
* const data = new VDirectoryObject("data", cache)
|
|
55
|
+
* // Virtual path: /data, Real path: /home/user/.cache/data, Cap: cache
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // Virtual absolute path with parent (treated as cap-relative)
|
|
59
|
+
* const config = new VDirectoryObject("/etc/config", cache)
|
|
60
|
+
* // Virtual path: /etc/config, Real path: /home/user/.cache/etc/config, Cap: cache
|
|
61
|
+
*/
|
|
62
|
+
constructor(directory, source=null) {
|
|
63
|
+
Valid.type(source, "Null|VDirectoryObject")
|
|
64
|
+
|
|
65
|
+
directory ||= "."
|
|
66
|
+
|
|
67
|
+
const baseLocalPath = source?.path ?? "/"
|
|
68
|
+
const baseRealPath = source?.real.path ?? directory
|
|
69
|
+
|
|
70
|
+
if(source && directory.startsWith("/"))
|
|
71
|
+
directory = directory.slice(1)
|
|
72
|
+
|
|
73
|
+
// Find out what directory means to the basePath
|
|
74
|
+
const realResolved = FileSystem.resolvePath(baseRealPath, directory)
|
|
75
|
+
const localResolved = source
|
|
76
|
+
? FileSystem.resolvePath(baseLocalPath, directory)
|
|
77
|
+
: path.parse(path.resolve("")).root
|
|
78
|
+
|
|
79
|
+
super(localResolved)
|
|
80
|
+
|
|
81
|
+
this.#real = new DirectoryObject(realResolved)
|
|
82
|
+
this.#cap = source?.cap ?? this
|
|
83
|
+
|
|
84
|
+
if(source) {
|
|
85
|
+
this.#cappedParent = source
|
|
86
|
+
this.#cappedParentPath = source.path
|
|
87
|
+
} else {
|
|
88
|
+
this.#cappedParent = null
|
|
89
|
+
this.#cappedParentPath = null
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Creates a VDirectoryObject from the current working directory.
|
|
95
|
+
* This is useful when working with pnpx or other tools where you need to
|
|
96
|
+
* cap at the project's root directory determined at runtime.
|
|
97
|
+
*
|
|
98
|
+
* @returns {VDirectoryObject} A VDirectoryObject capped at the current working directory
|
|
99
|
+
* @example
|
|
100
|
+
* // When using pnpx or similar tools
|
|
101
|
+
* const projectRoot = VDirectoryObject.fromCwd()
|
|
102
|
+
* const srcDir = projectRoot.getDirectory("src")
|
|
103
|
+
* // srcDir is capped at the project root
|
|
104
|
+
*/
|
|
105
|
+
static fromCwd() {
|
|
106
|
+
return new this(process.cwd())
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Indicates whether this directory is capped (constrained to a specific tree).
|
|
111
|
+
* Always returns true for VDirectoryObject instances.
|
|
112
|
+
*
|
|
113
|
+
* @returns {boolean} True for all VDirectoryObject instances
|
|
114
|
+
* @example
|
|
115
|
+
* const capped = new TempDirectoryObject("myapp")
|
|
116
|
+
* console.log(capped.isVirtual) // true
|
|
117
|
+
*
|
|
118
|
+
* const regular = new DirectoryObject("/tmp")
|
|
119
|
+
* console.log(regular.isVirtual) // false
|
|
120
|
+
*/
|
|
121
|
+
get isVirtual() {
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns the cap (root) of the capped directory tree.
|
|
127
|
+
* For root VDirectoryObject instances, returns itself.
|
|
128
|
+
* For children, returns the inherited cap from the parent chain.
|
|
129
|
+
*
|
|
130
|
+
* @returns {VDirectoryObject} The cap directory object (root of the capped tree)
|
|
131
|
+
* @example
|
|
132
|
+
* const temp = new TempDirectoryObject("myapp")
|
|
133
|
+
* console.log(temp.cap === temp) // true (root is its own cap)
|
|
134
|
+
*
|
|
135
|
+
* const subdir = temp.getDirectory("data")
|
|
136
|
+
* console.log(subdir.cap === temp) // true (child inherits parent's cap)
|
|
137
|
+
*/
|
|
138
|
+
get cap() {
|
|
139
|
+
return this.#cap
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns a plain DirectoryObject representing the actual filesystem location.
|
|
144
|
+
* This provides an "escape hatch" from the capped environment to interact
|
|
145
|
+
* with the real filesystem when needed.
|
|
146
|
+
*
|
|
147
|
+
* @returns {DirectoryObject} Uncapped directory object at the real filesystem path
|
|
148
|
+
* @example
|
|
149
|
+
* const temp = new TempDirectoryObject("myapp")
|
|
150
|
+
* const subdir = temp.getDirectory("data")
|
|
151
|
+
*
|
|
152
|
+
* // Work within the capped environment (virtual paths)
|
|
153
|
+
* console.log(subdir.path) // "/data" (virtual)
|
|
154
|
+
* subdir.getFile("config.json") // Stays within cap
|
|
155
|
+
*
|
|
156
|
+
* // Break out to real filesystem when needed
|
|
157
|
+
* console.log(subdir.real.path) // "/tmp/myapp-ABC123/data" (real)
|
|
158
|
+
* subdir.real.parent // Can traverse outside the cap
|
|
159
|
+
*/
|
|
160
|
+
get real() {
|
|
161
|
+
return this.#real
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Returns the parent directory of this capped directory.
|
|
166
|
+
* Returns null only if this directory is at the cap (the "root" of the capped tree).
|
|
167
|
+
*
|
|
168
|
+
* Note: The returned parent is a VDirectoryObject with the same cap.
|
|
169
|
+
* This maintains the capping behavior throughout the directory hierarchy.
|
|
170
|
+
*
|
|
171
|
+
* @returns {VDirectoryObject|null} Parent directory or null if at cap root
|
|
172
|
+
* @example
|
|
173
|
+
* const capped = new TempDirectoryObject("myapp")
|
|
174
|
+
* const subdir = capped.getDirectory("data")
|
|
175
|
+
* console.log(subdir.parent.path) // Returns parent VDirectoryObject
|
|
176
|
+
* console.log(capped.parent) // null (at cap root)
|
|
177
|
+
*/
|
|
178
|
+
get parent() {
|
|
179
|
+
return this.#cappedParent
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Returns the path of the parent directory.
|
|
184
|
+
* Returns null if this directory is at the cap root (no parent).
|
|
185
|
+
*
|
|
186
|
+
* @returns {string|null} The parent directory path, or null if at cap root
|
|
187
|
+
* @example
|
|
188
|
+
* const temp = new TempDirectoryObject("myapp")
|
|
189
|
+
* console.log(temp.parentPath) // null (at cap root)
|
|
190
|
+
*
|
|
191
|
+
* const subdir = temp.getDirectory("data")
|
|
192
|
+
* console.log(subdir.parentPath) // "/" (parent's virtual path)
|
|
193
|
+
*/
|
|
194
|
+
get parentPath() {
|
|
195
|
+
return this.#cappedParentPath
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file VFileObject.js
|
|
3
|
+
* @description Class representing a virtual file within a capped directory tree.
|
|
4
|
+
* Extends FileObject with virtual path support and real filesystem mapping.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import FileObject from "./FileObject.js"
|
|
8
|
+
import FS from "./FileSystem.js"
|
|
9
|
+
import Valid from "./Valid.js"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* VFileObject extends FileObject with virtual path support, maintaining both
|
|
13
|
+
* a virtual path (relative to cap) and a real filesystem path.
|
|
14
|
+
*
|
|
15
|
+
* Virtual files must have a VDirectoryObject parent and cannot exist independently.
|
|
16
|
+
* All file operations use the real filesystem path while exposing clean virtual paths.
|
|
17
|
+
*
|
|
18
|
+
* @property {string} supplied - User-supplied path
|
|
19
|
+
* @property {string} path - The virtual file path (relative to cap)
|
|
20
|
+
* @property {URL} url - The file URL
|
|
21
|
+
* @property {string} name - The file name
|
|
22
|
+
* @property {string} module - The file name without extension
|
|
23
|
+
* @property {string} extension - The file extension
|
|
24
|
+
* @property {boolean} isFile - Always true for files
|
|
25
|
+
* @property {boolean} isVirtual - Always true for VFileObject instances
|
|
26
|
+
* @property {VDirectoryObject} parent - The parent virtual directory object
|
|
27
|
+
* @property {FileObject} real - The real filesystem FileObject
|
|
28
|
+
* @property {Promise<boolean>} exists - Whether the file exists (async)
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
export default class VFileObject extends FileObject {
|
|
32
|
+
#real
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Constructs a VFileObject instance.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} fileName - The file path
|
|
38
|
+
* @param {VDirectoryObject} parent - The parent virtual directory (required)
|
|
39
|
+
*/
|
|
40
|
+
constructor(fileName, parent) {
|
|
41
|
+
Valid.type(fileName, "String", {allowEmpty: false})
|
|
42
|
+
Valid.type(parent, "VDirectoryObject")
|
|
43
|
+
|
|
44
|
+
super(fileName, parent)
|
|
45
|
+
|
|
46
|
+
const parentRealPath = this.parent.real.path
|
|
47
|
+
const relative = FS.absoluteToRelative(this.path, true)
|
|
48
|
+
const resolved = FS.resolvePath(parentRealPath, relative)
|
|
49
|
+
const {base, dir} = FS.pathParts(resolved)
|
|
50
|
+
|
|
51
|
+
this.#real = new FileObject(base, dir)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get isVirtual() {
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get real() {
|
|
59
|
+
return this.#real
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import Sass from "./Sass.js"
|
|
9
|
-
import Data from "
|
|
10
|
-
import Collection from "
|
|
9
|
+
import Data from "../../browser/lib/Data.js"
|
|
10
|
+
import Collection from "../../browser/lib/Collection.js"
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Validation utility class providing type checking and assertion methods.
|
|
@@ -23,10 +23,14 @@ export default class Valid {
|
|
|
23
23
|
* @param {object} [options] - Additional options for validation.
|
|
24
24
|
*/
|
|
25
25
|
static type(value, type, options) {
|
|
26
|
+
const expected = [type]
|
|
27
|
+
|
|
28
|
+
if(options?.allowEmpty !== true)
|
|
29
|
+
expected.push("[no empty values]")
|
|
30
|
+
|
|
26
31
|
Valid.assert(
|
|
27
32
|
Data.isType(value, type, options),
|
|
28
|
-
`Invalid type. Expected ${
|
|
29
|
-
1,
|
|
33
|
+
`Invalid type. Expected ${expected.join(" ")}, got ${Data.typeOf(value)}`
|
|
30
34
|
)
|
|
31
35
|
}
|
|
32
36
|
|
|
@@ -57,7 +61,9 @@ export default class Valid {
|
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
/**
|
|
60
|
-
* Protects against prototype pollution by checking keys for dangerous
|
|
64
|
+
* Protects against prototype pollution by checking keys for dangerous
|
|
65
|
+
* property names.
|
|
66
|
+
*
|
|
61
67
|
* Throws if any restricted prototype properties are found in the keys array.
|
|
62
68
|
*
|
|
63
69
|
* @param {Array<string>} keys - Array of property keys to validate
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file CappedDirectoryObject.js
|
|
3
|
-
* @description Abstract base class for directory objects that are constrained
|
|
4
|
-
* to a specific directory tree (the "cap"). This provides security by ensuring
|
|
5
|
-
* all operations remain within the capped directory hierarchy.
|
|
6
|
-
*
|
|
7
|
-
* This class is not intended to be instantiated directly. Use subclasses like
|
|
8
|
-
* TempDirectoryObject that define specific caps.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import path from "node:path"
|
|
12
|
-
|
|
13
|
-
import DirectoryObject from "./DirectoryObject.js"
|
|
14
|
-
import FileObject from "./FileObject.js"
|
|
15
|
-
import FS from "./FS.js"
|
|
16
|
-
import Valid from "./Valid.js"
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* CappedDirectoryObject extends DirectoryObject with constraints that ensure
|
|
20
|
-
* all operations are restricted to a specific directory tree (the "cap").
|
|
21
|
-
*
|
|
22
|
-
* All path operations are validated to ensure they remain within the
|
|
23
|
-
* cap directory hierarchy for security.
|
|
24
|
-
*
|
|
25
|
-
* @augments DirectoryObject
|
|
26
|
-
*/
|
|
27
|
-
export default class CappedDirectoryObject extends DirectoryObject {
|
|
28
|
-
#real
|
|
29
|
-
#cap
|
|
30
|
-
#cappedParentPath
|
|
31
|
-
#cappedParent
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Constructs a CappedDirectoryObject instance.
|
|
35
|
-
*
|
|
36
|
-
* Without a parent, the path becomes both the directory location and the cap
|
|
37
|
-
* (virtual root). With a parent, the path is resolved relative to the parent's
|
|
38
|
-
* cap using virtual path semantics (absolute paths treated as cap-relative).
|
|
39
|
-
*
|
|
40
|
-
* @param {string} [directory="."] - Directory path (becomes cap if no parent, else relative to parent's cap, defaults to current directory)
|
|
41
|
-
* @param {CappedDirectoryObject?} [parent] - Optional parent capped directory
|
|
42
|
-
* @throws {Sass} If parent is provided but not a CappedDirectoryObject
|
|
43
|
-
* @throws {Sass} If the resulting path would escape the cap
|
|
44
|
-
* @example
|
|
45
|
-
* // Create new capped directory at current directory
|
|
46
|
-
* const cwd = new CappedDirectoryObject()
|
|
47
|
-
* // path: process.cwd(), cap: process.cwd()
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* // Create new capped directory
|
|
51
|
-
* const cache = new CappedDirectoryObject("/home/user/.cache")
|
|
52
|
-
* // path: /home/user/.cache, cap: /home/user/.cache
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* // Create subdirectory with parent
|
|
56
|
-
* const data = new CappedDirectoryObject("data", cache)
|
|
57
|
-
* // path: /home/user/.cache/data, cap: /home/user/.cache
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* // Virtual absolute path with parent
|
|
61
|
-
* const config = new CappedDirectoryObject("/etc/config", cache)
|
|
62
|
-
* // path: /home/user/.cache/etc/config, cap: /home/user/.cache
|
|
63
|
-
*/
|
|
64
|
-
constructor(directory, source=null) {
|
|
65
|
-
Valid.type(source, "Null|CappedDirectoryObject")
|
|
66
|
-
|
|
67
|
-
directory ||= "."
|
|
68
|
-
|
|
69
|
-
const baseLocalPath = source?.path ?? "/"
|
|
70
|
-
const baseRealPath = source?.real.path ?? directory
|
|
71
|
-
|
|
72
|
-
if(source && directory.startsWith("/"))
|
|
73
|
-
directory = directory.slice(1)
|
|
74
|
-
|
|
75
|
-
// Find out what directory means to the basePath
|
|
76
|
-
const realResolved = FS.resolvePath(baseRealPath, directory)
|
|
77
|
-
const localResolved = source
|
|
78
|
-
? FS.resolvePath(baseLocalPath, directory)
|
|
79
|
-
: path.parse(path.resolve("")).root
|
|
80
|
-
|
|
81
|
-
super(localResolved)
|
|
82
|
-
|
|
83
|
-
this.#real = new DirectoryObject(realResolved)
|
|
84
|
-
this.#cap = source?.cap ?? this
|
|
85
|
-
|
|
86
|
-
if(source) {
|
|
87
|
-
this.#cappedParent = source
|
|
88
|
-
this.#cappedParentPath = source.path
|
|
89
|
-
} else {
|
|
90
|
-
this.#cappedParent = null
|
|
91
|
-
this.#cappedParentPath = null
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Creates a CappedDirectoryObject from the current working directory.
|
|
97
|
-
* This is useful when working with pnpx or other tools where you need to
|
|
98
|
-
* cap at the project's root directory determined at runtime.
|
|
99
|
-
*
|
|
100
|
-
* @returns {CappedDirectoryObject} A CappedDirectoryObject capped at the current working directory
|
|
101
|
-
* @example
|
|
102
|
-
* // When using pnpx or similar tools
|
|
103
|
-
* const projectRoot = CappedDirectoryObject.fromCwd()
|
|
104
|
-
* const srcDir = projectRoot.getDirectory("src")
|
|
105
|
-
* // srcDir is capped at the project root
|
|
106
|
-
*/
|
|
107
|
-
static fromCwd() {
|
|
108
|
-
return new this(process.cwd())
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Indicates whether this directory is capped (constrained to a specific tree).
|
|
113
|
-
* Always returns true for CappedDirectoryObject instances.
|
|
114
|
-
*
|
|
115
|
-
* @returns {boolean} True for all CappedDirectoryObject instances
|
|
116
|
-
* @example
|
|
117
|
-
* const capped = new TempDirectoryObject("myapp")
|
|
118
|
-
* console.log(capped.isCapped) // true
|
|
119
|
-
*
|
|
120
|
-
* const regular = new DirectoryObject("/tmp")
|
|
121
|
-
* console.log(regular.isCapped) // false
|
|
122
|
-
*/
|
|
123
|
-
get isCapped() {
|
|
124
|
-
return true
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns the cap (root) of the capped directory tree.
|
|
129
|
-
* For root CappedDirectoryObject instances, returns itself.
|
|
130
|
-
* For children, returns the inherited cap from the parent chain.
|
|
131
|
-
*
|
|
132
|
-
* @returns {CappedDirectoryObject} The cap directory object (root of the capped tree)
|
|
133
|
-
* @example
|
|
134
|
-
* const temp = new TempDirectoryObject("myapp")
|
|
135
|
-
* console.log(temp.cap === temp) // true (root is its own cap)
|
|
136
|
-
*
|
|
137
|
-
* const subdir = temp.getDirectory("data")
|
|
138
|
-
* console.log(subdir.cap === temp) // true (child inherits parent's cap)
|
|
139
|
-
*/
|
|
140
|
-
get cap() {
|
|
141
|
-
return this.#cap
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Returns a plain DirectoryObject representing the actual filesystem location.
|
|
146
|
-
* This provides an "escape hatch" from the capped environment to interact
|
|
147
|
-
* with the real filesystem when needed.
|
|
148
|
-
*
|
|
149
|
-
* @returns {DirectoryObject} Uncapped directory object at the real filesystem path
|
|
150
|
-
* @example
|
|
151
|
-
* const temp = new TempDirectoryObject("myapp")
|
|
152
|
-
* const subdir = temp.getDirectory("data")
|
|
153
|
-
*
|
|
154
|
-
* // Work within the capped environment (virtual paths)
|
|
155
|
-
* console.log(subdir.path) // "/data" (virtual)
|
|
156
|
-
* subdir.getFile("config.json") // Stays within cap
|
|
157
|
-
*
|
|
158
|
-
* // Break out to real filesystem when needed
|
|
159
|
-
* console.log(subdir.real.path) // "/tmp/myapp-ABC123/data" (real)
|
|
160
|
-
* subdir.real.parent // Can traverse outside the cap
|
|
161
|
-
*/
|
|
162
|
-
get real() {
|
|
163
|
-
return this.#real
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Returns the parent directory of this capped directory.
|
|
168
|
-
* Returns null only if this directory is at the cap (the "root" of the capped tree).
|
|
169
|
-
*
|
|
170
|
-
* Note: The returned parent is a CappedDirectoryObject with the same cap.
|
|
171
|
-
* This maintains the capping behavior throughout the directory hierarchy.
|
|
172
|
-
*
|
|
173
|
-
* @returns {CappedDirectoryObject|null} Parent directory or null if at cap root
|
|
174
|
-
* @example
|
|
175
|
-
* const capped = new TempDirectoryObject("myapp")
|
|
176
|
-
* const subdir = capped.getDirectory("data")
|
|
177
|
-
* console.log(subdir.parent.path) // Returns parent CappedDirectoryObject
|
|
178
|
-
* console.log(capped.parent) // null (at cap root)
|
|
179
|
-
*/
|
|
180
|
-
get parent() {
|
|
181
|
-
return this.#cappedParent
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Returns the path of the parent directory.
|
|
186
|
-
* Returns null if this directory is at the cap root (no parent).
|
|
187
|
-
*
|
|
188
|
-
* @returns {string|null} The parent directory path, or null if at cap root
|
|
189
|
-
* @example
|
|
190
|
-
* const temp = new TempDirectoryObject("myapp")
|
|
191
|
-
* console.log(temp.parentPath) // null (at cap root)
|
|
192
|
-
*
|
|
193
|
-
* const subdir = temp.getDirectory("data")
|
|
194
|
-
* console.log(subdir.parentPath) // "/data" or similar (parent's virtual path)
|
|
195
|
-
*/
|
|
196
|
-
get parentPath() {
|
|
197
|
-
return this.#cappedParentPath
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Returns a JSON representation of the DirectoryObject.
|
|
202
|
-
*
|
|
203
|
-
* @returns {object} JSON representation of the DirectoryObject
|
|
204
|
-
*/
|
|
205
|
-
toJSON() {
|
|
206
|
-
return {
|
|
207
|
-
supplied: this.supplied,
|
|
208
|
-
path: this.path,
|
|
209
|
-
url: this.url.toString(),
|
|
210
|
-
name: this.name,
|
|
211
|
-
module: this.module,
|
|
212
|
-
extension: this.extension,
|
|
213
|
-
isFile: this.isFile,
|
|
214
|
-
isDirectory: this.isDirectory,
|
|
215
|
-
parent: this.parent,
|
|
216
|
-
parentPath: this.parentPath,
|
|
217
|
-
capped: this.isCapped,
|
|
218
|
-
cap: this.cap,
|
|
219
|
-
real: this.real
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Override exists to use real filesystem path.
|
|
225
|
-
*
|
|
226
|
-
* @returns {Promise<boolean>} Whether the directory exists
|
|
227
|
-
*/
|
|
228
|
-
get exists() {
|
|
229
|
-
return this.real.exists
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Override read to use real filesystem path and return capped objects.
|
|
234
|
-
*
|
|
235
|
-
* @param {string} [pat=""] - Optional glob pattern
|
|
236
|
-
* @returns {Promise<{files: Array<FileObject>, directories: Array}>} Directory contents
|
|
237
|
-
*/
|
|
238
|
-
async read(...arg) {
|
|
239
|
-
const {files, directories} = await this.real.read(...arg)
|
|
240
|
-
|
|
241
|
-
// we need to re-cast
|
|
242
|
-
const recastDirs = directories.map(e => this.getDirectory(e.name))
|
|
243
|
-
const recastFiles = files.map(f => new FileObject(f.name, this))
|
|
244
|
-
|
|
245
|
-
return {files: recastFiles, directories: recastDirs}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Override hasDirectory to use real filesystem path.
|
|
250
|
-
*
|
|
251
|
-
* @param {string} dirname - Directory name to check
|
|
252
|
-
* @returns {Promise<boolean>} True if directory exists
|
|
253
|
-
*/
|
|
254
|
-
async hasDirectory(dirname) {
|
|
255
|
-
return await this.real.hasDirectory(dirname)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Override assureExists to use real filesystem path.
|
|
260
|
-
*
|
|
261
|
-
* @param {object} [options] - Options for mkdir
|
|
262
|
-
* @returns {Promise<void>}
|
|
263
|
-
*/
|
|
264
|
-
async assureExists(options = {}) {
|
|
265
|
-
return await this.real.assureExists(options)
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Override delete to use real filesystem path.
|
|
270
|
-
*
|
|
271
|
-
* @returns {Promise<void>}
|
|
272
|
-
*/
|
|
273
|
-
async delete() {
|
|
274
|
-
return await this.real.delete()
|
|
275
|
-
}
|
|
276
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { default as Collection } from "./lib/Collection.js";
|
|
2
|
-
export { default as Data } from "./lib/Data.js";
|
|
3
|
-
export { default as Notify } from "./lib/Notify.js";
|
|
4
|
-
export { default as Promised } from "./lib/Promised.js";
|
|
5
|
-
export { default as Sass } from "./lib/Sass.js";
|
|
6
|
-
export { default as Tantrum } from "./lib/Tantrum.js";
|
|
7
|
-
export { default as Time } from "./lib/Time.js";
|
|
8
|
-
export { default as Type } from "./lib/TypeSpec.js";
|
|
9
|
-
export { default as Util } from "./lib/Util.js";
|
|
10
|
-
export { default as Valid } from "./lib/Valid.js";
|
|
11
|
-
export { default as Disposer, Disposer as DisposerClass } from "./lib/Disposer.js";
|
|
12
|
-
export { default as HTML, HTML as HTMLClass } from "./lib/HTML.js";
|
|
13
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../browser/index.js"],"names":[],"mappings":""}
|