-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·117 lines (100 loc) · 2.59 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const getStdin = require('get-stdin');
const globby = require('globby');
const meow = require('meow');
const ora = require('ora');
const cli = meow({
help: `
Usage
$ archiverjs <path|glob> --out-file=output.zip [--format=zip|tar|json] [--compression-method=deflate|store]
Options
-f, --archive-format Set the archive format.
Archive format can be set to:
zip - This is the default format
tar - just your everyday tarball
json - great for debugging
-o, --out-file Output file
-Z, --compression-method Set the default compression method. Currently the main methods supported by zip are store and deflate.
Compression method can be set to:
deflate - This is the default method for zip
store - Setting the compression method to store forces zip to store entries with no compression
Examples
$ archiverjs images/* --out-file=images.zip
`,
autoVersion: process.argv.indexOf('--no-auto-version') === -1,
autoHelp: process.argv.indexOf('--no-auto-help') === -1,
flags: {
archiveFormat: {
type: 'string',
alias: 'f',
default: 'zip'
},
compressionMethod: {
type: 'string',
alias: 'Z'
},
outFile: {
type: 'string',
alias: 'o'
}
}
});
var archive;
var archiveOptions = {};
const handleFile = function(input, opts) {
archive.file(input);
return Promise.resolve();
};
const run = function(input, opts) {
opts = Object.assign({ compressionMethod: 'deflate' }, opts);
const spinner = ora('Archiving');
if (Buffer.isBuffer(input)) {
return;
}
if (opts.outFile) {
spinner.start();
}
if (opts.compressionMethod === 'store') {
archiveOptions.store = true;
}
var output = fs.createWriteStream(opts.outFile);
archive = archiver(opts.archiveFormat, archiveOptions);
archive.on('error', function(err) {
spinner.stop();
throw err;
});
archive.pipe(output);
input.push('!' + opts.outFile);
globby(input, {onlyFiles: true})
.then(function(paths) {
return Promise.all(paths.map(function(filepath) {
return handleFile(filepath);
}));
})
.then(function() {
return archive.finalize();
})
.then(function() {
spinner.stop();
console.log(`Archive completed`);
})
.catch(function(err) {
spinner.stop();
throw err;
});
};
if (!cli.input.length && process.stdin.isTTY) {
console.error('Specify at least one filename');
process.exit(1);
}
if (cli.input.length) {
run(cli.input, cli.flags);
} else {
getStdin.buffer().then(function(buf) {
return run(buf, cli.flags);
});
}