codekingpro/portable-devtools
115k
1import { WriteStream, WriteStreamSync } from '@isaacs/fs-minipass';
2import path from 'node:path';
3import { list } from './list.js';
4import { makeCommand } from './make-command.js';
5import { Pack, PackSync } from './pack.js';
6const createFileSync = (opt, files) => {
7 const p = new PackSync(opt);
8 const stream = new WriteStreamSync(opt.file, {
9 mode: opt.mode || 0o666,
10 });
11 p.pipe(stream);
12 addFilesSync(p, files);
13};
14const createFile = (opt, files) => {
15 const p = new Pack(opt);
16 const stream = new WriteStream(opt.file, {
17 mode: opt.mode || 0o666,
18 });
19 p.pipe(stream);
20 const promise = new Promise((res, rej) => {
21 stream.on('error', rej);
22 stream.on('close', res);
23 p.on('error', rej);
24 });
25 addFilesAsync(p, files).catch(er => p.emit('error', er));
26 return promise;
27};
28const addFilesSync = (p, files) => {
29 files.forEach(file => {
30 if (file.charAt(0) === '@') {
31 list({
32 file: path.resolve(p.cwd, file.slice(1)),
33 sync: true,
34 noResume: true,
35 onReadEntry: entry => p.add(entry),
36 });
37 }
38 else {
39 p.add(file);
40 }
41 });
42 p.end();
43};
44const addFilesAsync = async (p, files) => {
45 for (const file of files) {
46 if (file.charAt(0) === '@') {
47 await list({
48 file: path.resolve(String(p.cwd), file.slice(1)),
49 noResume: true,
50 onReadEntry: entry => {
51 p.add(entry);
52 },
53 });
54 }
55 else {
56 p.add(file);
57 }
58 }
59 p.end();
60};
61const createSync = (opt, files) => {
62 const p = new PackSync(opt);
63 addFilesSync(p, files);
64 return p;
65};
66const createAsync = (opt, files) => {
67 const p = new Pack(opt);
68 addFilesAsync(p, files).catch(er => p.emit('error', er));
69 return p;
70};
71export const create = makeCommand(createFileSync, createFile, createSync, createAsync, (_opt, files) => {
72 if (!files?.length) {
73 throw new TypeError('no paths specified to add to archive');
74 }
75});
76//# sourceMappingURL=create.js.map