-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathfile-system.js
98 lines (81 loc) · 2.35 KB
/
file-system.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs from "node:fs";
import { dirname, resolve } from "node:path";
import url from "node:url";
import _ from "lodash";
import { Logger } from "./logger.js";
const FILE_PREFIX = `/* eslint-disable */
/* tslint:disable */
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/
`;
class FileSystem {
/** @type {Logger} */
logger;
constructor({ logger = new Logger("file-system") } = {}) {
this.logger = logger;
}
getFileContent = (path) => {
return fs.readFileSync(path, { encoding: "utf8" });
};
readDir = (path) => {
return fs.readdirSync(path);
};
pathIsDir = (path) => {
if (!path) return false;
try {
const stat = fs.statSync(path);
return stat.isDirectory();
} catch (e) {
return false;
}
};
cropExtension = (fileName) => {
const fileNameParts = _.split(fileName, ".");
if (fileNameParts.length > 1) {
fileNameParts.pop();
}
return fileNameParts.join(".");
};
removeDir = (path) => {
try {
if (typeof fs.rmSync === "function") {
fs.rmSync(path, { recursive: true });
} else {
fs.rmdirSync(path, { recursive: true });
}
} catch (e) {
this.logger.debug("failed to remove dir", e);
}
};
createDir = (path) => {
try {
fs.mkdirSync(path, { recursive: true });
} catch (e) {
this.logger.debug("failed to create dir", e);
}
};
cleanDir = (path) => {
this.removeDir(path);
this.createDir(path);
};
pathIsExist = (path) => {
return !!path && fs.existsSync(path);
};
createFile = ({ path, fileName, content, withPrefix }) => {
const __dirname = dirname(url.fileURLToPath(import.meta.url));
const absolutePath = resolve(__dirname, path, `./${fileName}`);
const fileContent = `${withPrefix ? FILE_PREFIX : ""}${content}`;
const dirPath = dirname(absolutePath);
if (!this.pathIsExist(dirPath)) {
this.createDir(dirPath);
}
return fs.writeFileSync(absolutePath, fileContent, _.noop);
};
}
export { FileSystem };