microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
da86e6fffddfaf7f6ca5a97cf9f1c345394f3bdc

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

199lines · modeblame

3fb37ad5unknown10 years ago1// 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";
bc7a32ceJimmy Thomson10 years ago5import * as path from "path";
3fb37ad5unknown10 years ago6import * as Q from "q";
7
8export class FileSystem {
9
10public ensureDirectory(dir: string): Q.Promise<void> {
11return Q.nfcall(fs.stat, dir).then((stat: fs.Stats): void => {
12if (stat.isDirectory()) {
13return;
14}
b3a793eeNisheet Jain10 years ago15throw new Error(`Expected ${dir} to be a directory`);
d24fea86Joshua Skelton10 years ago16}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago17if (err && err.code === "ENOENT") {
18return Q.nfcall(fs.mkdir, dir);
19}
b3a793eeNisheet Jain10 years ago20throw err;
3fb37ad5unknown10 years ago21});
22}
23
24public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
1e812de2dlebu10 years ago25return Q.nfcall(fs.stat, file).then((stat: fs.Stats) => {
3fb37ad5unknown10 years ago26if (!stat.isFile()) {
27throw new Error(`Expected ${file} to be a file`);
28}
1e812de2dlebu10 years ago29
30return this.readFile(file).then(existingContents => {
31if (contents !== existingContents) {
32return this.writeFile(file, contents);
33}
34});
d24fea86Joshua Skelton10 years ago35}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago36if (err && err.code === "ENOENT") {
37return Q.nfcall(fs.writeFile, file, contents);
38}
b3a793eeNisheet Jain10 years ago39throw err;
3fb37ad5unknown10 years ago40});
41}
42
d24fea86Joshua Skelton10 years ago43/**
44* Helper function to check if a file or directory exists
45*/
46public existsSync(filename: string) {
3fb37ad5unknown10 years ago47try {
d24fea86Joshua Skelton10 years ago48fs.statSync(filename);
3fb37ad5unknown10 years ago49return true;
50} catch (error) {
51return false;
52}
53}
54
2b1090f9Joshua Skelton10 years ago55/**
56* Helper (asynchronous) function to check if a file or directory exists
57*/
58public exists(filename: string): Q.Promise<boolean> {
59return Q.nfcall(fs.stat, filename)
b031edc7digeff10 years ago60.then(function() {
2b1090f9Joshua Skelton10 years ago61return Q.resolve(true);
62})
63.catch(function(err) {
64return Q.resolve(false);
51769beaJoshua Skelton10 years ago65});
2b1090f9Joshua Skelton10 years ago66}
67
d24fea86Joshua Skelton10 years ago68/**
69* Helper (synchronous) function to create a directory recursively
70*/
3d2b4b71Joshua Skelton10 years ago71public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago72let parentPath = path.dirname(dirPath);
73if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago74this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago75}
76
51769beaJoshua Skelton10 years ago77fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago78}
79
80/**
81* Helper function to asynchronously copy a file
82*/
2b1090f9Joshua Skelton10 years ago83public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> {
d8231ba2Joshua Skelton10 years ago84let deferred: Q.Deferred<void> = Q.defer<void>();
85let destFile: fs.WriteStream = fs.createWriteStream(to, { encoding: encoding });
86let srcFile: fs.ReadStream = fs.createReadStream(from, { encoding: encoding });
d24fea86Joshua Skelton10 years ago87destFile.on("finish", function(): void {
2b1090f9Joshua Skelton10 years ago88deferred.resolve(void 0);
d24fea86Joshua Skelton10 years ago89});
90
91destFile.on("error", function(e: Error): void {
92deferred.reject(e);
93});
94
95srcFile.on("error", function(e: Error): void {
96deferred.reject(e);
97});
98
99srcFile.pipe(destFile);
100return deferred.promise;
101}
102
3fb37ad5unknown10 years ago103public deleteFileIfExistsSync(filename: string) {
d24fea86Joshua Skelton10 years ago104if (this.existsSync(filename)) {
3fb37ad5unknown10 years ago105fs.unlinkSync(filename);
106}
107}
108
5d4d4de0digeff10 years ago109public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
0555a744digeff10 years ago110return Q.nfcall<string>(fs.readFile, filename, encoding);
3fb37ad5unknown10 years ago111}
112
4677921cdigeff10 years ago113public writeFile(filename: string, data: any): Q.Promise<void> {
0555a744digeff10 years ago114return Q.nfcall<void>(fs.writeFile, filename, data);
4677921cdigeff10 years ago115}
bc7a32ceJimmy Thomson10 years ago116
bc96b26bJimmy Thomson10 years ago117public unlink(filename: string): Q.Promise<void> {
118return Q.nfcall<void>(fs.unlink, filename);
119}
120
afc46a73Jimmy Thomson10 years ago121public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> {
bc7a32ceJimmy Thomson10 years ago122return Q.nfcall(fs.readdir, folder).then((files: string[]) => {
123const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`);
124if (extFiles.length === 0) {
125throw new Error(`Unable to find any ${extension} files.`);
3fb37ad5unknown10 years ago126}
afc46a73Jimmy Thomson10 years ago127return extFiles;
3fb37ad5unknown10 years ago128});
129}
8ab8eac0dlebu10 years ago130
4e7a6f0edlebu10 years ago131public mkDir(p: string): Q.Promise<void> {
132return Q.nfcall<void>(fs.mkdir, p);
133}
134
e7f5d062Joshua Skelton10 years ago135/**
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*/
142public copyRecursive(source: string, target: string): Q.Promise<void> {
143return Q.nfcall<fs.Stats>(fs.stat, source).then(stats => {
144if (stats.isDirectory()) {
145return this.exists(target).then(exists => {
146if (!exists) {
147return Q.nfcall<void>(fs.mkdir, target);
148}
149})
b031edc7digeff10 years ago150.then(() => {
151return Q.nfcall<string[]>(fs.readdir, source);
152})
153.then(contents => {
154Q.all(contents.map((childPath: string): Q.Promise<void> => {
155return this.copyRecursive(path.join(source, childPath), path.join(target, childPath));
156}));
157});
e7f5d062Joshua Skelton10 years ago158} else {
159return this.copyFile(source, target);
160}
161});
162}
163
4e7a6f0edlebu10 years ago164public removePathRecursivelyAsync(p: string): Q.Promise<void> {
e7f5d062Joshua Skelton10 years ago165return this.exists(p).then(exists => {
4e7a6f0edlebu10 years ago166if (exists) {
167return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => {
168if (stats.isDirectory()) {
169return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => {
170let result = Q<void>(void 0);
171childPaths.forEach(childPath =>
172result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath))));
173return result;
174}).then(() =>
175Q.nfcall<void>(fs.rmdir, p));
176} else {
177/* file */
178return Q.nfcall<void>(fs.unlink, p);
179}
180});
181}
182});
183}
184
8ab8eac0dlebu10 years ago185public removePathRecursivelySync(p: string): void {
186if (fs.existsSync(p)) {
187let stats = fs.statSync(p);
188if (stats.isDirectory()) {
189let contents = fs.readdirSync(p);
190contents.forEach(childPath =>
191this.removePathRecursivelySync(path.join(p, childPath)));
192fs.rmdirSync(p);
193} else {
194/* file */
195fs.unlinkSync(p);
196}
197}
198}
3fb37ad5unknown10 years ago199}