source: trip-planner-front/node_modules/tar/lib/create.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[6a3a178]1'use strict'
2
3// tar -c
4const hlo = require('./high-level-opt.js')
5
6const Pack = require('./pack.js')
7const fsm = require('fs-minipass')
8const t = require('./list.js')
9const path = require('path')
10
11module.exports = (opt_, files, cb) => {
12 if (typeof files === 'function')
13 cb = files
14
15 if (Array.isArray(opt_))
16 files = opt_, opt_ = {}
17
18 if (!files || !Array.isArray(files) || !files.length)
19 throw new TypeError('no files or directories specified')
20
21 files = Array.from(files)
22
23 const opt = hlo(opt_)
24
25 if (opt.sync && typeof cb === 'function')
26 throw new TypeError('callback not supported for sync tar functions')
27
28 if (!opt.file && typeof cb === 'function')
29 throw new TypeError('callback only supported with file option')
30
31 return opt.file && opt.sync ? createFileSync(opt, files)
32 : opt.file ? createFile(opt, files, cb)
33 : opt.sync ? createSync(opt, files)
34 : create(opt, files)
35}
36
37const createFileSync = (opt, files) => {
38 const p = new Pack.Sync(opt)
39 const stream = new fsm.WriteStreamSync(opt.file, {
40 mode: opt.mode || 0o666,
41 })
42 p.pipe(stream)
43 addFilesSync(p, files)
44}
45
46const createFile = (opt, files, cb) => {
47 const p = new Pack(opt)
48 const stream = new fsm.WriteStream(opt.file, {
49 mode: opt.mode || 0o666,
50 })
51 p.pipe(stream)
52
53 const promise = new Promise((res, rej) => {
54 stream.on('error', rej)
55 stream.on('close', res)
56 p.on('error', rej)
57 })
58
59 addFilesAsync(p, files)
60
61 return cb ? promise.then(cb, cb) : promise
62}
63
64const addFilesSync = (p, files) => {
65 files.forEach(file => {
66 if (file.charAt(0) === '@') {
67 t({
68 file: path.resolve(p.cwd, file.substr(1)),
69 sync: true,
70 noResume: true,
71 onentry: entry => p.add(entry),
72 })
73 } else
74 p.add(file)
75 })
76 p.end()
77}
78
79const addFilesAsync = (p, files) => {
80 while (files.length) {
81 const file = files.shift()
82 if (file.charAt(0) === '@') {
83 return t({
84 file: path.resolve(p.cwd, file.substr(1)),
85 noResume: true,
86 onentry: entry => p.add(entry),
87 }).then(_ => addFilesAsync(p, files))
88 } else
89 p.add(file)
90 }
91 p.end()
92}
93
94const createSync = (opt, files) => {
95 const p = new Pack.Sync(opt)
96 addFilesSync(p, files)
97 return p
98}
99
100const create = (opt, files) => {
101 const p = new Pack(opt)
102 addFilesAsync(p, files)
103 return p
104}
Note: See TracBrowser for help on using the repository browser.