UNPKG

suf-node

Version:
53 lines (52 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Exits = exports.Walk = void 0; const fs_1 = require("fs"); const path_1 = require("path"); /** * Returns an array with all the file names in the given directory */ async function Walk(dir) { return new Promise((_resolve, reject) => { let results = []; fs_1.readdir(dir, (err, list) => { if (err) return reject(err); let pending = list.length; let completed = true; if (!pending) { return _resolve(list); } list.forEach((file) => { file = path_1.resolve(dir, file); fs_1.stat(file, async (err, stat) => { if (err) return reject(err); if (stat && stat.isDirectory()) { completed = false; const a = await Walk(file); results.push(...a); if (!--pending) { return _resolve(results); } } else { results.push(file); if (!--pending && completed) { return _resolve(results); } } }); }); }); }); } exports.Walk = Walk; async function Exits(path) { return new Promise(res => { fs_1.exists(path, exists => { res(exists); }); }); } exports.Exits = Exits;