-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfs-helper.ts
More file actions
52 lines (44 loc) · 1.31 KB
/
Copy pathfs-helper.ts
File metadata and controls
52 lines (44 loc) · 1.31 KB
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
import { promises as fsp } from 'fs'
import mkdirp from 'mkdirp'
import * as path from 'path'
async function mkdir(dirPath: string): Promise<string> {
return mkdirp(dirPath)
}
export async function writeFile(filePath: string, content: string): Promise<void> {
if (!fsp || !fsp.writeFile) {
throw new Error('[Bulma CSS Vars] requires fs.promises (Node.js v12 or higher)')
}
const dir = path.dirname(filePath)
if (!(await exists(dir))) {
await mkdir(dir)
}
await fsp.writeFile(filePath, content)
}
export async function exists(filePath: string): Promise<boolean> {
if (!fsp || !fsp.access) {
throw new Error('[Bulma CSS Vars] requires fs.promises (Node.js v12 or higher)')
}
try {
await fsp.access(filePath)
return true
} catch (err) {
return false
}
}
export async function fileStartsWith(filePath: string, start: string): Promise<boolean> {
if (!fsp || !fsp.readFile) {
throw new Error('[Bulma CSS Vars] requires fs.promises (Node.js v12 or higher)')
}
try {
const content = (await fsp.readFile(filePath)).toString()
return content.startsWith(start)
} catch (err) {
return false
}
}
export function getAbsoluteFileName(fileName: string, cwd: string): string {
if (!path.isAbsolute(fileName)) {
return path.join(cwd, fileName)
}
return fileName
}