55 lines
5.5 KiB
JavaScript
55 lines
5.5 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const graceful_fs_1 = __importDefault(require("graceful-fs"));
|
||
|
const path_1 = require("path");
|
||
|
const mkdirp_1 = __importDefault(require("mkdirp"));
|
||
|
const promisify_1 = __importDefault(require("./promisify"));
|
||
|
const delay_1 = __importDefault(require("./delay"));
|
||
|
const stat = promisify_1.default(graceful_fs_1.default.stat);
|
||
|
const readdir = promisify_1.default(graceful_fs_1.default.readdir);
|
||
|
const mkdir = promisify_1.default(mkdirp_1.default);
|
||
|
const MAX_RETRY_COUNT = 10;
|
||
|
const RETRY_DELAY = 100;
|
||
|
async function createDirectory(directoryPath) {
|
||
|
try {
|
||
|
await mkdir(directoryPath);
|
||
|
return true;
|
||
|
}
|
||
|
catch (e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
async function readDirectory(directoryPath) {
|
||
|
try {
|
||
|
await readdir(directoryPath);
|
||
|
return true;
|
||
|
}
|
||
|
catch (e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
async function ensureDirectory(directoryPath) {
|
||
|
var fileEnsured = await createDirectory(directoryPath) && await readDirectory(directoryPath);
|
||
|
for (var i = 0; i < MAX_RETRY_COUNT && !fileEnsured; i++) {
|
||
|
await delay_1.default(RETRY_DELAY);
|
||
|
fileEnsured = await createDirectory(directoryPath) && await readDirectory(directoryPath);
|
||
|
}
|
||
|
return fileEnsured;
|
||
|
}
|
||
|
async function default_1(fileName) {
|
||
|
var directoryPath = path_1.dirname(fileName);
|
||
|
try {
|
||
|
var stats = await stat(directoryPath);
|
||
|
return stats.isDirectory();
|
||
|
}
|
||
|
catch (e) {
|
||
|
if (e.code === 'ENOENT')
|
||
|
return await ensureDirectory(directoryPath);
|
||
|
return false;
|
||
|
}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