microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3d2b4b710b80ed7f71637750bf6b62dd4445a51d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

124lines · 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 } 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