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 · 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";
d24fea86Joshua Skelton10 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}
d24fea86Joshua Skelton10 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.
d24fea86Joshua Skelton10 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
d24fea86Joshua Skelton10 years ago41/**
42* Helper function to check if a file or directory exists
43*/
44public existsSync(filename: string) {
3fb37ad5unknown10 years ago45try {
d24fea86Joshua Skelton10 years ago46fs.statSync(filename);
3fb37ad5unknown10 years ago47return true;
48} catch (error) {
49return false;
50}
51}
52
2b1090f9Joshua Skelton10 years ago53/**
54* Helper (asynchronous) function to check if a file or directory exists
55*/
56public exists(filename: string): Q.Promise<boolean> {
57return Q.nfcall(fs.stat, filename)
58.then(function(){
59return Q.resolve(true);
60})
61.catch(function(err) {
62return Q.resolve(false);
63})
64}
65
d24fea86Joshua Skelton10 years ago66/**
67* Helper (synchronous) function to create a directory recursively
68*/
69public makeDirectoryRecursive(dirPath: string): void {
70let parentPath = path.dirname(dirPath);
71if (!this.existsSync(parentPath)) {
72this.makeDirectoryRecursive(parentPath);
73}
74
75fs.mkdirSync(dirPath)
76}
77
78/**
79* Helper function to asynchronously copy a file
80*/
2b1090f9Joshua Skelton10 years ago81public copyFile(from: string, to: string, encoding?: string): Q.Promise<void> {
82var deferred: Q.Deferred<void> = Q.defer<void>();
d24fea86Joshua Skelton10 years ago83var destFile: fs.WriteStream = fs.createWriteStream(to, { encoding: encoding });
84var srcFile: fs.ReadStream = fs.createReadStream(from, { encoding: encoding });
85destFile.on("finish", function(): void {
2b1090f9Joshua Skelton10 years ago86deferred.resolve(void 0);
d24fea86Joshua Skelton10 years ago87});
88
89destFile.on("error", function(e: Error): void {
90deferred.reject(e);
91});
92
93srcFile.on("error", function(e: Error): void {
94deferred.reject(e);
95});
96
97srcFile.pipe(destFile);
98return 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*/
105public getOrCreateTypingsTargetPath(projectRoot: string): string {
2b1090f9Joshua Skelton10 years ago106let targetPath = path.resolve(projectRoot, ".vscode", "typings");
107if (!this.existsSync(targetPath)) {
108this.makeDirectoryRecursive(targetPath);
d24fea86Joshua Skelton10 years ago109}
110
2b1090f9Joshua Skelton10 years ago111return targetPath;
d24fea86Joshua Skelton10 years ago112}
113
3fb37ad5unknown10 years ago114public deleteFileIfExistsSync(filename: string) {
d24fea86Joshua Skelton10 years ago115if (this.existsSync(filename)) {
3fb37ad5unknown10 years ago116fs.unlinkSync(filename);
117}
118}
119
120public readFile(filename: string, encoding: string): Q.Promise<string> {
121let contents = Q.defer<string>();
122
123fs.readFile(filename, encoding, (err: NodeJS.ErrnoException, data: string) => {
124if (err) {
125contents.reject(err);
126} else {
127contents.resolve(data);
128}
129});
130
131return contents.promise;
132}
133}