microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0904a0d7ddf15d888e1d494ac66aa4f27e49aefb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

68lines · 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";
6
7export class FileSystem {
8
9public ensureDirectory(dir: string): Q.Promise<void> {
10return Q.nfcall(fs.stat, dir).then((stat: fs.Stats): void => {
11if (stat.isDirectory()) {
12return;
13} else {
14throw new Error(`Expected ${dir} to be a directory`);
15}
16}, (err: Error & {code?: string}): Q.Promise<any> => {
17if (err && err.code === "ENOENT") {
18return Q.nfcall(fs.mkdir, dir);
19} else {
20throw err;
21}
22});
23}
24
25public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
26return Q.nfcall(fs.stat, file).then((stat: fs.Stats): void => {
27if (!stat.isFile()) {
28throw new Error(`Expected ${file} to be a file`);
29}
30// The file already exists, assume the contents are good and do not touch it.
31}, (err: Error & {code?: string}): Q.Promise<any> => {
32if (err && err.code === "ENOENT") {
33return Q.nfcall(fs.writeFile, file, contents);
34} else {
35throw err;
36}
37});
38}
39
40public fileExistsSync(filename: string) {
41try {
42fs.lstatSync(filename);
43return true;
44} catch (error) {
45return false;
46}
47}
48
49public deleteFileIfExistsSync(filename: string) {
50if (this.fileExistsSync(filename)) {
51fs.unlinkSync(filename);
52}
53}
54
55public readFile(filename: string, encoding: string): Q.Promise<string> {
56let contents = Q.defer<string>();
57
58fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => {
59if (err) {
60contents.reject(err);
61} else {
62contents.resolve(data);
63}
64});
65
66return contents.promise;
67}
68}