microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a4ad19568b74f319bcd395c85afa60a8784b30da

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

181lines · 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> {
34472878RedMickey5 years ago15return this.stat(dir).then(
16(stat: nodeFs.Stats): void => {
17if (stat.isDirectory()) {
18return;
19}
20throw new Error(`Expected ${dir} to be a directory`);
21},
22(err: Error & { code?: string }): Promise<any> => {
23if (err && err.code === "ENOENT") {
24return this.mkDir(dir);
25}
26throw err;
27},
28);
3fb37ad5unknown10 years ago29}
30
ce5e88eeYuri Skorokhodov5 years ago31public ensureFileWithContents(file: string, contents: string): Promise<void> {
34472878RedMickey5 years ago32return this.stat(file).then(
33(stat: nodeFs.Stats) => {
34if (!stat.isFile()) {
35throw new Error(`Expected ${file} to be a file`);
36}
1e812de2dlebu10 years ago37
34472878RedMickey5 years ago38return this.readFile(file).then(existingContents => {
39if (contents !== existingContents) {
40return this.writeFile(file, contents);
41}
42return Promise.resolve();
43});
44},
45(err: Error & { code?: string }): Promise<any> => {
46if (err && err.code === "ENOENT") {
1e812de2dlebu10 years ago47return this.writeFile(file, contents);
48}
34472878RedMickey5 years ago49throw err;
50},
51);
3fb37ad5unknown10 years ago52}
53
d24fea86Joshua Skelton10 years ago54/**
55* Helper function to check if a file or directory exists
56*/
ce5e88eeYuri Skorokhodov5 years ago57public existsSync(filename: string): boolean {
34472878RedMickey5 years ago58return this.fs.existsSync(filename);
3fb37ad5unknown10 years ago59}
60
2b1090f9Joshua Skelton10 years ago61/**
62* Helper (asynchronous) function to check if a file or directory exists
63*/
ce5e88eeYuri Skorokhodov5 years ago64public exists(filename: string): Promise<boolean> {
65return this.stat(filename)
66.then(() => {
67return Promise.resolve(true);
2b1090f9Joshua Skelton10 years ago68})
34472878RedMickey5 years ago69.catch(() => {
ce5e88eeYuri Skorokhodov5 years ago70return Promise.resolve(false);
51769beaJoshua Skelton10 years ago71});
2b1090f9Joshua Skelton10 years ago72}
73
db80cd4eJimmy Thomson10 years ago74/**
75* Helper async function to read the contents of a directory
76*/
ce5e88eeYuri Skorokhodov5 years ago77public readDir(directory: string): Promise<string[]> {
78return this.fs.promises.readdir(directory);
db80cd4eJimmy Thomson10 years ago79}
80
d24fea86Joshua Skelton10 years ago81/**
82* Helper (synchronous) function to create a directory recursively
83*/
3d2b4b71Joshua Skelton10 years ago84public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago85let parentPath = path.dirname(dirPath);
86if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago87this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago88}
89
aab2095edigeff10 years ago90this.fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago91}
92
93/**
94* Helper function to asynchronously copy a file
95*/
ce5e88eeYuri Skorokhodov5 years ago96public copyFile(from: string, to: string): Promise<void> {
97return this.fs.promises.copyFile(from, to);
d24fea86Joshua Skelton10 years ago98}
99
34472878RedMickey5 years ago100public deleteFileIfExistsSync(filename: string): void {
d24fea86Joshua Skelton10 years ago101if (this.existsSync(filename)) {
aab2095edigeff10 years ago102this.fs.unlinkSync(filename);
3fb37ad5unknown10 years ago103}
104}
105
ce5e88eeYuri Skorokhodov5 years ago106public readFile(filename: string, encoding: string = "utf8"): Promise<string | Buffer> {
107return this.fs.promises.readFile(filename, { encoding });
3fb37ad5unknown10 years ago108}
109
ce5e88eeYuri Skorokhodov5 years ago110public writeFile(filename: string, data: any): Promise<void> {
111return this.fs.promises.writeFile(filename, data);
4677921cdigeff10 years ago112}
bc7a32ceJimmy Thomson10 years ago113
ce5e88eeYuri Skorokhodov5 years ago114public unlink(filename: string): Promise<void> {
115return this.fs.promises.unlink(filename);
bc96b26bJimmy Thomson10 years ago116}
117
ce5e88eeYuri Skorokhodov5 years ago118public mkDir(p: string): Promise<void> {
119return this.fs.promises.mkdir(p);
bc6696cbdigeff10 years ago120}
121
ce5e88eeYuri Skorokhodov5 years ago122public stat(fsPath: string): Promise<nodeFs.Stats> {
123return this.fs.promises.stat(fsPath);
bc6696cbdigeff10 years ago124}
125
ce5e88eeYuri Skorokhodov5 years ago126public directoryExists(directoryPath: string): Promise<boolean> {
34472878RedMickey5 years ago127return this.stat(directoryPath)
128.then(stats => {
129return stats.isDirectory();
130})
131.catch(reason => {
132return reason.code === "ENOENT" ? false : Promise.reject<boolean>(reason);
133});
bc6696cbdigeff10 years ago134}
135
50241c19digeff10 years ago136/**
137* Delete 'dirPath' if it's an empty folder. If not fail.
138*
139* @param {dirPath} path to the folder
140* @returns {void} Nothing
141*/
ce5e88eeYuri Skorokhodov5 years ago142public rmdir(dirPath: string): Promise<void> {
143return this.fs.promises.rmdir(dirPath);
144}
145
146public async removePathRecursivelyAsync(p: string): Promise<void> {
147const exists = await this.exists(p);
148if (exists) {
149const stats = await this.stat(p);
34472878RedMickey5 years ago150if (stats.isDirectory()) {
151const childPaths = await this.readDir(p);
c2331a81RedMickey5 years ago152await Promise.all(
153childPaths.map(childPath =>
34472878RedMickey5 years ago154this.removePathRecursivelyAsync(path.join(p, childPath)),
c2331a81RedMickey5 years ago155),
156);
157await this.rmdir(p);
34472878RedMickey5 years ago158} else {
159/* file */
160return this.unlink(p);
161}
ce5e88eeYuri Skorokhodov5 years ago162}
163return Promise.resolve();
4e7a6f0edlebu10 years ago164}
165
8ab8eac0dlebu10 years ago166public removePathRecursivelySync(p: string): void {
aab2095edigeff10 years ago167if (this.fs.existsSync(p)) {
168let stats = this.fs.statSync(p);
8ab8eac0dlebu10 years ago169if (stats.isDirectory()) {
aab2095edigeff10 years ago170let contents = this.fs.readdirSync(p);
8ab8eac0dlebu10 years ago171contents.forEach(childPath =>
34472878RedMickey5 years ago172this.removePathRecursivelySync(path.join(p, childPath)),
173);
aab2095edigeff10 years ago174this.fs.rmdirSync(p);
8ab8eac0dlebu10 years ago175} else {
176/* file */
aab2095edigeff10 years ago177this.fs.unlinkSync(p);
8ab8eac0dlebu10 years ago178}
179}
180}
3fb37ad5unknown10 years ago181}