microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/node/fileSystem.ts
170lines · modecode
| 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 path from "path"; |
| 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 | } |
| 17 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
| 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. |
| 32 | }, (err: Error & { code?: string }): Q.Promise<any> => { |
| 33 | if (err && err.code === "ENOENT") { |
| 34 | return Q.nfcall(fs.writeFile, file, contents); |
| 35 | } else { |
| 36 | throw err; |
| 37 | } |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Helper function to check if a file or directory exists |
| 43 | */ |
| 44 | public existsSync(filename: string) { |
| 45 | try { |
| 46 | fs.statSync(filename); |
| 47 | return true; |
| 48 | } catch (error) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 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 | |
| 66 | /** |
| 67 | * Helper (synchronous) function to create a directory recursively |
| 68 | */ |
| 69 | public makeDirectoryRecursiveSync(dirPath: string): void { |
| 70 | let parentPath = path.dirname(dirPath); |
| 71 | if (!this.existsSync(parentPath)) { |
| 72 | this.makeDirectoryRecursiveSync(parentPath); |
| 73 | } |
| 74 | |
| 75 | fs.mkdirSync(dirPath); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Helper function to asynchronously copy a file |
| 80 | */ |
| 81 | public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> { |
| 82 | var deferred: Q.Deferred<void> = Q.defer<void>(); |
| 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 { |
| 86 | deferred.resolve(void 0); |
| 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 | public deleteFileIfExistsSync(filename: string) { |
| 102 | if (this.existsSync(filename)) { |
| 103 | fs.unlinkSync(filename); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> { |
| 108 | return Q.nfcall<string>(fs.readFile, filename, encoding); |
| 109 | } |
| 110 | |
| 111 | public writeFile(filename: string, data: any): Q.Promise<void> { |
| 112 | return Q.nfcall<void>(fs.writeFile, filename, data); |
| 113 | } |
| 114 | |
| 115 | public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> { |
| 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.`); |
| 120 | } |
| 121 | return extFiles; |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | public pathExists(p: string): Q.Promise<boolean> { |
| 126 | let deferred = Q.defer<boolean>(); |
| 127 | fs.exists(p, deferred.resolve); |
| 128 | return deferred.promise; |
| 129 | } |
| 130 | |
| 131 | public mkDir(p: string): Q.Promise<void> { |
| 132 | return Q.nfcall<void>(fs.mkdir, p); |
| 133 | } |
| 134 | |
| 135 | public removePathRecursivelyAsync(p: string): Q.Promise<void> { |
| 136 | return this.pathExists(p).then(exists => { |
| 137 | if (exists) { |
| 138 | return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => { |
| 139 | if (stats.isDirectory()) { |
| 140 | return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => { |
| 141 | let result = Q<void>(void 0); |
| 142 | childPaths.forEach(childPath => |
| 143 | result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath)))); |
| 144 | return result; |
| 145 | }).then(() => |
| 146 | Q.nfcall<void>(fs.rmdir, p)); |
| 147 | } else { |
| 148 | /* file */ |
| 149 | return Q.nfcall<void>(fs.unlink, p); |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | public removePathRecursivelySync(p: string): void { |
| 157 | if (fs.existsSync(p)) { |
| 158 | let stats = fs.statSync(p); |
| 159 | if (stats.isDirectory()) { |
| 160 | let contents = fs.readdirSync(p); |
| 161 | contents.forEach(childPath => |
| 162 | this.removePathRecursivelySync(path.join(p, childPath))); |
| 163 | fs.rmdirSync(p); |
| 164 | } else { |
| 165 | /* file */ |
| 166 | fs.unlinkSync(p); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |