Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
list.js110 linesDownload Raw Back to esm
1// tar -t
2import * as fsm from '@isaacs/fs-minipass';
3import fs from 'node:fs';
4import { dirname, parse } from 'path';
5import { makeCommand } from './make-command.js';
6import { Parser } from './parse.js';
7import { stripTrailingSlashes } from './strip-trailing-slashes.js';
8const onReadEntryFunction = (opt) => {
9    const onReadEntry = opt.onReadEntry;
10    opt.onReadEntry =
11        onReadEntry ?
12            e => {
13                onReadEntry(e);
14                e.resume();
15            }
16            : e => e.resume();
17};
18// construct a filter that limits the file entries listed
19// include child entries if a dir is included
20export const filesFilter = (opt, files) => {
21    const map = new Map(files.map(f => [stripTrailingSlashes(f), true]));
22    const filter = opt.filter;
23    const mapHas = (file, r = '') => {
24        const root = r || parse(file).root || '.';
25        let ret;
26        if (file === root)
27            ret = false;
28        else {
29            const m = map.get(file);
30            if (m !== undefined) {
31                ret = m;
32            }
33            else {
34                ret = mapHas(dirname(file), root);
35            }
36        }
37        map.set(file, ret);
38        return ret;
39    };
40    opt.filter =
41        filter ?
42            (file, entry) => filter(file, entry) && mapHas(stripTrailingSlashes(file))
43            : file => mapHas(stripTrailingSlashes(file));
44};
45const listFileSync = (opt) => {
46    const p = new Parser(opt);
47    const file = opt.file;
48    let fd;
49    try {
50        fd = fs.openSync(file, 'r');
51        const stat = fs.fstatSync(fd);
52        const readSize = opt.maxReadSize || 16 * 1024 * 1024;
53        if (stat.size < readSize) {
54            const buf = Buffer.allocUnsafe(stat.size);
55            const read = fs.readSync(fd, buf, 0, stat.size, 0);
56            p.end(read === buf.byteLength ? buf : buf.subarray(0, read));
57        }
58        else {
59            let pos = 0;
60            const buf = Buffer.allocUnsafe(readSize);
61            while (pos < stat.size) {
62                const bytesRead = fs.readSync(fd, buf, 0, readSize, pos);
63                if (bytesRead === 0)
64                    break;
65                pos += bytesRead;
66                p.write(buf.subarray(0, bytesRead));
67            }
68            p.end();
69        }
70    }
71    finally {
72        if (typeof fd === 'number') {
73            try {
74                fs.closeSync(fd);
75                /* c8 ignore next */
76            }
77            catch (er) { }
78        }
79    }
80};
81const listFile = (opt, _files) => {
82    const parse = new Parser(opt);
83    const readSize = opt.maxReadSize || 16 * 1024 * 1024;
84    const file = opt.file;
85    const p = new Promise((resolve, reject) => {
86        parse.on('error', reject);
87        parse.on('end', resolve);
88        fs.stat(file, (er, stat) => {
89            if (er) {
90                reject(er);
91            }
92            else {
93                const stream = new fsm.ReadStream(file, {
94                    readSize: readSize,
95                    size: stat.size,
96                });
97                stream.on('error', reject);
98                stream.pipe(parse);
99            }
100        });
101    });
102    return p;
103};
104export const list = makeCommand(listFileSync, listFile, opt => new Parser(opt), opt => new Parser(opt), (opt, files) => {
105    if (files?.length)
106        filesFilter(opt, files);
107    if (!opt.noResume)
108        onReadEntryFunction(opt);
109});
110//# sourceMappingURL=list.js.map
codekingpro/portable-devtools · Team Ai