microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2b1090f993d570bfd63819ace5e2650adca40cb3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

133lines · 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 Q from "q";
6import * as path from "path";
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 makeDirectoryRecursive(dirPath: string): void {
70 let parentPath = path.dirname(dirPath);
71 if (!this.existsSync(parentPath)) {
72 this.makeDirectoryRecursive(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 /**
102 * Helper function to get the target path for the type definition files (to be used for intellisense).
103 * Creates the target path if it does not exist already.
104 */
105 public getOrCreateTypingsTargetPath(projectRoot: string): string {
106 let targetPath = path.resolve(projectRoot, ".vscode", "typings");
107 if (!this.existsSync(targetPath)) {
108 this.makeDirectoryRecursive(targetPath);
109 }
110
111 return targetPath;
112 }
113
114 public deleteFileIfExistsSync(filename: string) {
115 if (this.existsSync(filename)) {
116 fs.unlinkSync(filename);
117 }
118 }
119
120 public readFile(filename: string, encoding: string): Q.Promise<string> {
121 let contents = Q.defer<string>();
122
123 fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => {
124 if (err) {
125 contents.reject(err);
126 } else {
127 contents.resolve(data);
128 }
129 });
130
131 return contents.promise;
132 }
133}
134