-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.ts
130 lines (110 loc) · 3.82 KB
/
io.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// JAUL: io.ts
/** @hidden */
const fs = require("fs")
/** @hidden */
const path = require("path")
/** IO Utilities class. */
class IOUtils {
/**
* Finds the correct path to the file looking first on the (optional) base path
* then the current or running directory, finally the root directory.
* Returns null if file is not found.
* @param filename The filename to be searched
* @param basepath Optional, basepath where to look for the file.
* @returns The full path to the file if one was found, or null if not found.
*/
static getFilePath(filename: string, basepath?: string): string {
const originalFilename = filename.toString()
let hasFile = false
// A basepath was passed? Try there first.
if (basepath) {
filename = path.resolve(basepath, originalFilename)
hasFile = fs.existsSync(filename)
if (hasFile) {
return filename
}
}
// Check if correct full path was passed.
hasFile = fs.existsSync(filename)
if (hasFile) {
return filename
}
// Try running directory.
filename = path.resolve(process.cwd(), originalFilename)
hasFile = fs.existsSync(filename)
/* istanbul ignore if */
if (hasFile) {
return filename
}
// Try application root path.
filename = path.resolve(path.dirname(require.main.filename), originalFilename)
hasFile = fs.existsSync(filename)
if (hasFile) {
return filename
}
// Nothing found, so return null.
return null
}
/**
* Copy the `source` file to the `target`, both must be the full file path.
* @param source The full source file path.
* @param target The full target file path.
*/
static copyFileSync(source: string, target: string): void {
const fileBuffer = fs.readFileSync(source)
fs.writeFileSync(target, fileBuffer)
}
/**
* Make sure the `target` directory exists by recursively iterating through its parents
* and creating the directories.
* @param target The full target path, with or without a trailing slash.
*/
static mkdirRecursive(target: string): void {
let stat
// Check if exists and not a file.
if (fs.existsSync(path.resolve(target))) {
stat = fs.statSync(target)
if (!stat.isDirectory()) {
throw new Error(`Target ${target} is a file.`)
}
return
}
var callback = function(p) {
p = path.resolve(p)
try {
fs.mkdirSync(p)
} catch (ex) {
if (ex.code === "ENOENT") {
callback(path.dirname(p))
callback(p)
} else {
let stat
try {
stat = fs.statSync(p)
} catch (ex1) {
ex1.friendlyMessage = `Can't create directory: ${p}`
throw ex1
}
/* istanbul ignore next */
if (!stat.isDirectory()) {
ex.friendlyMessage = `Target ${p} is a file.`
throw ex
}
}
}
}
callback(target)
}
/**
* Helper to delay async code execution. To be used inside async functions using await.
* @param number - How long to stall the execution for, in milliseconds.
* @returns A promise with a setTimeout for the specified milliseconds.
*/
static sleep(ms: number): Promise<Function> {
return new Promise(function(resolve) {
return setTimeout(resolve, ms)
})
}
}
// Exports...
export = IOUtils