Skip to content

Commit 70e3e41

Browse files
author
Mike Pham
committed
feat: adding support for mkdirp and renaming
1 parent a3f7f3f commit 70e3e41

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

‎src/Core/Files.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as child_process from 'child_process'
12
import * as fs from 'fs'
23
import * as path from 'path'
34

@@ -53,19 +54,6 @@ class InternalFileSystem implements FileSystem {
5354
})
5455
}
5556

56-
public readfile(filepath: string): Promise<Buffer> {
57-
return new Promise<Buffer>((resolve, reject) => {
58-
fs.readFile(filepath, (error: NodeJS.ErrnoException, data: Buffer) => {
59-
if (error) {
60-
reject(error)
61-
} else {
62-
this.log.debug('read-file', filepath)
63-
resolve(data)
64-
}
65-
})
66-
})
67-
}
68-
6957
public listdirs(filepath: string): Promise<string[]> {
7058
return this.statfiles(filepath).then(stats => stats.filter(stat => stat.dir).map(stat => stat.filename))
7159
}
@@ -86,6 +74,37 @@ class InternalFileSystem implements FileSystem {
8674
})
8775
}
8876

77+
public mkdirp(filepath: string): Promise<void> {
78+
return new Promise<void>((resolve, reject) =>
79+
child_process.exec(`mkdir -p "${filepath}"`)
80+
.on('close', code => code === 0 ? resolve() : reject())
81+
.on('error', error => reject(error))
82+
)
83+
}
84+
85+
public readfile(filepath: string): Promise<Buffer> {
86+
return new Promise<Buffer>((resolve, reject) => {
87+
fs.readFile(filepath, (error: NodeJS.ErrnoException, data: Buffer) => {
88+
if (error) {
89+
reject(error)
90+
} else {
91+
this.log.debug('read-file', filepath)
92+
resolve(data)
93+
}
94+
})
95+
})
96+
}
97+
98+
public rename(original: string, filepath: string): Promise<void> {
99+
return new Promise<void>((resolve, reject) => fs.rename(original, filepath, (error: NodeJS.ErrnoException) => {
100+
if (error) {
101+
reject(error)
102+
} else {
103+
resolve()
104+
}
105+
}))
106+
}
107+
89108
public save<T>(filepath: string, data: T): Promise<void> {
90109
return this.writefile(filepath, JSON.stringify(data, null, 2))
91110
}

‎src/Core/Interfaces/FileSystem.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ export interface FileSystem {
1111
extensionless(filename: string): string
1212
join(...args: string[]): string
1313
json<T>(filepath: string): Promise<T>
14-
readfile(filepath: string): Promise<Buffer>
1514
listdirs(filepath: string): Promise<string[]>
1615
listfiles(filepath: string): Promise<string[]>
1716
mkdir(filepath: string): Promise<void>
17+
mkdirp(filepath: string): Promise<void>
18+
readfile(filepath: string): Promise<Buffer>
19+
rename(original: string, filepath: string): Promise<void>
1820
save<T>(filepath: string, data: T): Promise<void>
1921
statfile(filepath: string): Promise<fs.Stats>
2022
statfiles(filepath: string): Promise<Stat[]>

0 commit comments

Comments
 (0)