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 · 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";
bc7a32ceJimmy Thomson10 years ago5import * as path from "path";
3fb37ad5unknown10 years ago6import * as Q from "q";
7
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);
51769beaJoshua Skelton10 years ago63});
2b1090f9Joshua Skelton10 years ago64}
65
d24fea86Joshua Skelton10 years ago66/**
67* Helper (synchronous) function to create a directory recursively
68*/
3d2b4b71Joshua Skelton10 years ago69public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago70let parentPath = path.dirname(dirPath);
71if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago72this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago73}
74
51769beaJoshua Skelton10 years ago75fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago76}
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
3fb37ad5unknown10 years ago101public deleteFileIfExistsSync(filename: string) {
d24fea86Joshua Skelton10 years ago102if (this.existsSync(filename)) {
3fb37ad5unknown10 years ago103fs.unlinkSync(filename);
104}
105}
106
5d4d4de0digeff10 years ago107public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
0555a744digeff10 years ago108return Q.nfcall<string>(fs.readFile, filename, encoding);
3fb37ad5unknown10 years ago109}
110
4677921cdigeff10 years ago111public writeFile(filename: string, data: any): Q.Promise<void> {
0555a744digeff10 years ago112return Q.nfcall<void>(fs.writeFile, filename, data);
4677921cdigeff10 years ago113}
bc7a32ceJimmy Thomson10 years ago114
afc46a73Jimmy Thomson10 years ago115public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> {
bc7a32ceJimmy Thomson10 years ago116return Q.nfcall(fs.readdir, folder).then((files: string[]) => {
117const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`);
118if (extFiles.length === 0) {
119throw new Error(`Unable to find any ${extension} files.`);
3fb37ad5unknown10 years ago120}
afc46a73Jimmy Thomson10 years ago121return extFiles;
3fb37ad5unknown10 years ago122});
123}
124}