microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2db9ac850d4b22dd97b01aa5e98bb5b469cddb3a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

172lines · 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
aab2095edigeff10 years ago4import * as nodeFs from "fs";
bc7a32ceJimmy Thomson10 years ago5import * as path from "path";
3fb37ad5unknown10 years ago6
7export class FileSystem {
aab2095edigeff10 years ago8private fs: typeof nodeFs;
9
10constructor({ fs = nodeFs } = {}) {
11this.fs = fs;
12}
3fb37ad5unknown10 years ago13
ce5e88eeYuri Skorokhodov5 years ago14public ensureDirectory(dir: string): Promise<void> {
15return this.stat(dir).then((stat: nodeFs.Stats): void => {
3fb37ad5unknown10 years ago16if (stat.isDirectory()) {
17return;
18}
b3a793eeNisheet Jain10 years ago19throw new Error(`Expected ${dir} to be a directory`);
ce5e88eeYuri Skorokhodov5 years ago20}, (err: Error & { code?: string }): Promise<any> => {
3fb37ad5unknown10 years ago21if (err && err.code === "ENOENT") {
ce5e88eeYuri Skorokhodov5 years ago22return this.mkDir(dir);
3fb37ad5unknown10 years ago23}
b3a793eeNisheet Jain10 years ago24throw err;
3fb37ad5unknown10 years ago25});
26}
27
ce5e88eeYuri Skorokhodov5 years ago28public ensureFileWithContents(file: string, contents: string): Promise<void> {
29return this.stat(file).then((stat: nodeFs.Stats) => {
3fb37ad5unknown10 years ago30if (!stat.isFile()) {
31throw new Error(`Expected ${file} to be a file`);
32}
1e812de2dlebu10 years ago33
34return this.readFile(file).then(existingContents => {
35if (contents !== existingContents) {
36return this.writeFile(file, contents);
37}
ce5e88eeYuri Skorokhodov5 years ago38return Promise.resolve();
1e812de2dlebu10 years ago39});
ce5e88eeYuri Skorokhodov5 years ago40}, (err: Error & { code?: string }): Promise<any> => {
3fb37ad5unknown10 years ago41if (err && err.code === "ENOENT") {
ce5e88eeYuri Skorokhodov5 years ago42return this.writeFile(file, contents);
3fb37ad5unknown10 years ago43}
b3a793eeNisheet Jain10 years ago44throw err;
3fb37ad5unknown10 years ago45});
46}
47
d24fea86Joshua Skelton10 years ago48/**
49* Helper function to check if a file or directory exists
50*/
ce5e88eeYuri Skorokhodov5 years ago51public existsSync(filename: string): boolean {
52return this.fs.existsSync(filename);
3fb37ad5unknown10 years ago53}
54
2b1090f9Joshua Skelton10 years ago55/**
56* Helper (asynchronous) function to check if a file or directory exists
57*/
ce5e88eeYuri Skorokhodov5 years ago58public exists(filename: string): Promise<boolean> {
59return this.stat(filename)
60.then(() => {
61return Promise.resolve(true);
2b1090f9Joshua Skelton10 years ago62})
ce5e88eeYuri Skorokhodov5 years ago63.catch((err) => {
64return Promise.resolve(false);
51769beaJoshua Skelton10 years ago65});
2b1090f9Joshua Skelton10 years ago66}
67
db80cd4eJimmy Thomson10 years ago68/**
69* Helper async function to read the contents of a directory
70*/
ce5e88eeYuri Skorokhodov5 years ago71public readDir(directory: string): Promise<string[]> {
72return this.fs.promises.readdir(directory);
db80cd4eJimmy Thomson10 years ago73}
74
d24fea86Joshua Skelton10 years ago75/**
76* Helper (synchronous) function to create a directory recursively
77*/
3d2b4b71Joshua Skelton10 years ago78public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago79let parentPath = path.dirname(dirPath);
80if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago81this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago82}
83
aab2095edigeff10 years ago84this.fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago85}
86
87/**
88* Helper function to asynchronously copy a file
89*/
ce5e88eeYuri Skorokhodov5 years ago90public copyFile(from: string, to: string): Promise<void> {
91return this.fs.promises.copyFile(from, to);
d24fea86Joshua Skelton10 years ago92}
93
3fb37ad5unknown10 years ago94public deleteFileIfExistsSync(filename: string) {
d24fea86Joshua Skelton10 years ago95if (this.existsSync(filename)) {
aab2095edigeff10 years ago96this.fs.unlinkSync(filename);
3fb37ad5unknown10 years ago97}
98}
99
ce5e88eeYuri Skorokhodov5 years ago100public readFile(filename: string, encoding: string = "utf8"): Promise<string | Buffer> {
101return this.fs.promises.readFile(filename, { encoding });
3fb37ad5unknown10 years ago102}
103
ce5e88eeYuri Skorokhodov5 years ago104public writeFile(filename: string, data: any): Promise<void> {
105return this.fs.promises.writeFile(filename, data);
4677921cdigeff10 years ago106}
bc7a32ceJimmy Thomson10 years ago107
ce5e88eeYuri Skorokhodov5 years ago108public unlink(filename: string): Promise<void> {
109return this.fs.promises.unlink(filename);
bc96b26bJimmy Thomson10 years ago110}
111
ce5e88eeYuri Skorokhodov5 years ago112public mkDir(p: string): Promise<void> {
113return this.fs.promises.mkdir(p);
bc6696cbdigeff10 years ago114}
115
ce5e88eeYuri Skorokhodov5 years ago116public stat(fsPath: string): Promise<nodeFs.Stats> {
117return this.fs.promises.stat(fsPath);
bc6696cbdigeff10 years ago118}
119
ce5e88eeYuri Skorokhodov5 years ago120public directoryExists(directoryPath: string): Promise<boolean> {
bc6696cbdigeff10 years ago121return this.stat(directoryPath).then(stats => {
122return stats.isDirectory();
123}).catch(reason => {
124return reason.code === "ENOENT"
125? false
ce5e88eeYuri Skorokhodov5 years ago126: Promise.reject<boolean>(reason);
bc6696cbdigeff10 years ago127});
128}
129
50241c19digeff10 years ago130/**
131* Delete 'dirPath' if it's an empty folder. If not fail.
132*
133* @param {dirPath} path to the folder
134* @returns {void} Nothing
135*/
ce5e88eeYuri Skorokhodov5 years ago136public rmdir(dirPath: string): Promise<void> {
137return this.fs.promises.rmdir(dirPath);
138}
139
140public async removePathRecursivelyAsync(p: string): Promise<void> {
141const exists = await this.exists(p);
142if (exists) {
143const stats = await this.stat(p);
144if (stats.isDirectory()) {
145const childPaths = await this.readDir(p);
146childPaths.forEach(childPath => {
147return new Promise(() => this.removePathRecursivelyAsync(path.join(p, childPath)));
b031edc7digeff10 years ago148});
ce5e88eeYuri Skorokhodov5 years ago149this.rmdir(p);
150} else {
151/* file */
152return this.unlink(p);
153}
154}
155return Promise.resolve();
4e7a6f0edlebu10 years ago156}
157
8ab8eac0dlebu10 years ago158public removePathRecursivelySync(p: string): void {
aab2095edigeff10 years ago159if (this.fs.existsSync(p)) {
160let stats = this.fs.statSync(p);
8ab8eac0dlebu10 years ago161if (stats.isDirectory()) {
aab2095edigeff10 years ago162let contents = this.fs.readdirSync(p);
8ab8eac0dlebu10 years ago163contents.forEach(childPath =>
164this.removePathRecursivelySync(path.join(p, childPath)));
aab2095edigeff10 years ago165this.fs.rmdirSync(p);
8ab8eac0dlebu10 years ago166} else {
167/* file */
aab2095edigeff10 years ago168this.fs.unlinkSync(p);
8ab8eac0dlebu10 years ago169}
170}
171}
3fb37ad5unknown10 years ago172}