@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
49 lines (48 loc) • 1.4 kB
JavaScript
import { __decorate } from "tslib";
import { Injectable } from '@nestjs/common';
import * as fs from 'fs';
import { join, parse, resolve, sep } from 'path';
let FileSystemHelper = class FileSystemHelper {
async writeFile(path, content) {
try {
await fs.promises.writeFile(path, content);
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
await this.mkdirRecursive(path);
await fs.promises.writeFile(path, content);
}
}
async mkdirRecursive(path) {
for (const dir of this.getDirs(path)) {
try {
await fs.promises.mkdir(dir);
}
catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
}
}
getDirs(path) {
const parsedPath = parse(resolve(path));
const chunks = parsedPath.dir.split(sep);
if (parsedPath.root === '/') {
chunks[0] = `/${chunks[0]}`;
}
const dirs = new Array();
chunks.reduce((previous, next) => {
const directory = join(previous, next);
dirs.push(directory);
return join(directory);
});
return dirs;
}
};
FileSystemHelper = __decorate([
Injectable()
], FileSystemHelper);
export { FileSystemHelper };