microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/fileSystem.ts
211lines · 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 | import * as Q from "q"; |
| 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 | |
| 15 | public ensureDirectory(dir: string): Q.Promise<void> { | |
aab2095edigeff10 years ago | 16 | return Q.nfcall(this.fs.stat, dir).then((stat: nodeFs.Stats): void => { |
3fb37ad5unknown10 years ago | 17 | if (stat.isDirectory()) { |
| 18 | return; | |
| 19 | } | |
b3a793eeNisheet Jain10 years ago | 20 | throw new Error(`Expected ${dir} to be a directory`); |
d24fea86Joshua Skelton10 years ago | 21 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
3fb37ad5unknown10 years ago | 22 | if (err && err.code === "ENOENT") { |
aab2095edigeff10 years ago | 23 | return Q.nfcall(this.fs.mkdir, dir); |
3fb37ad5unknown10 years ago | 24 | } |
b3a793eeNisheet Jain10 years ago | 25 | throw err; |
3fb37ad5unknown10 years ago | 26 | }); |
| 27 | } | |
| 28 | | |
| 29 | public ensureFileWithContents(file: string, contents: string): Q.Promise<void> { | |
aab2095edigeff10 years ago | 30 | return Q.nfcall(this.fs.stat, file).then((stat: nodeFs.Stats) => { |
3fb37ad5unknown10 years ago | 31 | if (!stat.isFile()) { |
| 32 | throw new Error(`Expected ${file} to be a file`); | |
| 33 | } | |
1e812de2dlebu10 years ago | 34 | |
| 35 | return this.readFile(file).then(existingContents => { | |
| 36 | if (contents !== existingContents) { | |
| 37 | return this.writeFile(file, contents); | |
| 38 | } | |
| 39 | }); | |
d24fea86Joshua Skelton10 years ago | 40 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
3fb37ad5unknown10 years ago | 41 | if (err && err.code === "ENOENT") { |
aab2095edigeff10 years ago | 42 | return Q.nfcall(this.fs.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 | */ | |
| 51 | public existsSync(filename: string) { | |
3fb37ad5unknown10 years ago | 52 | try { |
aab2095edigeff10 years ago | 53 | this.fs.statSync(filename); |
3fb37ad5unknown10 years ago | 54 | return true; |
| 55 | } catch (error) { | |
| 56 | return false; | |
| 57 | } | |
| 58 | } | |
| 59 | | |
2b1090f9Joshua Skelton10 years ago | 60 | /** |
| 61 | * Helper (asynchronous) function to check if a file or directory exists | |
| 62 | */ | |
| 63 | public exists(filename: string): Q.Promise<boolean> { | |
aab2095edigeff10 years ago | 64 | return Q.nfcall(this.fs.stat, filename) |
b031edc7digeff10 years ago | 65 | .then(function() { |
2b1090f9Joshua Skelton10 years ago | 66 | return Q.resolve(true); |
| 67 | }) | |
| 68 | .catch(function(err) { | |
| 69 | return Q.resolve(false); | |
51769beaJoshua Skelton10 years ago | 70 | }); |
2b1090f9Joshua Skelton10 years ago | 71 | } |
| 72 | | |
db80cd4eJimmy Thomson10 years ago | 73 | /** |
| 74 | * Helper async function to read the contents of a directory | |
| 75 | */ | |
| 76 | public readDir(directory: string): Q.Promise<string[]> { | |
aab2095edigeff10 years ago | 77 | return Q.nfcall<string[]>(this.fs.readdir, directory); |
db80cd4eJimmy Thomson10 years ago | 78 | } |
| 79 | | |
d24fea86Joshua Skelton10 years ago | 80 | /** |
| 81 | * Helper (synchronous) function to create a directory recursively | |
| 82 | */ | |
3d2b4b71Joshua Skelton10 years ago | 83 | public makeDirectoryRecursiveSync(dirPath: string): void { |
d24fea86Joshua Skelton10 years ago | 84 | let parentPath = path.dirname(dirPath); |
| 85 | if (!this.existsSync(parentPath)) { | |
3d2b4b71Joshua Skelton10 years ago | 86 | this.makeDirectoryRecursiveSync(parentPath); |
d24fea86Joshua Skelton10 years ago | 87 | } |
| 88 | | |
aab2095edigeff10 years ago | 89 | this.fs.mkdirSync(dirPath); |
d24fea86Joshua Skelton10 years ago | 90 | } |
| 91 | | |
| 92 | /** | |
| 93 | * Helper function to asynchronously copy a file | |
| 94 | */ | |
2b1090f9Joshua Skelton10 years ago | 95 | public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> { |
d8231ba2Joshua Skelton10 years ago | 96 | let deferred: Q.Deferred<void> = Q.defer<void>(); |
aab2095edigeff10 years ago | 97 | let destFile: nodeFs.WriteStream = this.fs.createWriteStream(to, { encoding: encoding }); |
| 98 | let srcFile: nodeFs.ReadStream = this.fs.createReadStream(from, { encoding: encoding }); | |
d24fea86Joshua Skelton10 years ago | 99 | destFile.on("finish", function(): void { |
2b1090f9Joshua Skelton10 years ago | 100 | deferred.resolve(void 0); |
d24fea86Joshua Skelton10 years ago | 101 | }); |
| 102 | | |
| 103 | destFile.on("error", function(e: Error): void { | |
| 104 | deferred.reject(e); | |
| 105 | }); | |
| 106 | | |
| 107 | srcFile.on("error", function(e: Error): void { | |
| 108 | deferred.reject(e); | |
| 109 | }); | |
| 110 | | |
| 111 | srcFile.pipe(destFile); | |
| 112 | return deferred.promise; | |
| 113 | } | |
| 114 | | |
3fb37ad5unknown10 years ago | 115 | public deleteFileIfExistsSync(filename: string) { |
d24fea86Joshua Skelton10 years ago | 116 | if (this.existsSync(filename)) { |
aab2095edigeff10 years ago | 117 | this.fs.unlinkSync(filename); |
3fb37ad5unknown10 years ago | 118 | } |
| 119 | } | |
| 120 | | |
5d4d4de0digeff10 years ago | 121 | public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> { |
aab2095edigeff10 years ago | 122 | return Q.nfcall<string>(this.fs.readFile, filename, encoding); |
3fb37ad5unknown10 years ago | 123 | } |
| 124 | | |
4677921cdigeff10 years ago | 125 | public writeFile(filename: string, data: any): Q.Promise<void> { |
aab2095edigeff10 years ago | 126 | return Q.nfcall<void>(this.fs.writeFile, filename, data); |
4677921cdigeff10 years ago | 127 | } |
bc7a32ceJimmy Thomson10 years ago | 128 | |
bc96b26bJimmy Thomson10 years ago | 129 | public unlink(filename: string): Q.Promise<void> { |
aab2095edigeff10 years ago | 130 | return Q.nfcall<void>(this.fs.unlink, filename); |
bc96b26bJimmy Thomson10 years ago | 131 | } |
| 132 | | |
afc46a73Jimmy Thomson10 years ago | 133 | public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> { |
aab2095edigeff10 years ago | 134 | return Q.nfcall(this.fs.readdir, folder).then((files: string[]) => { |
bc7a32ceJimmy Thomson10 years ago | 135 | const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`); |
| 136 | if (extFiles.length === 0) { | |
| 137 | throw new Error(`Unable to find any ${extension} files.`); | |
3fb37ad5unknown10 years ago | 138 | } |
afc46a73Jimmy Thomson10 years ago | 139 | return extFiles; |
3fb37ad5unknown10 years ago | 140 | }); |
| 141 | } | |
8ab8eac0dlebu10 years ago | 142 | |
4e7a6f0edlebu10 years ago | 143 | public mkDir(p: string): Q.Promise<void> { |
aab2095edigeff10 years ago | 144 | return Q.nfcall<void>(this.fs.mkdir, p); |
4e7a6f0edlebu10 years ago | 145 | } |
| 146 | | |
e7f5d062Joshua Skelton10 years ago | 147 | /** |
| 148 | * Recursively copy 'source' to 'target' asynchronously | |
| 149 | * | |
| 150 | * @param {string} source Location to copy from | |
| 151 | * @param {string} target Location to copy to | |
| 152 | * @returns {Q.Promise} A promise which is fulfilled when the copy completes, and is rejected on error | |
| 153 | */ | |
| 154 | public copyRecursive(source: string, target: string): Q.Promise<void> { | |
aab2095edigeff10 years ago | 155 | return Q.nfcall<nodeFs.Stats>(this.fs.stat, source).then(stats => { |
e7f5d062Joshua Skelton10 years ago | 156 | if (stats.isDirectory()) { |
| 157 | return this.exists(target).then(exists => { | |
| 158 | if (!exists) { | |
aab2095edigeff10 years ago | 159 | return Q.nfcall<void>(this.fs.mkdir, target); |
e7f5d062Joshua Skelton10 years ago | 160 | } |
| 161 | }) | |
b031edc7digeff10 years ago | 162 | .then(() => { |
aab2095edigeff10 years ago | 163 | return Q.nfcall<string[]>(this.fs.readdir, source); |
b031edc7digeff10 years ago | 164 | }) |
| 165 | .then(contents => { | |
| 166 | Q.all(contents.map((childPath: string): Q.Promise<void> => { | |
| 167 | return this.copyRecursive(path.join(source, childPath), path.join(target, childPath)); | |
| 168 | })); | |
| 169 | }); | |
e7f5d062Joshua Skelton10 years ago | 170 | } else { |
| 171 | return this.copyFile(source, target); | |
| 172 | } | |
| 173 | }); | |
| 174 | } | |
| 175 | | |
4e7a6f0edlebu10 years ago | 176 | public removePathRecursivelyAsync(p: string): Q.Promise<void> { |
e7f5d062Joshua Skelton10 years ago | 177 | return this.exists(p).then(exists => { |
4e7a6f0edlebu10 years ago | 178 | if (exists) { |
aab2095edigeff10 years ago | 179 | return Q.nfcall<nodeFs.Stats>(this.fs.stat, p).then((stats: nodeFs.Stats) => { |
4e7a6f0edlebu10 years ago | 180 | if (stats.isDirectory()) { |
aab2095edigeff10 years ago | 181 | return Q.nfcall<string[]>(this.fs.readdir, p).then((childPaths: string[]) => { |
4e7a6f0edlebu10 years ago | 182 | let result = Q<void>(void 0); |
| 183 | childPaths.forEach(childPath => | |
| 184 | result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath)))); | |
| 185 | return result; | |
| 186 | }).then(() => | |
aab2095edigeff10 years ago | 187 | Q.nfcall<void>(this.fs.rmdir, p)); |
4e7a6f0edlebu10 years ago | 188 | } else { |
| 189 | /* file */ | |
aab2095edigeff10 years ago | 190 | return Q.nfcall<void>(this.fs.unlink, p); |
4e7a6f0edlebu10 years ago | 191 | } |
| 192 | }); | |
| 193 | } | |
| 194 | }); | |
| 195 | } | |
| 196 | | |
8ab8eac0dlebu10 years ago | 197 | public removePathRecursivelySync(p: string): void { |
aab2095edigeff10 years ago | 198 | if (this.fs.existsSync(p)) { |
| 199 | let stats = this.fs.statSync(p); | |
8ab8eac0dlebu10 years ago | 200 | if (stats.isDirectory()) { |
aab2095edigeff10 years ago | 201 | let contents = this.fs.readdirSync(p); |
8ab8eac0dlebu10 years ago | 202 | contents.forEach(childPath => |
| 203 | this.removePathRecursivelySync(path.join(p, childPath))); | |
aab2095edigeff10 years ago | 204 | this.fs.rmdirSync(p); |
8ab8eac0dlebu10 years ago | 205 | } else { |
| 206 | /* file */ | |
aab2095edigeff10 years ago | 207 | this.fs.unlinkSync(p); |
8ab8eac0dlebu10 years ago | 208 | } |
| 209 | } | |
| 210 | } | |
3fb37ad5unknown10 years ago | 211 | } |