microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
830b8c67a2065b1e65d2445b8f64ea1bf09f501d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/node/fileSystem.ts

116lines · 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";
8ab8eac0dlebu10 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}
b3a793eeNisheet Jain10 years ago15throw new Error(`Expected ${dir} to be a directory`);
8ab8eac0dlebu10 years ago16}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago17if (err && err.code === "ENOENT") {
18return Q.nfcall(fs.mkdir, dir);
19}
b3a793eeNisheet Jain10 years ago20throw err;
3fb37ad5unknown10 years ago21});
22}
23
24public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
25return Q.nfcall(fs.stat, file).then((stat: fs.Stats): void => {
26if (!stat.isFile()) {
27throw new Error(`Expected ${file} to be a file`);
28}
29// The file already exists, assume the contents are good and do not touch it.
8ab8eac0dlebu10 years ago30}, (err: Error & { code?: string }): Q.Promise<any> => {
3fb37ad5unknown10 years ago31if (err && err.code === "ENOENT") {
32return Q.nfcall(fs.writeFile, file, contents);
33}
b3a793eeNisheet Jain10 years ago34throw err;
3fb37ad5unknown10 years ago35});
36}
37
38public fileExistsSync(filename: string) {
39try {
40fs.lstatSync(filename);
41return true;
42} catch (error) {
43return false;
44}
45}
46
47public deleteFileIfExistsSync(filename: string) {
48if (this.fileExistsSync(filename)) {
49fs.unlinkSync(filename);
50}
51}
52
5d4d4de0digeff10 years ago53public readFile(filename: string, encoding: string = "utf8"): Q.Promise<string> {
0555a744digeff10 years ago54return Q.nfcall<string>(fs.readFile, filename, encoding);
3fb37ad5unknown10 years ago55}
4677921cdigeff10 years ago56
57public writeFile(filename: string, data: any): Q.Promise<void> {
0555a744digeff10 years ago58return Q.nfcall<void>(fs.writeFile, filename, data);
4677921cdigeff10 years ago59}
60
afc46a73Jimmy Thomson10 years ago61public findFilesByExtension(folder: string, extension: string): Q.Promise<string[]> {
bc7a32ceJimmy Thomson10 years ago62return Q.nfcall(fs.readdir, folder).then((files: string[]) => {
63const extFiles = files.filter((file: string) => path.extname(file) === `.${extension}`);
64if (extFiles.length === 0) {
65throw new Error(`Unable to find any ${extension} files.`);
4677921cdigeff10 years ago66}
afc46a73Jimmy Thomson10 years ago67return extFiles;
4677921cdigeff10 years ago68});
69}
8ab8eac0dlebu10 years ago70
4e7a6f0edlebu10 years ago71public pathExists(p: string): Q.Promise<boolean> {
72let deferred = Q.defer<boolean>();
73fs.exists(p, deferred.resolve);
74return deferred.promise;
75}
76
77public mkDir(p: string): Q.Promise<void> {
78return Q.nfcall<void>(fs.mkdir, p);
79}
80
81public removePathRecursivelyAsync(p: string): Q.Promise<void> {
82return this.pathExists(p).then(exists => {
83if (exists) {
84return Q.nfcall<fs.Stats>(fs.stat, p).then((stats: fs.Stats) => {
85if (stats.isDirectory()) {
86return Q.nfcall<string[]>(fs.readdir, p).then((childPaths: string[]) => {
87let result = Q<void>(void 0);
88childPaths.forEach(childPath =>
89result = result.then<void>(() => this.removePathRecursivelyAsync(path.join(p, childPath))));
90return result;
91}).then(() =>
92Q.nfcall<void>(fs.rmdir, p));
93} else {
94/* file */
95return Q.nfcall<void>(fs.unlink, p);
96}
97});
98}
99});
100}
101
8ab8eac0dlebu10 years ago102public removePathRecursivelySync(p: string): void {
103if (fs.existsSync(p)) {
104let stats = fs.statSync(p);
105if (stats.isDirectory()) {
106let contents = fs.readdirSync(p);
107contents.forEach(childPath =>
108this.removePathRecursivelySync(path.join(p, childPath)));
109fs.rmdirSync(p);
110} else {
111/* file */
112fs.unlinkSync(p);
113}
114}
115}
3fb37ad5unknown10 years ago116}