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