microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4bb0956ede5635069acf360dcc9c791802048661

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/fileSystem.ts

189lines · 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
ce5e88eeYuri Skorokhodov5 years ago15public ensureDirectory(dir: string): Promise<void> {
34472878RedMickey5 years ago16return this.stat(dir).then(
17(stat: nodeFs.Stats): void => {
18if (stat.isDirectory()) {
19return;
20}
21throw new Error(`Expected ${dir} to be a directory`);
22},
23(err: Error & { code?: string }): Promise<any> => {
24if (err && err.code === "ENOENT") {
25return this.mkDir(dir);
26}
27throw err;
28},
29);
3fb37ad5unknown10 years ago30}
31
ce5e88eeYuri Skorokhodov5 years ago32public ensureFileWithContents(file: string, contents: string): Promise<void> {
34472878RedMickey5 years ago33return this.stat(file).then(
34(stat: nodeFs.Stats) => {
35if (!stat.isFile()) {
36throw new Error(`Expected ${file} to be a file`);
37}
1e812de2dlebu10 years ago38
34472878RedMickey5 years ago39return this.readFile(file).then(existingContents => {
40if (contents !== existingContents) {
41return this.writeFile(file, contents);
42}
43return Promise.resolve();
44});
45},
46(err: Error & { code?: string }): Promise<any> => {
47if (err && err.code === "ENOENT") {
1e812de2dlebu10 years ago48return this.writeFile(file, contents);
49}
34472878RedMickey5 years ago50throw err;
51},
52);
3fb37ad5unknown10 years ago53}
54
d24fea86Joshua Skelton10 years ago55/**
56* Helper function to check if a file or directory exists
57*/
ce5e88eeYuri Skorokhodov5 years ago58public existsSync(filename: string): boolean {
34472878RedMickey5 years ago59return this.fs.existsSync(filename);
3fb37ad5unknown10 years ago60}
61
2b1090f9Joshua Skelton10 years ago62/**
63* Helper (asynchronous) function to check if a file or directory exists
64*/
ce5e88eeYuri Skorokhodov5 years ago65public exists(filename: string): Promise<boolean> {
66return this.stat(filename)
67.then(() => {
68return Promise.resolve(true);
2b1090f9Joshua Skelton10 years ago69})
34472878RedMickey5 years ago70.catch(() => {
ce5e88eeYuri Skorokhodov5 years ago71return Promise.resolve(false);
51769beaJoshua Skelton10 years ago72});
2b1090f9Joshua Skelton10 years ago73}
74
db80cd4eJimmy Thomson10 years ago75/**
76* Helper async function to read the contents of a directory
77*/
ce5e88eeYuri Skorokhodov5 years ago78public readDir(directory: string): Promise<string[]> {
79return this.fs.promises.readdir(directory);
db80cd4eJimmy Thomson10 years ago80}
81
d24fea86Joshua Skelton10 years ago82/**
83* Helper (synchronous) function to create a directory recursively
84*/
3d2b4b71Joshua Skelton10 years ago85public makeDirectoryRecursiveSync(dirPath: string): void {
d24fea86Joshua Skelton10 years ago86let parentPath = path.dirname(dirPath);
87if (!this.existsSync(parentPath)) {
3d2b4b71Joshua Skelton10 years ago88this.makeDirectoryRecursiveSync(parentPath);
d24fea86Joshua Skelton10 years ago89}
90
aab2095edigeff10 years ago91this.fs.mkdirSync(dirPath);
d24fea86Joshua Skelton10 years ago92}
93
94/**
95* Helper function to asynchronously copy a file
96*/
ce5e88eeYuri Skorokhodov5 years ago97public copyFile(from: string, to: string): Promise<void> {
98return this.fs.promises.copyFile(from, to);
d24fea86Joshua Skelton10 years ago99}
100
34472878RedMickey5 years ago101public deleteFileIfExistsSync(filename: string): void {
d24fea86Joshua Skelton10 years ago102if (this.existsSync(filename)) {
aab2095edigeff10 years ago103this.fs.unlinkSync(filename);
3fb37ad5unknown10 years ago104}
105}
106
ce5e88eeYuri Skorokhodov5 years ago107public readFile(filename: string, encoding: string = "utf8"): Promise<string | Buffer> {
108return this.fs.promises.readFile(filename, { encoding });
3fb37ad5unknown10 years ago109}
110
ce5e88eeYuri Skorokhodov5 years ago111public writeFile(filename: string, data: any): Promise<void> {
112return this.fs.promises.writeFile(filename, data);
4677921cdigeff10 years ago113}
bc7a32ceJimmy Thomson10 years ago114
4bb0956eRedMickey5 years ago115public static writeFileToFolder(folder: string, basename: string, data: any): Promise<void> {
116if (!nodeFs.existsSync(folder)) {
117mkdirp.sync(folder);
118}
119return nodeFs.promises.writeFile(path.join(folder, basename), data);
120}
121
ce5e88eeYuri Skorokhodov5 years ago122public unlink(filename: string): Promise<void> {
123return this.fs.promises.unlink(filename);
bc96b26bJimmy Thomson10 years ago124}
125
ce5e88eeYuri Skorokhodov5 years ago126public mkDir(p: string): Promise<void> {
127return this.fs.promises.mkdir(p);
bc6696cbdigeff10 years ago128}
129
ce5e88eeYuri Skorokhodov5 years ago130public stat(fsPath: string): Promise<nodeFs.Stats> {
131return this.fs.promises.stat(fsPath);
bc6696cbdigeff10 years ago132}
133
ce5e88eeYuri Skorokhodov5 years ago134public directoryExists(directoryPath: string): Promise<boolean> {
34472878RedMickey5 years ago135return this.stat(directoryPath)
136.then(stats => {
137return stats.isDirectory();
138})
139.catch(reason => {
140return reason.code === "ENOENT" ? false : Promise.reject<boolean>(reason);
141});
bc6696cbdigeff10 years ago142}
143
50241c19digeff10 years ago144/**
145* Delete 'dirPath' if it's an empty folder. If not fail.
146*
147* @param {dirPath} path to the folder
148* @returns {void} Nothing
149*/
ce5e88eeYuri Skorokhodov5 years ago150public rmdir(dirPath: string): Promise<void> {
151return this.fs.promises.rmdir(dirPath);
152}
153
154public async removePathRecursivelyAsync(p: string): Promise<void> {
155const exists = await this.exists(p);
156if (exists) {
157const stats = await this.stat(p);
34472878RedMickey5 years ago158if (stats.isDirectory()) {
159const childPaths = await this.readDir(p);
c2331a81RedMickey5 years ago160await Promise.all(
161childPaths.map(childPath =>
34472878RedMickey5 years ago162this.removePathRecursivelyAsync(path.join(p, childPath)),
c2331a81RedMickey5 years ago163),
164);
165await this.rmdir(p);
34472878RedMickey5 years ago166} else {
167/* file */
168return this.unlink(p);
169}
ce5e88eeYuri Skorokhodov5 years ago170}
171return Promise.resolve();
4e7a6f0edlebu10 years ago172}
173
8ab8eac0dlebu10 years ago174public removePathRecursivelySync(p: string): void {
aab2095edigeff10 years ago175if (this.fs.existsSync(p)) {
176let stats = this.fs.statSync(p);
8ab8eac0dlebu10 years ago177if (stats.isDirectory()) {
aab2095edigeff10 years ago178let contents = this.fs.readdirSync(p);
8ab8eac0dlebu10 years ago179contents.forEach(childPath =>
34472878RedMickey5 years ago180this.removePathRecursivelySync(path.join(p, childPath)),
181);
aab2095edigeff10 years ago182this.fs.rmdirSync(p);
8ab8eac0dlebu10 years ago183} else {
184/* file */
aab2095edigeff10 years ago185this.fs.unlinkSync(p);
8ab8eac0dlebu10 years ago186}
187}
188}
3fb37ad5unknown10 years ago189}