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