appolo-utils
Advanced tools
Comparing version 0.0.8 to 0.0.9
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const util_1 = require("util"); | ||
let readdir = util_1.promisify(fs.readdir); | ||
let stat = util_1.promisify(fs.stat); | ||
let unlink = util_1.promisify(fs.unlink); | ||
let rmdir = util_1.promisify(fs.rmdir); | ||
let exists = util_1.promisify(fs.exists); | ||
let mkdir = util_1.promisify(fs.mkdir); | ||
const fs_1 = require("fs"); | ||
class Files { | ||
static async removeDir(dirPath, removeSelf = true) { | ||
let isExists = await exists(dirPath); | ||
if (!isExists) { | ||
let status = await Files.pathStats(dirPath); | ||
if (!status || !status.isDirectory()) { | ||
return; | ||
} | ||
let status = await stat(dirPath); | ||
if (!status.isDirectory()) { | ||
return; | ||
} | ||
let files = await readdir(dirPath); | ||
let files = await fs_1.promises.readdir(dirPath); | ||
let len = files.length; | ||
@@ -34,11 +23,10 @@ if (len > 0) { | ||
if (removeSelf) { | ||
await rmdir(dirPath); | ||
await fs_1.promises.rmdir(dirPath); | ||
} | ||
} | ||
static async removeFile(filePath) { | ||
let isExists = await exists(filePath); | ||
if (!isExists) { | ||
let status = await Files.pathStats(filePath); | ||
if (!status) { | ||
return; | ||
} | ||
let status = await stat(filePath); | ||
if (status.isDirectory()) { | ||
@@ -48,11 +36,26 @@ return Files.removeDir(filePath); | ||
if (status.isFile()) { | ||
await unlink(filePath); | ||
await fs_1.promises.unlink(filePath); | ||
} | ||
} | ||
static async pathStats(filePath) { | ||
let status; | ||
try { | ||
status = await fs_1.promises.stat(filePath); | ||
return status; | ||
} | ||
catch (e) { | ||
if (e.code === "ENOENT") { | ||
return null; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
static async createDir(dirPath) { | ||
let isExists = await exists(dirPath); | ||
if (isExists) { | ||
let status = await Files.pathStats(dirPath); | ||
if (status) { | ||
return; | ||
} | ||
await mkdir(dirPath); | ||
await fs_1.promises.mkdir(dirPath, { recursive: true }); | ||
} | ||
@@ -59,0 +62,0 @@ static async reCreateDir(dirPath) { |
@@ -1,28 +0,14 @@ | ||
import * as fs from "fs"; | ||
import {promisify} from "util"; | ||
import {promises, Stats} from "fs"; | ||
let readdir = promisify(fs.readdir); | ||
let stat = promisify(fs.stat); | ||
let unlink = promisify(fs.unlink); | ||
let rmdir = promisify(fs.rmdir); | ||
let exists = promisify(fs.exists); | ||
let mkdir = promisify(fs.mkdir); | ||
export class Files { | ||
public static async removeDir(dirPath: string, removeSelf: boolean = true): Promise<void> { | ||
let isExists = await exists(dirPath); | ||
let status = await Files.pathStats(dirPath); | ||
if (!isExists) { | ||
if (!status || !status.isDirectory()) { | ||
return; | ||
} | ||
let status = await stat(dirPath); | ||
let files = await promises.readdir(dirPath); | ||
if (!status.isDirectory()) { | ||
return; | ||
} | ||
let files = await readdir(dirPath); | ||
let len = files.length; | ||
@@ -46,3 +32,3 @@ | ||
if (removeSelf) { | ||
await rmdir(dirPath); | ||
await promises.rmdir(dirPath); | ||
} | ||
@@ -53,10 +39,8 @@ } | ||
let isExists = await exists(filePath); | ||
let status = await Files.pathStats(filePath); | ||
if (!isExists) { | ||
if (!status) { | ||
return; | ||
} | ||
let status = await stat(filePath); | ||
if (status.isDirectory()) { | ||
@@ -67,15 +51,34 @@ return Files.removeDir(filePath); | ||
if (status.isFile()) { | ||
await unlink(filePath) | ||
await promises.unlink(filePath) | ||
} | ||
} | ||
public static async pathStats(filePath: string): Promise<Stats> { | ||
let status: Stats; | ||
try { | ||
status = await promises.stat(filePath); | ||
return status; | ||
} catch (e) { | ||
if (e.code === "ENOENT") { | ||
return null; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
public static async createDir(dirPath: string): Promise<void> { | ||
let isExists = await exists(dirPath); | ||
let status = await Files.pathStats(dirPath); | ||
if (isExists) { | ||
if (status) { | ||
return; | ||
} | ||
await mkdir(dirPath); | ||
await promises.mkdir(dirPath, {recursive: true}); | ||
} | ||
@@ -82,0 +85,0 @@ |
@@ -17,3 +17,3 @@ { | ||
"main": "./index.js", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"license": "MIT", | ||
@@ -20,0 +20,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35576
697