microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/node/fileSystem.ts
119lines · 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 | | |
| 4 | import * as fs from "fs"; | |
8ab8eac0dlebu10 years ago | 5 | import * as path from "path"; |
3fb37ad5unknown10 years ago | 6 | import * as Q from "q"; |
| 7 | | |
| 8 | export class FileSystem { | |
| 9 | | |
| 10 | public ensureDirectory(dir: string): Q.Promise<void> { | |
| 11 | return Q.nfcall(fs.stat, dir).then((stat: fs.Stats): void => { | |
| 12 | if (stat.isDirectory()) { | |
| 13 | return; | |
| 14 | } else { | |
| 15 | throw new Error(`Expected ${dir} to be a directory`); | |
| 16 | } | |
8ab8eac0dlebu10 years ago | 17 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
3fb37ad5unknown10 years ago | 18 | if (err && err.code === "ENOENT") { |
| 19 | return Q.nfcall(fs.mkdir, dir); | |
| 20 | } else { | |
| 21 | throw err; | |
| 22 | } | |
| 23 | }); | |
| 24 | } | |
| 25 | | |
| 26 | public ensureFileWithContents(file: string, contents: string): Q.Promise<void> { | |
| 27 | return Q.nfcall(fs.stat, file).then((stat: fs.Stats): void => { | |
| 28 | if (!stat.isFile()) { | |
| 29 | throw new Error(`Expected ${file} to be a file`); | |
| 30 | } | |
| 31 | // The file already exists, assume the contents are good and do not touch it. | |
8ab8eac0dlebu10 years ago | 32 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
3fb37ad5unknown10 years ago | 33 | if (err && err.code === "ENOENT") { |
| 34 | return Q.nfcall(fs.writeFile, file, contents); | |
| 35 | } else { | |
| 36 | throw err; | |
| 37 | } | |
| 38 | }); | |
| 39 | } | |
| 40 | | |
| 41 | public fileExistsSync(filename: string) { | |
| 42 | try { | |
| 43 | fs.lstatSync(filename); | |
| 44 | return true; | |
| 45 | } catch (error) { | |
| 46 | return false; | |
| 47 | } | |
| 48 | } | |
| 49 | | |
| 50 | public deleteFileIfExistsSync(filename: string) { | |
| 51 | if (this.fileExistsSync(filename)) { | |
| 52 | fs.unlinkSync(filename); | |
| 53 | } | |
| 54 | } | |
| 55 | | |
5d4d4de0digeff10 years ago | 56 | public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> { |
0555a744digeff10 years ago | 57 | return Q.nfcall<string>(fs.readFile, filename, encoding); |
3fb37ad5unknown10 years ago | 58 | } |
4677921cdigeff10 years ago | 59 | |
| 60 | public writeFile(filename: string, data: any): Q.Promise<void> { | |
0555a744digeff10 years ago | 61 | return Q.nfcall<void>(fs.writeFile, filename, data); |
4677921cdigeff10 years ago | 62 | } |
| 63 | | |
afc46a73Jimmy Thomson10 years ago | 64 | public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> { |
bc7a32ceJimmy Thomson10 years ago | 65 | return Q.nfcall(fs.readdir, folder).then((files: string[]) => { |
| 66 | const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`); | |
| 67 | if (extFiles.length === 0) { | |
| 68 | throw new Error(`Unable to find any ${extension} files.`); | |
4677921cdigeff10 years ago | 69 | } |
afc46a73Jimmy Thomson10 years ago | 70 | return extFiles; |
4677921cdigeff10 years ago | 71 | }); |
| 72 | } | |
8ab8eac0dlebu10 years ago | 73 | |
4e7a6f0edlebu10 years ago | 74 | public pathExists(p: string): Q.Promise<boolean> { |
| 75 | let deferred = Q.defer<boolean>(); | |
| 76 | fs.exists(p, deferred.resolve); | |
| 77 | return deferred.promise; | |
| 78 | } | |
| 79 | | |
| 80 | public mkDir(p: string): Q.Promise<void> { | |
| 81 | return Q.nfcall<void>(fs.mkdir, p); | |
| 82 | } | |
| 83 | | |
| 84 | public removePathRecursivelyAsync(p: string): Q.Promise<void> { | |
| 85 | return this.pathExists(p).then(exists => { | |
| 86 | if (exists) { | |
| 87 | return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => { | |
| 88 | if (stats.isDirectory()) { | |
| 89 | return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => { | |
| 90 | let result = Q<void>(void 0); | |
| 91 | childPaths.forEach(childPath => | |
| 92 | result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath)))); | |
| 93 | return result; | |
| 94 | }).then(() => | |
| 95 | Q.nfcall<void>(fs.rmdir, p)); | |
| 96 | } else { | |
| 97 | /* file */ | |
| 98 | return Q.nfcall<void>(fs.unlink, p); | |
| 99 | } | |
| 100 | }); | |
| 101 | } | |
| 102 | }); | |
| 103 | } | |
| 104 | | |
8ab8eac0dlebu10 years ago | 105 | public removePathRecursivelySync(p: string): void { |
| 106 | if (fs.existsSync(p)) { | |
| 107 | let stats = fs.statSync(p); | |
| 108 | if (stats.isDirectory()) { | |
| 109 | let contents = fs.readdirSync(p); | |
| 110 | contents.forEach(childPath => | |
| 111 | this.removePathRecursivelySync(path.join(p, childPath))); | |
| 112 | fs.rmdirSync(p); | |
| 113 | } else { | |
| 114 | /* file */ | |
| 115 | fs.unlinkSync(p); | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
3fb37ad5unknown10 years ago | 119 | } |