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