microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.13

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

227lines · 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
aab2095edigeff10 years ago4import * as nodeFs from "fs";
bc7a32ceJimmy Thomson10 years ago5import * as path from "path";
3fb37ad5unknown10 years ago6import * as Q from "q";
7
8export class FileSystem {
aab2095edigeff10 years ago9private fs: typeof nodeFs;
10
11constructor({ fs = nodeFs } = {}) {
12this.fs = fs;
13}
3fb37ad5unknown10 years ago14
15public ensureDirectory(dir: string): Q.Promise<void> {
aab2095edigeff10 years ago16return Q.nfcall(this.fs.stat, dir).then((stat: nodeFs.Stats): void => {
3fb37ad5unknown10 years ago17if (stat.isDirectory()) {
18return;
19}
b3a793eeNisheet Jain10 years ago20throw new Error(`Expected ${dir} to be a directory`);
d24fea86Joshua Skelton10 years ago21}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago22if (err && err.code === "ENOENT") {
aab2095edigeff10 years ago23return Q.nfcall(this.fs.mkdir, dir);
3fb37ad5unknown10 years ago24}
b3a793eeNisheet Jain10 years ago25throw err;
3fb37ad5unknown10 years ago26});
27}
28
29public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
aab2095edigeff10 years ago30return Q.nfcall(this.fs.stat, file).then((stat: nodeFs.Stats) => {
3fb37ad5unknown10 years ago31if (!stat.isFile()) {
32throw new Error(`Expected ${file} to be a file`);
33}
1e812de2dlebu10 years ago34
35return this.readFile(file).then(existingContents => {
36if (contents !== existingContents) {
37return this.writeFile(file, contents);
38}
5c8365a6Artem Egorov8 years ago39return Q.resolve(void 0);
1e812de2dlebu10 years ago40});
d24fea86Joshua Skelton10 years ago41}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago42if (err && err.code === "ENOENT") {
aab2095edigeff10 years ago43return Q.nfcall(this.fs.writeFile, file, contents);
3fb37ad5unknown10 years ago44}
b3a793eeNisheet Jain10 years ago45throw err;
3fb37ad5unknown10 years ago46});
47}
48
d24fea86Joshua Skelton10 years ago49/**
50* Helper function to check if a file or directory exists
51*/
52public existsSync(filename: string) {
3fb37ad5unknown10 years ago53try {
aab2095edigeff10 years ago54this.fs.statSync(filename);
3fb37ad5unknown10 years ago55return true;
56} catch (error) {
57return false;
58}
59}
60
2b1090f9Joshua Skelton10 years ago61/**
62* Helper (asynchronous) function to check if a file or directory exists
63*/
64public exists(filename: string): Q.Promise<boolean> {
aab2095edigeff10 years ago65return Q.nfcall(this.fs.stat, filename)
b031edc7digeff10 years ago66.then(function() {
2b1090f9Joshua Skelton10 years ago67return Q.resolve(true);
68})
69.catch(function(err) {
70return Q.resolve(false);
51769beaJoshua Skelton10 years ago71});
2b1090f9Joshua Skelton10 years ago72}
73
db80cd4eJimmy Thomson10 years ago74/**
75* Helper async function to read the contents of a directory
76*/
77public readDir(directory: string): Q.Promise<string[]> {
aab2095edigeff10 years ago78return Q.nfcall<string[]>(this.fs.readdir, directory);
db80cd4eJimmy Thomson10 years ago79}
80
d24fea86Joshua Skelton10 years ago81/**
82* Helper (synchronous) function to create a directory recursively
83*/
3d2b4b71Joshua Skelton10 years ago84public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago85let parentPath = path.dirname(dirPath);
86if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago87this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago88}
89
aab2095edigeff10 years ago90this.fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago91}
92
93/**
94* Helper function to asynchronously copy a file
95*/
2b1090f9Joshua Skelton10 years ago96public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> {
d8231ba2Joshua Skelton10 years ago97let deferred: Q.Deferred<void> = Q.defer<void>();
aab2095edigeff10 years ago98let destFile: nodeFs.WriteStream = this.fs.createWriteStream(to, { encoding: encoding });
99let srcFile: nodeFs.ReadStream = this.fs.createReadStream(from, { encoding: encoding });
d24fea86Joshua Skelton10 years ago100destFile.on("finish", function(): void {
2b1090f9Joshua Skelton10 years ago101deferred.resolve(void 0);
d24fea86Joshua Skelton10 years ago102});
103
104destFile.on("error", function(e: Error): void {
105deferred.reject(e);
106});
107
108srcFile.on("error", function(e: Error): void {
109deferred.reject(e);
110});
111
112srcFile.pipe(destFile);
113return deferred.promise;
114}
115
3fb37ad5unknown10 years ago116public deleteFileIfExistsSync(filename: string) {
d24fea86Joshua Skelton10 years ago117if (this.existsSync(filename)) {
aab2095edigeff10 years ago118this.fs.unlinkSync(filename);
3fb37ad5unknown10 years ago119}
120}
121
5d4d4de0digeff10 years ago122public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
aab2095edigeff10 years ago123return Q.nfcall<string>(this.fs.readFile, filename, encoding);
3fb37ad5unknown10 years ago124}
125
4677921cdigeff10 years ago126public writeFile(filename: string, data: any): Q.Promise<void> {
aab2095edigeff10 years ago127return Q.nfcall<void>(this.fs.writeFile, filename, data);
4677921cdigeff10 years ago128}
bc7a32ceJimmy Thomson10 years ago129
bc96b26bJimmy Thomson10 years ago130public unlink(filename: string): Q.Promise<void> {
aab2095edigeff10 years ago131return Q.nfcall<void>(this.fs.unlink, filename);
bc96b26bJimmy Thomson10 years ago132}
133
4e7a6f0edlebu10 years ago134public mkDir(p: string): Q.Promise<void> {
bc6696cbdigeff10 years ago135return Q.nfcall<void>(this.fs.mkdir, p);
136}
137
27710197Vladimir Kotikov8 years ago138public stat(fsPath: string): Q.Promise<nodeFs.Stats> {
139return Q.nfcall<nodeFs.Stats>(this.fs.stat, fsPath);
bc6696cbdigeff10 years ago140}
141
142public directoryExists(directoryPath: string): Q.Promise<boolean> {
143return this.stat(directoryPath).then(stats => {
144return stats.isDirectory();
145}).catch(reason => {
146return reason.code === "ENOENT"
147? false
148: Q.reject<boolean>(reason);
149});
150}
151
50241c19digeff10 years ago152
153/**
154* Delete 'dirPath' if it's an empty folder. If not fail.
155*
156* @param {dirPath} path to the folder
157* @returns {void} Nothing
158*/
bc6696cbdigeff10 years ago159public rmdir(dirPath: string): Q.Promise<void> {
160return Q.nfcall<void>(this.fs.rmdir, dirPath);
4e7a6f0edlebu10 years ago161}
162
e7f5d062Joshua Skelton10 years ago163/**
164* Recursively copy 'source' to 'target' asynchronously
165*
166* @param {string} source Location to copy from
167* @param {string} target Location to copy to
168* @returns {Q.Promise} A promise which is fulfilled when the copy completes, and is rejected on error
169*/
170public copyRecursive(source: string, target: string): Q.Promise<void> {
aab2095edigeff10 years ago171return Q.nfcall<nodeFs.Stats>(this.fs.stat, source).then(stats => {
e7f5d062Joshua Skelton10 years ago172if (stats.isDirectory()) {
5c8365a6Artem Egorov8 years ago173return this.exists(target)
174.then(exists => {
175return exists ? void 0 : Q.nfcall<void>(this.fs.mkdir, target);
176})
b031edc7digeff10 years ago177.then(() => {
aab2095edigeff10 years ago178return Q.nfcall<string[]>(this.fs.readdir, source);
b031edc7digeff10 years ago179})
180.then(contents => {
181Q.all(contents.map((childPath: string): Q.Promise<void> => {
182return this.copyRecursive(path.join(source, childPath), path.join(target, childPath));
183}));
184});
e7f5d062Joshua Skelton10 years ago185} else {
186return this.copyFile(source, target);
187}
188});
189}
190
4e7a6f0edlebu10 years ago191public removePathRecursivelyAsync(p: string): Q.Promise<void> {
e7f5d062Joshua Skelton10 years ago192return this.exists(p).then(exists => {
4e7a6f0edlebu10 years ago193if (exists) {
aab2095edigeff10 years ago194return Q.nfcall<nodeFs.Stats>(this.fs.stat, p).then((stats: nodeFs.Stats) => {
4e7a6f0edlebu10 years ago195if (stats.isDirectory()) {
aab2095edigeff10 years ago196return Q.nfcall<string[]>(this.fs.readdir, p).then((childPaths: string[]) => {
4e7a6f0edlebu10 years ago197let result = Q<void>(void 0);
198childPaths.forEach(childPath =>
199result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath))));
200return result;
201}).then(() =>
aab2095edigeff10 years ago202Q.nfcall<void>(this.fs.rmdir, p));
4e7a6f0edlebu10 years ago203} else {
204/* file */
aab2095edigeff10 years ago205return Q.nfcall<void>(this.fs.unlink, p);
4e7a6f0edlebu10 years ago206}
207});
208}
5c8365a6Artem Egorov8 years ago209return Q.resolve(void 0);
4e7a6f0edlebu10 years ago210});
211}
212
8ab8eac0dlebu10 years ago213public removePathRecursivelySync(p: string): void {
aab2095edigeff10 years ago214if (this.fs.existsSync(p)) {
215let stats = this.fs.statSync(p);
8ab8eac0dlebu10 years ago216if (stats.isDirectory()) {
aab2095edigeff10 years ago217let contents = this.fs.readdirSync(p);
8ab8eac0dlebu10 years ago218contents.forEach(childPath =>
219this.removePathRecursivelySync(path.join(p, childPath)));
aab2095edigeff10 years ago220this.fs.rmdirSync(p);
8ab8eac0dlebu10 years ago221} else {
222/* file */
aab2095edigeff10 years ago223this.fs.unlinkSync(p);
8ab8eac0dlebu10 years ago224}
225}
226}
3fb37ad5unknown10 years ago227}