microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/fileSystem.ts
182lines · modeblame
3fb37ad5unknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
aab2095edigeff10 years ago | 4 | import * as nodeFs from "fs"; |
bc7a32ceJimmy Thomson10 years ago | 5 | import * as path from "path"; |
4bb0956eRedMickey5 years ago | 6 | import * as mkdirp from "mkdirp"; |
3fb37ad5unknown10 years ago | 7 | |
| 8 | export class FileSystem { | |
aab2095edigeff10 years ago | 9 | private fs: typeof nodeFs; |
| 10 | | |
| 11 | constructor({ fs = nodeFs } = {}) { | |
| 12 | this.fs = fs; | |
| 13 | } | |
3fb37ad5unknown10 years ago | 14 | |
0d77292aJiglioNero4 years ago | 15 | public async ensureDirectory(dir: string): Promise<void> { |
| 16 | try { | |
| 17 | const stat = await this.stat(dir); | |
| 18 | if (!stat.isDirectory()) { | |
34472878RedMickey5 years ago | 19 | throw new Error(`Expected ${dir} to be a directory`); |
0d77292aJiglioNero4 years ago | 20 | } |
| 21 | } catch (err) { | |
| 22 | if (err && err.code === "ENOENT") { | |
| 23 | return this.mkDir(dir); | |
| 24 | } | |
| 25 | throw err; | |
| 26 | } | |
3fb37ad5unknown10 years ago | 27 | } |
| 28 | | |
0d77292aJiglioNero4 years ago | 29 | public async ensureFileWithContents(file: string, contents: string): Promise<void> { |
| 30 | try { | |
| 31 | const stat = await this.stat(file); | |
| 32 | if (stat.isFile()) { | |
| 33 | const existingContents = await this.readFile(file); | |
| 34 | if (contents !== existingContents) { | |
1e812de2dlebu10 years ago | 35 | return this.writeFile(file, contents); |
| 36 | } | |
0d77292aJiglioNero4 years ago | 37 | } else { |
| 38 | throw new Error(`Expected ${file} to be a file`); | |
| 39 | } | |
| 40 | } catch (err) { | |
| 41 | if (err && err.code === "ENOENT") { | |
| 42 | return this.writeFile(file, contents); | |
| 43 | } | |
| 44 | throw err; | |
| 45 | } | |
3fb37ad5unknown10 years ago | 46 | } |
| 47 | | |
d24fea86Joshua Skelton10 years ago | 48 | /** |
| 49 | * Helper function to check if a file or directory exists | |
| 50 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 51 | public existsSync(filename: string): boolean { |
34472878RedMickey5 years ago | 52 | return this.fs.existsSync(filename); |
3fb37ad5unknown10 years ago | 53 | } |
| 54 | | |
2b1090f9Joshua Skelton10 years ago | 55 | /** |
| 56 | * Helper (asynchronous) function to check if a file or directory exists | |
| 57 | */ | |
0d77292aJiglioNero4 years ago | 58 | public async exists(filename: string): Promise<boolean> { |
| 59 | try { | |
| 60 | await this.stat(filename); | |
| 61 | return true; | |
| 62 | } catch (err) { | |
| 63 | return false; | |
| 64 | } | |
2b1090f9Joshua Skelton10 years ago | 65 | } |
| 66 | | |
db80cd4eJimmy Thomson10 years ago | 67 | /** |
| 68 | * Helper async function to read the contents of a directory | |
| 69 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 70 | public readDir(directory: string): Promise<string[]> { |
| 71 | return this.fs.promises.readdir(directory); | |
db80cd4eJimmy Thomson10 years ago | 72 | } |
| 73 | | |
d24fea86Joshua Skelton10 years ago | 74 | /** |
| 75 | * Helper (synchronous) function to create a directory recursively | |
| 76 | */ | |
3d2b4b71Joshua Skelton10 years ago | 77 | public makeDirectoryRecursiveSync(dirPath: string): void { |
09f6024fHeniker4 years ago | 78 | const parentPath = path.dirname(dirPath); |
d24fea86Joshua Skelton10 years ago | 79 | if (!this.existsSync(parentPath)) { |
3d2b4b71Joshua Skelton10 years ago | 80 | this.makeDirectoryRecursiveSync(parentPath); |
d24fea86Joshua Skelton10 years ago | 81 | } |
| 82 | | |
aab2095edigeff10 years ago | 83 | this.fs.mkdirSync(dirPath); |
d24fea86Joshua Skelton10 years ago | 84 | } |
| 85 | | |
| 86 | /** | |
| 87 | * Helper function to asynchronously copy a file | |
| 88 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 89 | public copyFile(from: string, to: string): Promise<void> { |
| 90 | return this.fs.promises.copyFile(from, to); | |
d24fea86Joshua Skelton10 years ago | 91 | } |
| 92 | | |
34472878RedMickey5 years ago | 93 | public deleteFileIfExistsSync(filename: string): void { |
d24fea86Joshua Skelton10 years ago | 94 | if (this.existsSync(filename)) { |
aab2095edigeff10 years ago | 95 | this.fs.unlinkSync(filename); |
3fb37ad5unknown10 years ago | 96 | } |
| 97 | } | |
| 98 | | |
ce5e88eeYuri Skorokhodov5 years ago | 99 | public readFile(filename: string, encoding: string = "utf8"): Promise<string | Buffer> { |
| 100 | return this.fs.promises.readFile(filename, { encoding }); | |
3fb37ad5unknown10 years ago | 101 | } |
| 102 | | |
ce5e88eeYuri Skorokhodov5 years ago | 103 | public writeFile(filename: string, data: any): Promise<void> { |
| 104 | return this.fs.promises.writeFile(filename, data); | |
4677921cdigeff10 years ago | 105 | } |
bc7a32ceJimmy Thomson10 years ago | 106 | |
4bb0956eRedMickey5 years ago | 107 | public static writeFileToFolder(folder: string, basename: string, data: any): Promise<void> { |
| 108 | if (!nodeFs.existsSync(folder)) { | |
| 109 | mkdirp.sync(folder); | |
| 110 | } | |
| 111 | return nodeFs.promises.writeFile(path.join(folder, basename), data); | |
| 112 | } | |
| 113 | | |
ce5e88eeYuri Skorokhodov5 years ago | 114 | public unlink(filename: string): Promise<void> { |
| 115 | return this.fs.promises.unlink(filename); | |
bc96b26bJimmy Thomson10 years ago | 116 | } |
| 117 | | |
ce5e88eeYuri Skorokhodov5 years ago | 118 | public mkDir(p: string): Promise<void> { |
| 119 | return this.fs.promises.mkdir(p); | |
bc6696cbdigeff10 years ago | 120 | } |
| 121 | | |
ce5e88eeYuri Skorokhodov5 years ago | 122 | public stat(fsPath: string): Promise<nodeFs.Stats> { |
| 123 | return this.fs.promises.stat(fsPath); | |
bc6696cbdigeff10 years ago | 124 | } |
| 125 | | |
0d77292aJiglioNero4 years ago | 126 | public async directoryExists(directoryPath: string): Promise<boolean> { |
| 127 | try { | |
| 128 | const stats = await this.stat(directoryPath); | |
| 129 | return stats.isDirectory(); | |
| 130 | } catch (err) { | |
| 131 | if (err.code === "ENOENT") { | |
| 132 | return false; | |
| 133 | } | |
| 134 | throw err; | |
| 135 | } | |
bc6696cbdigeff10 years ago | 136 | } |
| 137 | | |
50241c19digeff10 years ago | 138 | /** |
| 139 | * Delete 'dirPath' if it's an empty folder. If not fail. | |
| 140 | * | |
| 141 | * @param {dirPath} path to the folder | |
| 142 | * @returns {void} Nothing | |
| 143 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 144 | public rmdir(dirPath: string): Promise<void> { |
| 145 | return this.fs.promises.rmdir(dirPath); | |
| 146 | } | |
| 147 | | |
| 148 | public async removePathRecursivelyAsync(p: string): Promise<void> { | |
| 149 | const exists = await this.exists(p); | |
| 150 | if (exists) { | |
| 151 | const stats = await this.stat(p); | |
34472878RedMickey5 years ago | 152 | if (stats.isDirectory()) { |
| 153 | const childPaths = await this.readDir(p); | |
c2331a81RedMickey5 years ago | 154 | await Promise.all( |
| 155 | childPaths.map(childPath => | |
34472878RedMickey5 years ago | 156 | this.removePathRecursivelyAsync(path.join(p, childPath)), |
c2331a81RedMickey5 years ago | 157 | ), |
| 158 | ); | |
| 159 | await this.rmdir(p); | |
34472878RedMickey5 years ago | 160 | } else { |
| 161 | /* file */ | |
| 162 | return this.unlink(p); | |
| 163 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 164 | } |
4e7a6f0edlebu10 years ago | 165 | } |
| 166 | | |
8ab8eac0dlebu10 years ago | 167 | public removePathRecursivelySync(p: string): void { |
aab2095edigeff10 years ago | 168 | if (this.fs.existsSync(p)) { |
09f6024fHeniker4 years ago | 169 | const stats = this.fs.statSync(p); |
8ab8eac0dlebu10 years ago | 170 | if (stats.isDirectory()) { |
09f6024fHeniker4 years ago | 171 | const contents = this.fs.readdirSync(p); |
8ab8eac0dlebu10 years ago | 172 | contents.forEach(childPath => |
34472878RedMickey5 years ago | 173 | this.removePathRecursivelySync(path.join(p, childPath)), |
| 174 | ); | |
aab2095edigeff10 years ago | 175 | this.fs.rmdirSync(p); |
8ab8eac0dlebu10 years ago | 176 | } else { |
| 177 | /* file */ | |
aab2095edigeff10 years ago | 178 | this.fs.unlinkSync(p); |
8ab8eac0dlebu10 years ago | 179 | } |
| 180 | } | |
| 181 | } | |
3fb37ad5unknown10 years ago | 182 | } |