microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c8282f33fd286b46cb719722454ad2549b464f9f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

199lines · 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
4import * as fs from "fs";
5import * as path from "path";
6import * as Q from "q";
7
8export 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 }
15 throw new Error(`Expected ${dir} to be a directory`);
16 }, (err: Error & { code?: string }): Q.Promise<any> => {
17 if (err && err.code === "ENOENT") {
18 return Q.nfcall(fs.mkdir, dir);
19 }
20 throw err;
21 });
22 }
23
24 public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
25 return Q.nfcall(fs.stat, file).then((stat: fs.Stats) => {
26 if (!stat.isFile()) {
27 throw new Error(`Expected ${file} to be a file`);
28 }
29
30 return this.readFile(file).then(existingContents => {
31 if (contents !== existingContents) {
32 return this.writeFile(file, contents);
33 }
34 });
35 }, (err: Error & { code?: string }): Q.Promise<any> => {
36 if (err && err.code === "ENOENT") {
37 return Q.nfcall(fs.writeFile, file, contents);
38 }
39 throw err;
40 });
41 }
42
43 /**
44 * Helper function to check if a file or directory exists
45 */
46 public existsSync(filename: string) {
47 try {
48 fs.statSync(filename);
49 return true;
50 } catch (error) {
51 return false;
52 }
53 }
54
55 /**
56 * Helper (asynchronous) function to check if a file or directory exists
57 */
58 public exists(filename: string): Q.Promise<boolean> {
59 return Q.nfcall(fs.stat, filename)
60 .then(function() {
61 return Q.resolve(true);
62 })
63 .catch(function(err) {
64 return Q.resolve(false);
65 });
66 }
67
68 /**
69 * Helper (synchronous) function to create a directory recursively
70 */
71 public makeDirectoryRecursiveSync(dirPath: string): void {
72 let parentPath = path.dirname(dirPath);
73 if (!this.existsSync(parentPath)) {
74 this.makeDirectoryRecursiveSync(parentPath);
75 }
76
77 fs.mkdirSync(dirPath);
78 }
79
80 /**
81 * Helper function to asynchronously copy a file
82 */
83 public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> {
84 let deferred: Q.Deferred<void> = Q.defer<void>();
85 let destFile: fs.WriteStream = fs.createWriteStream(to, { encoding: encoding });
86 let srcFile: fs.ReadStream = fs.createReadStream(from, { encoding: encoding });
87 destFile.on("finish", function(): void {
88 deferred.resolve(void 0);
89 });
90
91 destFile.on("error", function(e: Error): void {
92 deferred.reject(e);
93 });
94
95 srcFile.on("error", function(e: Error): void {
96 deferred.reject(e);
97 });
98
99 srcFile.pipe(destFile);
100 return deferred.promise;
101 }
102
103 public deleteFileIfExistsSync(filename: string) {
104 if (this.existsSync(filename)) {
105 fs.unlinkSync(filename);
106 }
107 }
108
109 public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
110 return Q.nfcall<string>(fs.readFile, filename, encoding);
111 }
112
113 public writeFile(filename: string, data: any): Q.Promise<void> {
114 return Q.nfcall<void>(fs.writeFile, filename, data);
115 }
116
117 public unlink(filename: string): Q.Promise<void> {
118 return Q.nfcall<void>(fs.unlink, filename);
119 }
120
121 public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> {
122 return Q.nfcall(fs.readdir, folder).then((files: string[]) => {
123 const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`);
124 if (extFiles.length === 0) {
125 throw new Error(`Unable to find any ${extension} files.`);
126 }
127 return extFiles;
128 });
129 }
130
131 public mkDir(p: string): Q.Promise<void> {
132 return Q.nfcall<void>(fs.mkdir, p);
133 }
134
135 /**
136 * Recursively copy 'source' to 'target' asynchronously
137 *
138 * @param {string} source Location to copy from
139 * @param {string} target Location to copy to
140 * @returns {Q.Promise} A promise which is fulfilled when the copy completes, and is rejected on error
141 */
142 public copyRecursive(source: string, target: string): Q.Promise<void> {
143 return Q.nfcall<fs.Stats>(fs.stat, source).then(stats => {
144 if (stats.isDirectory()) {
145 return this.exists(target).then(exists => {
146 if (!exists) {
147 return Q.nfcall<void>(fs.mkdir, target);
148 }
149 })
150 .then(() => {
151 return Q.nfcall<string[]>(fs.readdir, source);
152 })
153 .then(contents => {
154 Q.all(contents.map((childPath: string): Q.Promise<void> => {
155 return this.copyRecursive(path.join(source, childPath), path.join(target, childPath));
156 }));
157 });
158 } else {
159 return this.copyFile(source, target);
160 }
161 });
162 }
163
164 public removePathRecursivelyAsync(p: string): Q.Promise<void> {
165 return this.exists(p).then(exists => {
166 if (exists) {
167 return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => {
168 if (stats.isDirectory()) {
169 return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => {
170 let result = Q<void>(void 0);
171 childPaths.forEach(childPath =>
172 result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath))));
173 return result;
174 }).then(() =>
175 Q.nfcall<void>(fs.rmdir, p));
176 } else {
177 /* file */
178 return Q.nfcall<void>(fs.unlink, p);
179 }
180 });
181 }
182 });
183 }
184
185 public removePathRecursivelySync(p: string): void {
186 if (fs.existsSync(p)) {
187 let stats = fs.statSync(p);
188 if (stats.isDirectory()) {
189 let contents = fs.readdirSync(p);
190 contents.forEach(childPath =>
191 this.removePathRecursivelySync(path.join(p, childPath)));
192 fs.rmdirSync(p);
193 } else {
194 /* file */
195 fs.unlinkSync(p);
196 }
197 }
198 }
199}