-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathfileUtils.ts
105 lines (91 loc) · 2.75 KB
/
fileUtils.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
import fs from 'fs'
import path from 'path'
import YAML from 'js-yaml'
import signale from 'signale'
const processPath = (savePath: boolean | string, defaultPath?: string) => {
let filePath = ''
if (savePath === true && defaultPath) {
filePath = defaultPath
} else if (typeof savePath === 'string') {
filePath = path.normalize(savePath)
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(filePath)
}
}
return filePath
}
const getPathExtname = (filePath: string): string => path.extname(filePath)
const fileExists = (filePath: string): boolean => fs.existsSync(filePath)
const isYaml = (filePath: string): boolean => {
const fileExtension = getPathExtname(filePath)
return fileExtension === '.yaml' || fileExtension === '.yml'
}
const parseYamlOrJson = (data: string, isYaml: boolean): Config => {
return isYaml ? YAML.load(data) : JSON.parse(data)
}
const stringifyToYamlOrJson = (data: Config, isYaml: boolean): string => {
return isYaml ? YAML.dump(data) : JSON.stringify(data, null, 2)
}
const readFile = (filePath: string): Buffer => {
try {
return fs.readFileSync(filePath)
} catch (error) {
signale.error(error)
process.exit(1)
}
}
const writeFile = (filePath: string, data: string | Buffer): void => {
try {
fs.writeFileSync(filePath, data)
} catch (error) {
signale.error(error)
process.exit(1)
}
}
const appendFile = (filePath: string, data: string | Buffer): void => {
try {
fs.appendFileSync(filePath, `${data}\n`)
} catch (error) {
signale.error(error)
process.exit(1)
}
}
const createNextNumberedFileName = (filePath: string): string => {
const escapeRegExp = (string: string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
try {
if (!fileExists(filePath)) return filePath
const dir = path.dirname(filePath)
const baseNameWithoutExt = path.basename(filePath, path.extname(filePath))
const ext = getPathExtname(filePath)
const files = fs.readdirSync(dir)
const regex = new RegExp(`${escapeRegExp(baseNameWithoutExt)}\\((\\d+)\\)(${escapeRegExp(ext)})?$`, 'i')
let maxNumber = 0
for (const file of files) {
const match = file.match(regex)
if (match) {
const number = parseInt(match[1], 10)
maxNumber = Math.max(maxNumber, number)
}
}
const newNumber = maxNumber + 1
const newFileName = `${baseNameWithoutExt}(${newNumber})${ext}`
return path.join(dir, newFileName)
} catch (err) {
signale.error(`Error: Unable to create a new numbered file name for path '${filePath}'.`)
process.exit(1)
}
}
export {
processPath,
getPathExtname,
fileExists,
isYaml,
parseYamlOrJson,
stringifyToYamlOrJson,
readFile,
writeFile,
appendFile,
createNextNumberedFileName,
}