microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/node/fileSystem.ts
133lines · 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"; | |
d24fea86Joshua Skelton10 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 | } | |
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); | |
| 63 | }) | |
| 64 | } | |
| 65 | | |
d24fea86Joshua Skelton10 years ago | 66 | /** |
| 67 | * Helper (synchronous) function to create a directory recursively | |
| 68 | */ | |
| 69 | public makeDirectoryRecursive(dirPath: string): void { | |
| 70 | let parentPath = path.dirname(dirPath); | |
| 71 | if (!this.existsSync(parentPath)) { | |
| 72 | this.makeDirectoryRecursive(parentPath); | |
| 73 | } | |
| 74 | | |
| 75 | fs.mkdirSync(dirPath) | |
| 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 | | |
| 101 | /** | |
| 102 | * Helper function to get the target path for the type definition files (to be used for intellisense). | |
| 103 | * Creates the target path if it does not exist already. | |
| 104 | */ | |
| 105 | public getOrCreateTypingsTargetPath(projectRoot: string): string { | |
2b1090f9Joshua Skelton10 years ago | 106 | let targetPath = path.resolve(projectRoot, ".vscode", "typings"); |
| 107 | if (!this.existsSync(targetPath)) { | |
| 108 | this.makeDirectoryRecursive(targetPath); | |
d24fea86Joshua Skelton10 years ago | 109 | } |
| 110 | | |
2b1090f9Joshua Skelton10 years ago | 111 | return targetPath; |
d24fea86Joshua Skelton10 years ago | 112 | } |
| 113 | | |
3fb37ad5unknown10 years ago | 114 | public deleteFileIfExistsSync(filename: string) { |
d24fea86Joshua Skelton10 years ago | 115 | if (this.existsSync(filename)) { |
3fb37ad5unknown10 years ago | 116 | fs.unlinkSync(filename); |
| 117 | } | |
| 118 | } | |
| 119 | | |
| 120 | public readFile(filename: string, encoding: string): Q.Promise<string> { | |
| 121 | let contents = Q.defer<string>(); | |
| 122 | | |
| 123 | fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => { | |
| 124 | if (err) { | |
| 125 | contents.reject(err); | |
| 126 | } else { | |
| 127 | contents.resolve(data); | |
| 128 | } | |
| 129 | }); | |
| 130 | | |
| 131 | return contents.promise; | |
| 132 | } | |
| 133 | } |