@gesslar/toolkit 0.0.1
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 +20 -0
- package/UNLICENSE.txt +24 -0
- package/package.json +62 -0
- package/src/index.js +11 -0
- package/src/lib/Data.js +533 -0
- package/src/lib/DirectoryObject.js +189 -0
- package/src/lib/File.js +346 -0
- package/src/lib/FileObject.js +226 -0
- package/src/lib/Sass.js +166 -0
- package/src/lib/Term.js +171 -0
- package/src/lib/Type.js +207 -0
- package/src/lib/Valid.js +50 -0
- package/src/types/Data.d.ts +96 -0
- package/src/types/DirectoryObject.d.ts +55 -0
- package/src/types/File.d.ts +76 -0
- package/src/types/FileObject.d.ts +59 -0
- package/src/types/Sass.d.ts +23 -0
- package/src/types/Term.d.ts +15 -0
- package/src/types/Type.d.ts +25 -0
- package/src/types/Valid.d.ts +9 -0
- package/src/types/index.d.ts +14 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file DirectoryObject.js
|
|
3
|
+
* @description Class representing a directory and its metadata, including path
|
|
4
|
+
* resolution and existence checks.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from "node:path"
|
|
8
|
+
import util from "node:util"
|
|
9
|
+
|
|
10
|
+
import File from "./File.js"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* DirectoryObject encapsulates metadata and operations for a directory,
|
|
14
|
+
* including path resolution and existence checks.
|
|
15
|
+
*
|
|
16
|
+
* @property {string} supplied - The supplied directory
|
|
17
|
+
* @property {string} path - The resolved path
|
|
18
|
+
* @property {string} uri - The directory URI
|
|
19
|
+
* @property {string} name - The directory name
|
|
20
|
+
* @property {string} module - The directory name without extension
|
|
21
|
+
* @property {string} extension - The directory extension (usually empty)
|
|
22
|
+
* @property {boolean} isFile - Always false
|
|
23
|
+
* @property {boolean} isDirectory - Always true
|
|
24
|
+
* @property {Promise<boolean>} exists - Whether the directory exists (async)
|
|
25
|
+
*/
|
|
26
|
+
export default class DirectoryObject {
|
|
27
|
+
/**
|
|
28
|
+
* @type {object}
|
|
29
|
+
* @private
|
|
30
|
+
* @property {string|null} supplied - User-supplied path
|
|
31
|
+
* @property {string|null} path - The absolute file path
|
|
32
|
+
* @property {string|null} uri - The file URI
|
|
33
|
+
* @property {string|null} name - The file name
|
|
34
|
+
* @property {string|null} module - The file name without extension
|
|
35
|
+
* @property {string|null} extension - The file extension
|
|
36
|
+
* @property {boolean} isFile - Always false
|
|
37
|
+
* @property {boolean} isDirectory - Always true
|
|
38
|
+
*/
|
|
39
|
+
#meta = Object.seal({
|
|
40
|
+
supplied: null,
|
|
41
|
+
path: null,
|
|
42
|
+
uri: null,
|
|
43
|
+
name: null,
|
|
44
|
+
module: null,
|
|
45
|
+
extension: null,
|
|
46
|
+
isFile: false,
|
|
47
|
+
isDirectory: true,
|
|
48
|
+
directory: null,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Constructs a DirectoryObject instance.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} directory - The directory path
|
|
55
|
+
*/
|
|
56
|
+
constructor(directory) {
|
|
57
|
+
const fixedDir = File.fixSlashes(directory ?? ".")
|
|
58
|
+
const absolutePath = path.resolve(fixedDir)
|
|
59
|
+
const fileUri = File.pathToUri(absolutePath)
|
|
60
|
+
const filePath = File.uriToPath(fileUri)
|
|
61
|
+
const baseName = path.basename(absolutePath) || "."
|
|
62
|
+
|
|
63
|
+
this.#meta.supplied = fixedDir
|
|
64
|
+
this.#meta.path = filePath
|
|
65
|
+
this.#meta.uri = fileUri
|
|
66
|
+
this.#meta.name = baseName
|
|
67
|
+
this.#meta.extension = ""
|
|
68
|
+
this.#meta.module = baseName
|
|
69
|
+
|
|
70
|
+
Object.freeze(this.#meta)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns a string representation of the DirectoryObject.
|
|
75
|
+
*
|
|
76
|
+
* @returns {string} string representation of the DirectoryObject
|
|
77
|
+
*/
|
|
78
|
+
toString() {
|
|
79
|
+
return `[DirectoryObject: ${this.path}]`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Returns a JSON representation of the DirectoryObject.
|
|
84
|
+
*
|
|
85
|
+
* @returns {object} JSON representation of the DirectoryObject
|
|
86
|
+
*/
|
|
87
|
+
toJSON() {
|
|
88
|
+
return {
|
|
89
|
+
supplied: this.supplied,
|
|
90
|
+
path: this.path,
|
|
91
|
+
uri: this.uri,
|
|
92
|
+
name: this.name,
|
|
93
|
+
module: this.module,
|
|
94
|
+
extension: this.extension,
|
|
95
|
+
isFile: this.isFile,
|
|
96
|
+
isDirectory: this.isDirectory
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Custom inspect method for Node.js console.
|
|
102
|
+
*
|
|
103
|
+
* @returns {object} JSON representation of this object.
|
|
104
|
+
*/
|
|
105
|
+
[util.inspect.custom]() {
|
|
106
|
+
return this.toJSON()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Checks if the directory exists (async).
|
|
111
|
+
*
|
|
112
|
+
* @returns {Promise<boolean>} - A Promise that resolves to true or false
|
|
113
|
+
*/
|
|
114
|
+
get exists() {
|
|
115
|
+
return File.directoryExists(this)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Return the path as passed to the constructor.
|
|
120
|
+
*
|
|
121
|
+
* @returns {string} The directory path
|
|
122
|
+
*/
|
|
123
|
+
get supplied() {
|
|
124
|
+
return this.#meta.supplied
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Return the resolved path
|
|
129
|
+
*
|
|
130
|
+
* @returns {string} The directory path
|
|
131
|
+
*/
|
|
132
|
+
get path() {
|
|
133
|
+
return this.#meta.path
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Returns the URI of the current directory.
|
|
138
|
+
*
|
|
139
|
+
* @returns {string} The directory URI
|
|
140
|
+
*/
|
|
141
|
+
get uri() {
|
|
142
|
+
return this.#meta.uri
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns the directory name with extension (if any) without the path.
|
|
147
|
+
*
|
|
148
|
+
* @returns {string} The directory name
|
|
149
|
+
*/
|
|
150
|
+
get name() {
|
|
151
|
+
return this.#meta.name
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the directory name without the path or extension.
|
|
156
|
+
*
|
|
157
|
+
* @returns {string} The directory name without extension
|
|
158
|
+
*/
|
|
159
|
+
get module() {
|
|
160
|
+
return this.#meta.module
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns the directory extension. Will be an empty string if unavailable.
|
|
165
|
+
*
|
|
166
|
+
* @returns {string} The directory extension
|
|
167
|
+
*/
|
|
168
|
+
get extension() {
|
|
169
|
+
return this.#meta.extension
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Returns false. Because this is a directory.
|
|
174
|
+
*
|
|
175
|
+
* @returns {boolean} Always false
|
|
176
|
+
*/
|
|
177
|
+
get isFile() {
|
|
178
|
+
return this.#meta.isFile
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* We're a directory!
|
|
183
|
+
*
|
|
184
|
+
* @returns {boolean} Always true
|
|
185
|
+
*/
|
|
186
|
+
get isDirectory() {
|
|
187
|
+
return this.#meta.isDirectory
|
|
188
|
+
}
|
|
189
|
+
}
|
package/src/lib/File.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file File system utilities for reading, writing, and manipulating files and directories.
|
|
3
|
+
* Provides comprehensive file operations including data file loading (JSON5/YAML),
|
|
4
|
+
* path resolution, and file system navigation with support for both files and directories.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {globby} from "globby"
|
|
8
|
+
import JSON5 from "json5"
|
|
9
|
+
import fs from "node:fs/promises"
|
|
10
|
+
import path from "node:path"
|
|
11
|
+
import url from "node:url"
|
|
12
|
+
import YAML from "yaml"
|
|
13
|
+
|
|
14
|
+
import Sass from "./Sass.js"
|
|
15
|
+
import Data from "./Data.js"
|
|
16
|
+
import DirectoryObject from "./DirectoryObject.js"
|
|
17
|
+
import FileObject from "./FileObject.js"
|
|
18
|
+
import Valid from "./Valid.js"
|
|
19
|
+
|
|
20
|
+
export default class File {
|
|
21
|
+
/**
|
|
22
|
+
* Fix slashes in a path
|
|
23
|
+
*
|
|
24
|
+
* @param {string} pathName - The path to fix
|
|
25
|
+
* @returns {string} The fixed path
|
|
26
|
+
*/
|
|
27
|
+
static fixSlashes(pathName) {
|
|
28
|
+
return pathName.replace(/\\/g, "/")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Convert a path to a URI
|
|
33
|
+
*
|
|
34
|
+
* @param {string} pathName - The path to convert
|
|
35
|
+
* @returns {string} The URI
|
|
36
|
+
*/
|
|
37
|
+
static pathToUri(pathName) {
|
|
38
|
+
try {
|
|
39
|
+
return url.pathToFileURL(pathName).href
|
|
40
|
+
} catch (e) {
|
|
41
|
+
void e // stfu linter
|
|
42
|
+
|
|
43
|
+
return pathName
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Check if a file can be read. Returns true if the file can be read, false
|
|
49
|
+
*
|
|
50
|
+
* @param {FileObject} file - The file map to check
|
|
51
|
+
* @returns {Promise<boolean>} Whether the file can be read
|
|
52
|
+
*/
|
|
53
|
+
static async canReadFile(file) {
|
|
54
|
+
try {
|
|
55
|
+
await fs.access(file.path, fs.constants.R_OK)
|
|
56
|
+
|
|
57
|
+
return true
|
|
58
|
+
} catch (_) {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Check if a file can be written. Returns true if the file can be written,
|
|
65
|
+
*
|
|
66
|
+
* @param {FileObject} file - The file map to check
|
|
67
|
+
* @returns {Promise<boolean>} Whether the file can be written
|
|
68
|
+
*/
|
|
69
|
+
static async canWriteFile(file) {
|
|
70
|
+
try {
|
|
71
|
+
await fs.access(file.path, fs.constants.W_OK)
|
|
72
|
+
|
|
73
|
+
return true
|
|
74
|
+
} catch (_error) {
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Check if a file exists
|
|
81
|
+
*
|
|
82
|
+
* @param {FileObject} file - The file map to check
|
|
83
|
+
* @returns {Promise<boolean>} Whether the file exists
|
|
84
|
+
*/
|
|
85
|
+
static async fileExists(file) {
|
|
86
|
+
try {
|
|
87
|
+
await fs.access(file.path, fs.constants.R_OK)
|
|
88
|
+
|
|
89
|
+
return true
|
|
90
|
+
} catch (_) {
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Determines the size of a file.
|
|
97
|
+
*
|
|
98
|
+
* @param {FileObject} file - The file object to test
|
|
99
|
+
* @returns {Promise<number?>} - The size of the file or null, if it doesn't exist.
|
|
100
|
+
*/
|
|
101
|
+
static async fileSize(file) {
|
|
102
|
+
try {
|
|
103
|
+
const stat = await fs.stat(file.path)
|
|
104
|
+
|
|
105
|
+
return stat.size
|
|
106
|
+
} catch (_) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Gets the last modification time of a file.
|
|
113
|
+
* Used by the caching system to determine if cached data is still valid.
|
|
114
|
+
*
|
|
115
|
+
* @param {FileObject} file - The file object to check
|
|
116
|
+
* @returns {Promise<Date|null>} The last modification time, or null if file doesn't exist
|
|
117
|
+
*/
|
|
118
|
+
static async fileModified(file) {
|
|
119
|
+
try {
|
|
120
|
+
const stat = await fs.stat(file.path)
|
|
121
|
+
|
|
122
|
+
return stat.mtime
|
|
123
|
+
} catch (_) {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Check if a directory exists
|
|
130
|
+
*
|
|
131
|
+
* @param {DirectoryObject} dirObject - The directory map to check
|
|
132
|
+
* @returns {Promise<boolean>} Whether the directory exists
|
|
133
|
+
*/
|
|
134
|
+
static async directoryExists(dirObject) {
|
|
135
|
+
try {
|
|
136
|
+
(await fs.opendir(dirObject.path)).close()
|
|
137
|
+
|
|
138
|
+
return true
|
|
139
|
+
} catch (_) {
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Convert a URI to a path
|
|
146
|
+
*
|
|
147
|
+
* @param {string} pathName - The URI to convert
|
|
148
|
+
* @returns {string} The path
|
|
149
|
+
*/
|
|
150
|
+
static uriToPath(pathName) {
|
|
151
|
+
try {
|
|
152
|
+
return url.fileURLToPath(pathName)
|
|
153
|
+
} catch (_) {
|
|
154
|
+
return pathName
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @typedef {object} FileParts
|
|
160
|
+
* @property {string} base - The file name with extension
|
|
161
|
+
* @property {string} dir - The directory path
|
|
162
|
+
* @property {string} ext - The file extension (including dot)
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Deconstruct a filename into parts
|
|
167
|
+
*
|
|
168
|
+
* @param {string} fileName - The filename to deconstruct
|
|
169
|
+
* @returns {FileParts} The filename parts
|
|
170
|
+
*/
|
|
171
|
+
static deconstructFilenameToParts(fileName) {
|
|
172
|
+
Valid.assert(typeof fileName === "string" && fileName.length > 0,
|
|
173
|
+
"file must be a non-zero length string", 1)
|
|
174
|
+
|
|
175
|
+
return path.parse(fileName)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Retrieve all files matching a specific glob pattern.
|
|
180
|
+
*
|
|
181
|
+
* @param {string|string[]} glob - The glob pattern(s) to search.
|
|
182
|
+
* @returns {Promise<Array<FileObject>>} A promise that resolves to an array of file objects
|
|
183
|
+
* @throws {Sass} If the input is not a string or array of strings.
|
|
184
|
+
* @throws {Sass} If the glob pattern array is empty or for other search failures.
|
|
185
|
+
*/
|
|
186
|
+
static async getFiles(glob) {
|
|
187
|
+
Valid.assert(
|
|
188
|
+
(
|
|
189
|
+
(typeof glob === "string" && glob.length > 0) ||
|
|
190
|
+
(
|
|
191
|
+
Array.isArray(glob) && Data.uniformStringArray(glob) &&
|
|
192
|
+
glob.length > 0
|
|
193
|
+
)
|
|
194
|
+
),
|
|
195
|
+
"glob must be a non-empty string or array of strings.",
|
|
196
|
+
1
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
const globbyArray = (
|
|
200
|
+
typeof glob === "string"
|
|
201
|
+
? glob
|
|
202
|
+
.split("|")
|
|
203
|
+
.map(g => g.trim())
|
|
204
|
+
.filter(Boolean)
|
|
205
|
+
: glob
|
|
206
|
+
).map(g => File.fixSlashes(g))
|
|
207
|
+
|
|
208
|
+
if(
|
|
209
|
+
Array.isArray(globbyArray) &&
|
|
210
|
+
Data.uniformStringArray(globbyArray) &&
|
|
211
|
+
!globbyArray.length
|
|
212
|
+
)
|
|
213
|
+
throw Sass.new(
|
|
214
|
+
`Invalid glob pattern: Array must contain only strings. Got ${JSON.stringify(glob)}`,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
// Use Globby to fetch matching files
|
|
218
|
+
|
|
219
|
+
const filesArray = await globby(globbyArray)
|
|
220
|
+
const files = filesArray.map(file => new FileObject(file))
|
|
221
|
+
|
|
222
|
+
// Flatten the result and remove duplicates
|
|
223
|
+
return files
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Lists the contents of a directory.
|
|
228
|
+
*
|
|
229
|
+
* @param {string} directory - The directory to list.
|
|
230
|
+
* @returns {Promise<{files: Array<FileObject>, directories: Array<DirectoryObject>}>} The files and
|
|
231
|
+
* directories in the directory.
|
|
232
|
+
*/
|
|
233
|
+
static async ls(directory) {
|
|
234
|
+
const found = await fs.readdir(directory, {withFileTypes: true})
|
|
235
|
+
const results = await Promise.all(
|
|
236
|
+
found.map(async dirent => {
|
|
237
|
+
const fullPath = path.join(directory, dirent.name)
|
|
238
|
+
const stat = await fs.stat(fullPath)
|
|
239
|
+
|
|
240
|
+
return {dirent, stat, fullPath}
|
|
241
|
+
}),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
const files = results
|
|
245
|
+
.filter(({stat}) => stat.isFile())
|
|
246
|
+
.map(({fullPath}) => new FileObject(fullPath))
|
|
247
|
+
|
|
248
|
+
const directories = results
|
|
249
|
+
.filter(({stat}) => stat.isDirectory())
|
|
250
|
+
.map(({fullPath}) => new DirectoryObject(fullPath))
|
|
251
|
+
|
|
252
|
+
return {files, directories}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Reads the content of a file asynchronously.
|
|
257
|
+
*
|
|
258
|
+
* @param {FileObject} fileObject - The file map containing the file path
|
|
259
|
+
* @returns {Promise<string>} The file contents
|
|
260
|
+
*/
|
|
261
|
+
static async readFile(fileObject) {
|
|
262
|
+
const filePath = fileObject.path
|
|
263
|
+
|
|
264
|
+
if(!(await fileObject.exists))
|
|
265
|
+
throw Sass.new(`No such file '${filePath}'`)
|
|
266
|
+
|
|
267
|
+
if(!filePath)
|
|
268
|
+
throw Sass.new("No absolute path in file map")
|
|
269
|
+
|
|
270
|
+
return await fs.readFile(filePath, "utf8")
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Writes content to a file synchronously.
|
|
275
|
+
*
|
|
276
|
+
* @param {FileObject} fileObject - The file map containing the file path
|
|
277
|
+
* @param {string} content - The content to write
|
|
278
|
+
*/
|
|
279
|
+
static async writeFile(fileObject, content) {
|
|
280
|
+
if(!fileObject.path)
|
|
281
|
+
throw Sass.new("No absolute path in file")
|
|
282
|
+
|
|
283
|
+
await fs.writeFile(fileObject.path, content, "utf8")
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Loads an object from JSON or YAML provided a fileMap
|
|
288
|
+
*
|
|
289
|
+
* @param {FileObject} fileObject - The FileObj file to load containing
|
|
290
|
+
* JSON or YAML text.
|
|
291
|
+
* @returns {object} The parsed data object.
|
|
292
|
+
*/
|
|
293
|
+
static async loadDataFile(fileObject) {
|
|
294
|
+
const content = await File.readFile(fileObject)
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
return JSON5.parse(content)
|
|
298
|
+
} catch {
|
|
299
|
+
try {
|
|
300
|
+
return YAML.parse(content)
|
|
301
|
+
} catch {
|
|
302
|
+
throw Sass.new(`Content is neither valid JSON nor valid YAML:\n'${fileObject.path}'`)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Ensures a directory exists, creating it if necessary
|
|
309
|
+
*
|
|
310
|
+
* @async
|
|
311
|
+
* @param {DirectoryObject} dirObject - The path or DirMap of the directory to assure exists
|
|
312
|
+
* @param {object} [options] - Any options to pass to mkdir
|
|
313
|
+
* @returns {Promise<boolean>} True if directory exists, false otherwise
|
|
314
|
+
* @throws {Sass} If directory creation fails
|
|
315
|
+
*/
|
|
316
|
+
static async assureDirectory(dirObject, options = {}) {
|
|
317
|
+
if(await dirObject.exists)
|
|
318
|
+
return true
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
await fs.mkdir(dirObject.path, options)
|
|
322
|
+
} catch (e) {
|
|
323
|
+
throw Sass.new(`Unable to create directory '${dirObject.path}': ${e.message}`)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return dirObject.exists
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Computes the relative path from one file or directory to another.
|
|
331
|
+
*
|
|
332
|
+
* If the target is outside the source (i.e., the relative path starts with ".."),
|
|
333
|
+
* returns the absolute path to the target instead.
|
|
334
|
+
*
|
|
335
|
+
* @param {FileObject|DirectoryObject} from - The source file or directory object
|
|
336
|
+
* @param {FileObject|DirectoryObject} to - The target file or directory object
|
|
337
|
+
* @returns {string} The relative path from `from` to `to`, or the absolute path if not reachable
|
|
338
|
+
*/
|
|
339
|
+
static relativeOrAbsolutePath(from, to) {
|
|
340
|
+
const relative = path.relative(from.path, to.path)
|
|
341
|
+
|
|
342
|
+
return relative.startsWith("..")
|
|
343
|
+
? to.path
|
|
344
|
+
: relative
|
|
345
|
+
}
|
|
346
|
+
}
|