microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/node/fileSystem.ts
124lines · 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"; | |
bc7a32ceJimmy Thomson10 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 | } | |
d24fea86Joshua Skelton10 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. | |
d24fea86Joshua Skelton10 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 | | |
d24fea86Joshua Skelton10 years ago | 41 | /** |
| 42 | * Helper function to check if a file or directory exists | |
| 43 | */ | |
| 44 | public existsSync(filename: string) { | |
3fb37ad5unknown10 years ago | 45 | try { |
d24fea86Joshua Skelton10 years ago | 46 | fs.statSync(filename); |
3fb37ad5unknown10 years ago | 47 | return true; |
| 48 | } catch (error) { | |
| 49 | return false; | |
| 50 | } | |
| 51 | } | |
| 52 | | |
2b1090f9Joshua Skelton10 years ago | 53 | /** |
| 54 | * Helper (asynchronous) function to check if a file or directory exists | |
| 55 | */ | |
| 56 | public exists(filename: string): Q.Promise<boolean> { | |
| 57 | return Q.nfcall(fs.stat, filename) | |
| 58 | .then(function(){ | |
| 59 | return Q.resolve(true); | |
| 60 | }) | |
| 61 | .catch(function(err) { | |
| 62 | return Q.resolve(false); | |
51769beaJoshua Skelton10 years ago | 63 | }); |
2b1090f9Joshua Skelton10 years ago | 64 | } |
| 65 | | |
d24fea86Joshua Skelton10 years ago | 66 | /** |
| 67 | * Helper (synchronous) function to create a directory recursively | |
| 68 | */ | |
3d2b4b71Joshua Skelton10 years ago | 69 | public makeDirectoryRecursiveSync(dirPath: string): void { |
d24fea86Joshua Skelton10 years ago | 70 | let parentPath = path.dirname(dirPath); |
| 71 | if (!this.existsSync(parentPath)) { | |
3d2b4b71Joshua Skelton10 years ago | 72 | this.makeDirectoryRecursiveSync(parentPath); |
d24fea86Joshua Skelton10 years ago | 73 | } |
| 74 | | |
51769beaJoshua Skelton10 years ago | 75 | fs.mkdirSync(dirPath); |
d24fea86Joshua Skelton10 years ago | 76 | } |
| 77 | | |
| 78 | /** | |
| 79 | * Helper function to asynchronously copy a file | |
| 80 | */ | |
2b1090f9Joshua Skelton10 years ago | 81 | public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> { |
| 82 | var deferred: Q.Deferred<void> = Q.defer<void>(); | |
d24fea86Joshua Skelton10 years ago | 83 | var destFile: fs.WriteStream = fs.createWriteStream(to, { encoding: encoding }); |
| 84 | var srcFile: fs.ReadStream = fs.createReadStream(from, { encoding: encoding }); | |
| 85 | destFile.on("finish", function(): void { | |
2b1090f9Joshua Skelton10 years ago | 86 | deferred.resolve(void 0); |
d24fea86Joshua Skelton10 years ago | 87 | }); |
| 88 | | |
| 89 | destFile.on("error", function(e: Error): void { | |
| 90 | deferred.reject(e); | |
| 91 | }); | |
| 92 | | |
| 93 | srcFile.on("error", function(e: Error): void { | |
| 94 | deferred.reject(e); | |
| 95 | }); | |
| 96 | | |
| 97 | srcFile.pipe(destFile); | |
| 98 | return deferred.promise; | |
| 99 | } | |
| 100 | | |
3fb37ad5unknown10 years ago | 101 | public deleteFileIfExistsSync(filename: string) { |
d24fea86Joshua Skelton10 years ago | 102 | if (this.existsSync(filename)) { |
3fb37ad5unknown10 years ago | 103 | fs.unlinkSync(filename); |
| 104 | } | |
| 105 | } | |
| 106 | | |
5d4d4de0digeff10 years ago | 107 | public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> { |
0555a744digeff10 years ago | 108 | return Q.nfcall<string>(fs.readFile, filename, encoding); |
3fb37ad5unknown10 years ago | 109 | } |
| 110 | | |
4677921cdigeff10 years ago | 111 | public writeFile(filename: string, data: any): Q.Promise<void> { |
0555a744digeff10 years ago | 112 | return Q.nfcall<void>(fs.writeFile, filename, data); |
4677921cdigeff10 years ago | 113 | } |
bc7a32ceJimmy Thomson10 years ago | 114 | |
afc46a73Jimmy Thomson10 years ago | 115 | public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> { |
bc7a32ceJimmy Thomson10 years ago | 116 | return Q.nfcall(fs.readdir, folder).then((files: string[]) => { |
| 117 | const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`); | |
| 118 | if (extFiles.length === 0) { | |
| 119 | throw new Error(`Unable to find any ${extension} files.`); | |
3fb37ad5unknown10 years ago | 120 | } |
afc46a73Jimmy Thomson10 years ago | 121 | return extFiles; |
3fb37ad5unknown10 years ago | 122 | }); |
| 123 | } | |
| 124 | } |