deepl-node
Version:
deepl-node is the official DeepL Node.js client library
78 lines (77 loc) • 3.13 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FsHelper = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
* This class is necessary because some fs library methods and/or params are not available in older versions of node, such as v12
*
* Docs for v12: https://nodejs.org/docs/latest-v12.x/api/fs.html#fspromisesreaddirpath-options
*/
class FsHelper {
static readdirSyncRecursive(filepath) {
if (!fs.existsSync(filepath)) {
throw new Error(`Error: no such file or directory, ${filepath}`);
}
if (!fs.statSync(filepath).isDirectory()) {
throw new Error(`Error: not a directory, ${filepath}`);
}
const results = [];
const filesAndDirs = fs.readdirSync(filepath);
filesAndDirs.forEach((fileOrDir) => {
const isDir = fs.statSync(path.join(filepath, fileOrDir)).isDirectory();
if (isDir) {
const dir = fileOrDir;
const dirList = this.readdirSyncRecursive(path.join(filepath, fileOrDir));
const subList = dirList.map((subpath) => `${dir}/${subpath}`);
results.push(dir, ...subList);
}
else {
const file = fileOrDir;
results.push(file);
}
});
return results;
}
static removeSyncRecursive(filepath) {
if (!fs.existsSync(filepath)) {
throw new Error(`Error: no such file or directory, ${filepath}`);
}
const stat = fs.statSync(filepath);
if (!stat.isDirectory()) {
fs.unlinkSync(filepath);
}
else {
// Note: it's okay to use the native readdirSync here because we do not need the recursive functionality
const filesAndDirs = fs.readdirSync(filepath);
filesAndDirs.forEach((file) => {
this.removeSyncRecursive(path.join(filepath, file));
});
fs.rmdirSync(filepath);
}
}
}
exports.FsHelper = FsHelper;
;