UNPKG

fs-match

Version:

A NodeJS package for finding folders, files & apps by Regular expression based on Asynchronous iteration of ES 9 & TypeScript

140 lines (139 loc) 5.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.traverse = traverse; exports.filter = filter; exports.which = which; const tslib_1 = require("tslib"); const child_process_1 = require("child_process"); const fs_extra_1 = require("fs-extra"); const path_1 = require("path"); const windows_1 = require("./windows"); const { platform, env } = process; /** * Traverse File-system * * @param path - Root path to traverse */ function traverse(path) { return tslib_1.__asyncGenerator(this, arguments, function* traverse_1() { if (path === '/' && platform === 'win32') { for (const disk of (0, windows_1.getPartition)()) yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(traverse(disk)))); return yield tslib_1.__await(void 0); } for (const node of yield tslib_1.__await((0, fs_extra_1.readdir)(path, { withFileTypes: true }))) { const fullPath = (0, path_1.join)(path, node.name); yield yield tslib_1.__await(fullPath); try { if (node.isDirectory()) yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(traverse(fullPath)))); } catch (error) { switch (error.code) { case 'ENOENT': case 'EPERM': case 'EACCES': case 'EBUSY': case 'ELOOP': case 'UNKNOWN': continue; } throw error; } } }); } /** * Iterator filter * * @param iterator * @param pattern String pattern to match * @param count Result count */ function filter(iterator_1, pattern_1) { return tslib_1.__asyncGenerator(this, arguments, function* filter_1(iterator, pattern, count = Infinity) { var _a, e_1, _b, _c; let index = 0; count = ~~count || Infinity; if (pattern && !(pattern instanceof RegExp)) pattern = RegExp(pattern + '', 'i'); try { for (var _d = true, iterator_2 = tslib_1.__asyncValues(iterator), iterator_2_1; iterator_2_1 = yield tslib_1.__await(iterator_2.next()), _a = iterator_2_1.done, !_a; _d = true) { _c = iterator_2_1.value; _d = false; const item = _c; if (!pattern || pattern.test(item)) if (index++ < count) yield yield tslib_1.__await(item); else break; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = iterator_2.return)) yield tslib_1.__await(_b.call(iterator_2)); } finally { if (e_1) throw e_1.error; } } }); } function $which(name) { try { return ((0, child_process_1.execSync)(`which ${name}`) + '').trim(); } catch (error) { if (!('' + error.stdout + error.stderr).trim()) return ''; } } const MacAppPath = ['/Applications', `${env.HOME}/Applications`].filter(fs_extra_1.existsSync); /** * @param names - Names (without extension name) of Executable files */ function which(...names) { return tslib_1.__asyncGenerator(this, arguments, function* which_1() { var _a, e_2, _b, _c; const appRoots = platform === 'win32' ? (0, windows_1.getAppFolder)() : platform === 'darwin' ? MacAppPath : ['/opt'], appPatterns = platform === 'win32' ? names.map(name => RegExp(String.raw `\\${name}\.exe$`, 'i')) : platform === 'darwin' ? names.map(name => RegExp(String.raw `\.app/Contents/MacOS/(\w+\W)?${name}$`, 'i')) : names.map(name => RegExp(`/${name}$`, 'i')), noPathCommandPatterns = []; if (platform === 'win32') noPathCommandPatterns.push(...appPatterns); else for (const [index, name] of Object.entries(names)) { const path = $which(name); if (path) yield yield tslib_1.__await(path); else noPathCommandPatterns.push(appPatterns[index]); } for (const root of appRoots) try { for (var _d = true, _e = (e_2 = void 0, tslib_1.__asyncValues(traverse(root))), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const file = _c; if (!noPathCommandPatterns[0]) return yield tslib_1.__await(void 0); const index = noPathCommandPatterns.findIndex(pattern => pattern.test(file)); if (index < 0 || !(0, fs_extra_1.statSync)(file).isFile()) continue; noPathCommandPatterns.splice(index, 1); yield yield tslib_1.__await(file); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e)); } finally { if (e_2) throw e_2.error; } } }); }