microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
run-test-smoke1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

182lines · 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";
4bb0956eRedMickey5 years ago6import * as mkdirp from "mkdirp";
3fb37ad5unknown10 years ago7
8export class FileSystem {
aab2095edigeff10 years ago9private fs: typeof nodeFs;
10
11constructor({ fs = nodeFs } = {}) {
12this.fs = fs;
13}
3fb37ad5unknown10 years ago14
0d77292aJiglioNero4 years ago15public async ensureDirectory(dir: string): Promise<void> {
16try {
17const stat = await this.stat(dir);
18if (!stat.isDirectory()) {
34472878RedMickey5 years ago19throw new Error(`Expected ${dir} to be a directory`);
0d77292aJiglioNero4 years ago20}
21} catch (err) {
22if (err && err.code === "ENOENT") {
23return this.mkDir(dir);
24}
25throw err;
26}
3fb37ad5unknown10 years ago27}
28
0d77292aJiglioNero4 years ago29public async ensureFileWithContents(file: string, contents: string): Promise<void> {
30try {
31const stat = await this.stat(file);
32if (stat.isFile()) {
33const existingContents = await this.readFile(file);
34if (contents !== existingContents) {
1e812de2dlebu10 years ago35return this.writeFile(file, contents);
36}
0d77292aJiglioNero4 years ago37} else {
38throw new Error(`Expected ${file} to be a file`);
39}
40} catch (err) {
41if (err && err.code === "ENOENT") {
42return this.writeFile(file, contents);
43}
44throw err;
45}
3fb37ad5unknown10 years ago46}
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 {
34472878RedMickey5 years ago52return 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*/
0d77292aJiglioNero4 years ago58public async exists(filename: string): Promise<boolean> {
59try {
60await this.stat(filename);
61return true;
62} catch (err) {
63return false;
64}
2b1090f9Joshua Skelton10 years ago65}
66
db80cd4eJimmy Thomson10 years ago67/**
68* Helper async function to read the contents of a directory
69*/
ce5e88eeYuri Skorokhodov5 years ago70public readDir(directory: string): Promise<string[]> {
71return this.fs.promises.readdir(directory);
db80cd4eJimmy Thomson10 years ago72}
73
d24fea86Joshua Skelton10 years ago74/**
75* Helper (synchronous) function to create a directory recursively
76*/
3d2b4b71Joshua Skelton10 years ago77public makeDirectoryRecursiveSync(dirPath: string): void {
09f6024fHeniker4 years ago78const parentPath = path.dirname(dirPath);
d24fea86Joshua Skelton10 years ago79if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago80this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago81}
82
aab2095edigeff10 years ago83this.fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago84}
85
86/**
87* Helper function to asynchronously copy a file
88*/
ce5e88eeYuri Skorokhodov5 years ago89public copyFile(from: string, to: string): Promise<void> {
90return this.fs.promises.copyFile(from, to);
d24fea86Joshua Skelton10 years ago91}
92
34472878RedMickey5 years ago93public deleteFileIfExistsSync(filename: string): void {
d24fea86Joshua Skelton10 years ago94if (this.existsSync(filename)) {
aab2095edigeff10 years ago95this.fs.unlinkSync(filename);
3fb37ad5unknown10 years ago96}
97}
98
ce5e88eeYuri Skorokhodov5 years ago99public readFile(filename: string, encoding: string = "utf8"): Promise<string | Buffer> {
100return this.fs.promises.readFile(filename, { encoding });
3fb37ad5unknown10 years ago101}
102
ce5e88eeYuri Skorokhodov5 years ago103public writeFile(filename: string, data: any): Promise<void> {
104return this.fs.promises.writeFile(filename, data);
4677921cdigeff10 years ago105}
bc7a32ceJimmy Thomson10 years ago106
4bb0956eRedMickey5 years ago107public static writeFileToFolder(folder: string, basename: string, data: any): Promise<void> {
108if (!nodeFs.existsSync(folder)) {
109mkdirp.sync(folder);
110}
111return nodeFs.promises.writeFile(path.join(folder, basename), data);
112}
113
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
0d77292aJiglioNero4 years ago126public async directoryExists(directoryPath: string): Promise<boolean> {
127try {
128const stats = await this.stat(directoryPath);
129return stats.isDirectory();
130} catch (err) {
131if (err.code === "ENOENT") {
132return false;
133}
134throw err;
135}
bc6696cbdigeff10 years ago136}
137
50241c19digeff10 years ago138/**
139* Delete 'dirPath' if it's an empty folder. If not fail.
140*
141* @param {dirPath} path to the folder
142* @returns {void} Nothing
143*/
ce5e88eeYuri Skorokhodov5 years ago144public rmdir(dirPath: string): Promise<void> {
145return this.fs.promises.rmdir(dirPath);
146}
147
148public async removePathRecursivelyAsync(p: string): Promise<void> {
149const exists = await this.exists(p);
150if (exists) {
151const stats = await this.stat(p);
34472878RedMickey5 years ago152if (stats.isDirectory()) {
153const childPaths = await this.readDir(p);
c2331a81RedMickey5 years ago154await Promise.all(
155childPaths.map(childPath =>
34472878RedMickey5 years ago156this.removePathRecursivelyAsync(path.join(p, childPath)),
c2331a81RedMickey5 years ago157),
158);
159await this.rmdir(p);
34472878RedMickey5 years ago160} else {
161/* file */
162return this.unlink(p);
163}
ce5e88eeYuri Skorokhodov5 years ago164}
4e7a6f0edlebu10 years ago165}
166
8ab8eac0dlebu10 years ago167public removePathRecursivelySync(p: string): void {
aab2095edigeff10 years ago168if (this.fs.existsSync(p)) {
09f6024fHeniker4 years ago169const stats = this.fs.statSync(p);
8ab8eac0dlebu10 years ago170if (stats.isDirectory()) {
09f6024fHeniker4 years ago171const contents = this.fs.readdirSync(p);
8ab8eac0dlebu10 years ago172contents.forEach(childPath =>
34472878RedMickey5 years ago173this.removePathRecursivelySync(path.join(p, childPath)),
174);
aab2095edigeff10 years ago175this.fs.rmdirSync(p);
8ab8eac0dlebu10 years ago176} else {
177/* file */
aab2095edigeff10 years ago178this.fs.unlinkSync(p);
8ab8eac0dlebu10 years ago179}
180}
181}
3fb37ad5unknown10 years ago182}