microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8ab8eac02e97316421203cd7f8075cdef6e98769

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

98lines · 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";
5import * as Q from "q";
8ab8eac0dlebu10 years ago6import * as path from "path";
3fb37ad5unknown10 years ago7
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} else {
15throw new Error(`Expected ${dir} to be a directory`);
16}
8ab8eac0dlebu10 years ago17}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago18if (err && err.code === "ENOENT") {
19return Q.nfcall(fs.mkdir, dir);
20} else {
21throw err;
22}
23});
24}
25
26public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
27return Q.nfcall(fs.stat, file).then((stat: fs.Stats): void => {
28if (!stat.isFile()) {
29throw new Error(`Expected ${file} to be a file`);
30}
31// The file already exists, assume the contents are good and do not touch it.
8ab8eac0dlebu10 years ago32}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago33if (err && err.code === "ENOENT") {
34return Q.nfcall(fs.writeFile, file, contents);
35} else {
36throw err;
37}
38});
39}
40
41public fileExistsSync(filename: string) {
42try {
43fs.lstatSync(filename);
44return true;
45} catch (error) {
46return false;
47}
48}
49
50public deleteFileIfExistsSync(filename: string) {
51if (this.fileExistsSync(filename)) {
52fs.unlinkSync(filename);
53}
54}
55
56public readFile(filename: string, encoding: string): Q.Promise<string> {
57let contents = Q.defer<string>();
58
59fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => {
60if (err) {
61contents.reject(err);
62} else {
63contents.resolve(data);
64}
65});
66
67return contents.promise;
68}
4677921cdigeff10 years ago69
70public writeFile(filename: string, data: any): Q.Promise<void> {
71let contents = Q.defer<void>();
72
73fs.writeFile(filename, data, (err: NodeJS.ErrnoException) => {
74if (err) {
75contents.reject(err);
76} else {
77contents.resolve(null);
78}
79});
80
81return contents.promise;
82}
8ab8eac0dlebu10 years ago83
84public removePathRecursivelySync(p: string): void {
85if (fs.existsSync(p)) {
86let stats = fs.statSync(p);
87if (stats.isDirectory()) {
88let contents = fs.readdirSync(p);
89contents.forEach(childPath =>
90this.removePathRecursivelySync(path.join(p, childPath)));
91fs.rmdirSync(p);
92} else {
93/* file */
94fs.unlinkSync(p);
95}
96}
97}
3fb37ad5unknown10 years ago98}