microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2ec44b6d62529aa08a88dc1dbe190833e1838036

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

116lines · 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): void => {
26 if (!stat.isFile()) {
27 throw new Error(`Expected ${file} to be a file`);
28 }
29 // The file already exists, assume the contents are good and do not touch it.
30 }, (err: Error & { code?: string }): Q.Promise<any> => {
31 if (err && err.code === "ENOENT") {
32 return Q.nfcall(fs.writeFile, file, contents);
33 }
34 throw err;
35 });
36 }
37
38 public fileExistsSync(filename: string) {
39 try {
40 fs.lstatSync(filename);
41 return true;
42 } catch (error) {
43 return false;
44 }
45 }
46
47 public deleteFileIfExistsSync(filename: string) {
48 if (this.fileExistsSync(filename)) {
49 fs.unlinkSync(filename);
50 }
51 }
52
53 public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
54 return Q.nfcall<string>(fs.readFile, filename, encoding);
55 }
56
57 public writeFile(filename: string, data: any): Q.Promise<void> {
58 return Q.nfcall<void>(fs.writeFile, filename, data);
59 }
60
61 public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> {
62 return Q.nfcall(fs.readdir, folder).then((files: string[]) => {
63 const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`);
64 if (extFiles.length === 0) {
65 throw new Error(`Unable to find any ${extension} files.`);
66 }
67 return extFiles;
68 });
69 }
70
71 public pathExists(p: string): Q.Promise<boolean> {
72 let deferred = Q.defer<boolean>();
73 fs.exists(p, deferred.resolve);
74 return deferred.promise;
75 }
76
77 public mkDir(p: string): Q.Promise<void> {
78 return Q.nfcall<void>(fs.mkdir, p);
79 }
80
81 public removePathRecursivelyAsync(p: string): Q.Promise<void> {
82 return this.pathExists(p).then(exists => {
83 if (exists) {
84 return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => {
85 if (stats.isDirectory()) {
86 return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => {
87 let result = Q<void>(void 0);
88 childPaths.forEach(childPath =>
89 result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath))));
90 return result;
91 }).then(() =>
92 Q.nfcall<void>(fs.rmdir, p));
93 } else {
94 /* file */
95 return Q.nfcall<void>(fs.unlink, p);
96 }
97 });
98 }
99 });
100 }
101
102 public removePathRecursivelySync(p: string): void {
103 if (fs.existsSync(p)) {
104 let stats = fs.statSync(p);
105 if (stats.isDirectory()) {
106 let contents = fs.readdirSync(p);
107 contents.forEach(childPath =>
108 this.removePathRecursivelySync(path.join(p, childPath)));
109 fs.rmdirSync(p);
110 } else {
111 /* file */
112 fs.unlinkSync(p);
113 }
114 }
115 }
116}
117