70e314295a4916e6f6c8e7feaf65075419b2ac2af7af8e78c44b18e17e0758d994c24541e1c97c544f08c9ba287f276a3e58492870b1698507b19a31242e4d 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { WriteStream, WriteStreamSync } from '@isaacs/fs-minipass';
  2. import path from 'node:path';
  3. import { list } from './list.js';
  4. import { makeCommand } from './make-command.js';
  5. import { Pack, PackSync } from './pack.js';
  6. const 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. };
  14. const 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);
  26. return promise;
  27. };
  28. const 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. };
  44. const addFilesAsync = async (p, files) => {
  45. for (let i = 0; i < files.length; i++) {
  46. const file = String(files[i]);
  47. if (file.charAt(0) === '@') {
  48. await list({
  49. file: path.resolve(String(p.cwd), file.slice(1)),
  50. noResume: true,
  51. onReadEntry: entry => {
  52. p.add(entry);
  53. },
  54. });
  55. }
  56. else {
  57. p.add(file);
  58. }
  59. }
  60. p.end();
  61. };
  62. const createSync = (opt, files) => {
  63. const p = new PackSync(opt);
  64. addFilesSync(p, files);
  65. return p;
  66. };
  67. const createAsync = (opt, files) => {
  68. const p = new Pack(opt);
  69. addFilesAsync(p, files);
  70. return p;
  71. };
  72. export const create = makeCommand(createFileSync, createFile, createSync, createAsync, (_opt, files) => {
  73. if (!files?.length) {
  74. throw new TypeError('no paths specified to add to archive');
  75. }
  76. });
  77. //# sourceMappingURL=create.js.map