UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.FileSystemHelper = void 0;
4const tslib_1 = require("tslib");
5const common_1 = require("@nestjs/common");
6const fs = require("fs");
7const path_1 = require("path");
8let FileSystemHelper = class FileSystemHelper {
9 async writeFile(path, content) {
10 try {
11 await fs.promises.writeFile(path, content);
12 }
13 catch (err) {
14 if (err.code !== 'ENOENT') {
15 throw err;
16 }
17 await this.mkdirRecursive(path);
18 await fs.promises.writeFile(path, content);
19 }
20 }
21 async mkdirRecursive(path) {
22 for (const dir of this.getDirs(path)) {
23 try {
24 await fs.promises.mkdir(dir);
25 }
26 catch (err) {
27 if (err.code !== 'EEXIST') {
28 throw err;
29 }
30 }
31 }
32 }
33 getDirs(path) {
34 const parsedPath = (0, path_1.parse)((0, path_1.resolve)(path));
35 const chunks = parsedPath.dir.split(path_1.sep);
36 if (parsedPath.root === '/') {
37 chunks[0] = `/${chunks[0]}`;
38 }
39 const dirs = new Array();
40 chunks.reduce((previous, next) => {
41 const directory = (0, path_1.join)(previous, next);
42 dirs.push(directory);
43 return (0, path_1.join)(directory);
44 });
45 return dirs;
46 }
47};
48FileSystemHelper = tslib_1.__decorate([
49 (0, common_1.Injectable)()
50], FileSystemHelper);
51exports.FileSystemHelper = FileSystemHelper;