microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/node/fileSystem.ts
98lines · 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"; | |
| 5 | import * as Q from "q"; | |
8ab8eac0dlebu10 years ago | 6 | import * as path from "path"; |
3fb37ad5unknown10 years ago | 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 | | |
| 56 | public readFile(filename: string, encoding: string): Q.Promise<string> { | |
| 57 | let contents = Q.defer<string>(); | |
| 58 | | |
| 59 | fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => { | |
| 60 | if (err) { | |
| 61 | contents.reject(err); | |
| 62 | } else { | |
| 63 | contents.resolve(data); | |
| 64 | } | |
| 65 | }); | |
| 66 | | |
| 67 | return contents.promise; | |
| 68 | } | |
4677921cdigeff10 years ago | 69 | |
| 70 | public writeFile(filename: string, data: any): Q.Promise<void> { | |
| 71 | let contents = Q.defer<void>(); | |
| 72 | | |
| 73 | fs.writeFile(filename, data, (err: NodeJS.ErrnoException) => { | |
| 74 | if (err) { | |
| 75 | contents.reject(err); | |
| 76 | } else { | |
| 77 | contents.resolve(null); | |
| 78 | } | |
| 79 | }); | |
| 80 | | |
| 81 | return contents.promise; | |
| 82 | } | |
8ab8eac0dlebu10 years ago | 83 | |
| 84 | public removePathRecursivelySync(p: string): void { | |
| 85 | if (fs.existsSync(p)) { | |
| 86 | let stats = fs.statSync(p); | |
| 87 | if (stats.isDirectory()) { | |
| 88 | let contents = fs.readdirSync(p); | |
| 89 | contents.forEach(childPath => | |
| 90 | this.removePathRecursivelySync(path.join(p, childPath))); | |
| 91 | fs.rmdirSync(p); | |
| 92 | } else { | |
| 93 | /* file */ | |
| 94 | fs.unlinkSync(p); | |
| 95 | } | |
| 96 | } | |
| 97 | } | |
3fb37ad5unknown10 years ago | 98 | } |