Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/io/index.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const fs = require('node:fs')
21
const path = require('node:path')
22
const tmp = require('tmp')
23
24
/**
25
* @param {!Function} fn .
26
* @return {!Promise<T>} .
27
* @template T
28
*/
29
function checkedCall(fn) {
30
return new Promise((resolve, reject) => {
31
try {
32
fn((err, value) => {
33
if (err) {
34
reject(err)
35
} else {
36
resolve(value)
37
}
38
})
39
} catch (e) {
40
reject(e)
41
}
42
})
43
}
44
45
/**
46
* Recursively removes a directory and all of its contents. This is equivalent
47
* to {@code rm -rf} on a POSIX system.
48
* @param {string} dirPath Path to the directory to remove.
49
* @return {!Promise} A promise to be resolved when the operation has
50
* completed.
51
*/
52
function rmDir(dirPath) {
53
return new Promise(function (fulfill, reject) {
54
fs.rm(dirPath, { recursive: true, maxRetries: 2 }, function (err) {
55
if (err && err.code === 'ENOENT') {
56
fulfill()
57
} else if (err) {
58
reject(err)
59
}
60
fulfill()
61
})
62
})
63
}
64
65
/**
66
* Copies one file to another.
67
* @param {string} src The source file.
68
* @param {string} dst The destination file.
69
* @return {!Promise<string>} A promise for the copied file's path.
70
*/
71
function copy(src, dst) {
72
return new Promise(function (fulfill, reject) {
73
const rs = fs.createReadStream(src)
74
rs.on('error', reject)
75
76
const ws = fs.createWriteStream(dst)
77
ws.on('error', reject)
78
ws.on('close', () => fulfill(dst))
79
80
rs.pipe(ws)
81
})
82
}
83
84
/**
85
* Recursively copies the contents of one directory to another.
86
* @param {string} src The source directory to copy.
87
* @param {string} dst The directory to copy into.
88
* @param {(RegExp|function(string): boolean)=} opt_exclude An exclusion filter
89
* as either a regex or predicate function. All files matching this filter
90
* will not be copied.
91
* @return {!Promise<string>} A promise for the destination
92
* directory's path once all files have been copied.
93
*/
94
function copyDir(src, dst, opt_exclude) {
95
let predicate = opt_exclude
96
if (opt_exclude && typeof opt_exclude !== 'function') {
97
predicate = function (p) {
98
return !opt_exclude.test(p)
99
}
100
}
101
102
if (!fs.existsSync(dst)) {
103
fs.mkdirSync(dst)
104
}
105
106
let files = fs.readdirSync(src)
107
files = files.map(function (file) {
108
return path.join(src, file)
109
})
110
111
if (predicate) {
112
files = files.filter(/** @type {function(string): boolean} */ (predicate))
113
}
114
115
const results = []
116
files.forEach(function (file) {
117
const stats = fs.statSync(file)
118
const target = path.join(dst, path.basename(file))
119
120
if (stats.isDirectory()) {
121
if (!fs.existsSync(target)) {
122
fs.mkdirSync(target, stats.mode)
123
}
124
results.push(copyDir(file, target, predicate))
125
} else {
126
results.push(copy(file, target))
127
}
128
})
129
130
return Promise.all(results).then(() => dst)
131
}
132
133
/**
134
* Tests if a file path exists.
135
* @param {string} aPath The path to test.
136
* @return {!Promise<boolean>} A promise for whether the file exists.
137
*/
138
function exists(aPath) {
139
return new Promise(function (fulfill, reject) {
140
let type = typeof aPath
141
if (type !== 'string') {
142
reject(TypeError(`expected string path, but got ${type}`))
143
} else {
144
fulfill(fs.existsSync(aPath))
145
}
146
})
147
}
148
149
/**
150
* Calls `stat(2)`.
151
* @param {string} aPath The path to stat.
152
* @return {!Promise<!fs.Stats>} A promise for the file stats.
153
*/
154
function stat(aPath) {
155
return checkedCall((callback) => fs.stat(aPath, callback))
156
}
157
158
/**
159
* Deletes a name from the filesystem and possibly the file it refers to. Has
160
* no effect if the file does not exist.
161
* @param {string} aPath The path to remove.
162
* @return {!Promise} A promise for when the file has been removed.
163
*/
164
function unlink(aPath) {
165
return new Promise(function (fulfill, reject) {
166
const exists = fs.existsSync(aPath)
167
if (exists) {
168
fs.unlink(aPath, function (err) {
169
;(err && reject(err)) || fulfill()
170
})
171
} else {
172
fulfill()
173
}
174
})
175
}
176
177
/**
178
* @return {!Promise<string>} A promise for the path to a temporary directory.
179
* @see https://www.npmjs.org/package/tmp
180
*/
181
function tmpDir() {
182
return checkedCall((callback) => tmp.dir({ unsafeCleanup: true }, callback))
183
}
184
185
/**
186
* @param {{postfix: string}=} opt_options Temporary file options.
187
* @return {!Promise<string>} A promise for the path to a temporary file.
188
* @see https://www.npmjs.org/package/tmp
189
*/
190
function tmpFile(opt_options) {
191
return checkedCall((callback) => {
192
/** check fixed in v > 0.2.1 if
193
* (typeof options === 'function') {
194
* return [{}, options];
195
* }
196
*/
197
tmp.file(opt_options, callback)
198
})
199
}
200
201
/**
202
* Searches the {@code PATH} environment variable for the given file.
203
* @param {string} file The file to locate on the PATH.
204
* @param {boolean=} opt_checkCwd Whether to always start with the search with
205
* the current working directory, regardless of whether it is explicitly
206
* listed on the PATH.
207
* @return {?string} Path to the located file, or {@code null} if it could
208
* not be found.
209
*/
210
function findInPath(file, opt_checkCwd) {
211
const dirs = []
212
if (opt_checkCwd) {
213
dirs.push(process.cwd())
214
}
215
dirs.push.apply(dirs, process.env['PATH'].split(path.delimiter))
216
217
let foundInDir = dirs.find((dir) => {
218
let tmp = path.join(dir, file)
219
try {
220
let stats = fs.statSync(tmp)
221
return stats.isFile() && !stats.isDirectory()
222
/*eslint no-unused-vars: "off"*/
223
} catch (ex) {
224
return false
225
}
226
})
227
228
return foundInDir ? path.join(foundInDir, file) : null
229
}
230
231
/**
232
* Reads the contents of the given file.
233
*
234
* @param {string} aPath Path to the file to read.
235
* @return {!Promise<!Buffer>} A promise that will resolve with a buffer of the
236
* file contents.
237
*/
238
function read(aPath) {
239
return checkedCall((callback) => fs.readFile(aPath, callback))
240
}
241
242
/**
243
* Writes to a file.
244
*
245
* @param {string} aPath Path to the file to write to.
246
* @param {(string|!Buffer)} data The data to write.
247
* @return {!Promise} A promise that will resolve when the operation has
248
* completed.
249
*/
250
function write(aPath, data) {
251
return checkedCall((callback) => fs.writeFile(aPath, data, callback))
252
}
253
254
/**
255
* Creates a directory.
256
*
257
* @param {string} aPath The directory path.
258
* @return {!Promise<string>} A promise that will resolve with the path of the
259
* created directory.
260
*/
261
function mkdir(aPath) {
262
return checkedCall((callback) => {
263
fs.mkdir(aPath, undefined, (err) => {
264
if (err && err.code !== 'EEXIST') {
265
callback(err)
266
} else {
267
callback(null, aPath)
268
}
269
})
270
})
271
}
272
273
/**
274
* Recursively creates a directory and any ancestors that do not yet exist.
275
*
276
* @param {string} dir The directory path to create.
277
* @return {!Promise<string>} A promise that will resolve with the path of the
278
* created directory.
279
*/
280
function mkdirp(dir) {
281
return checkedCall((callback) => {
282
fs.mkdir(dir, undefined, (err) => {
283
if (!err) {
284
callback(null, dir)
285
return
286
}
287
288
switch (err.code) {
289
case 'EEXIST':
290
callback(null, dir)
291
return
292
case 'ENOENT':
293
return mkdirp(path.dirname(dir))
294
.then(() => mkdirp(dir))
295
.then(
296
() => callback(null, dir),
297
(err) => callback(err),
298
)
299
default:
300
callback(err)
301
return
302
}
303
})
304
})
305
}
306
307
/**
308
* Recursively walks a directory, returning a promise that will resolve with
309
* a list of all files/directories seen.
310
*
311
* @param {string} rootPath the directory to walk.
312
* @return {!Promise<!Array<{path: string, dir: boolean}>>} a promise that will
313
* resolve with a list of entries seen. For each entry, the recorded path
314
* will be relative to `rootPath`.
315
*/
316
function walkDir(rootPath) {
317
const seen = []
318
return (function walk(dir) {
319
return checkedCall((callback) => fs.readdir(dir, callback)).then((files) =>
320
Promise.all(
321
files.map((file) => {
322
file = path.join(dir, file)
323
return checkedCall((cb) => fs.stat(file, cb)).then((stats) => {
324
seen.push({
325
path: path.relative(rootPath, file),
326
dir: stats.isDirectory(),
327
})
328
return stats.isDirectory() && walk(file)
329
})
330
}),
331
),
332
)
333
})(rootPath).then(() => seen)
334
}
335
336
// PUBLIC API
337
module.exports = {
338
walkDir,
339
rmDir,
340
mkdirp,
341
mkdir,
342
write,
343
read,
344
findInPath,
345
tmpFile,
346
tmpDir,
347
unlink,
348
copy,
349
copyDir,
350
exists,
351
stat,
352
}
353
354