-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (45 loc) · 1.17 KB
/
index.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
#!/usr/bin/env node
const inquirer = require('inquirer');
const AdmZip = require('adm-zip');
const fs = require('fs');
const path = require('path');
async function main() {
const options = ['Extract', 'Compress'];
const { choice }= await inquirer.prompt([
{
type: 'list',
name: 'choice',
message: 'What do you want to do?',
choices: options,
}
]);
const dirContents = fs.readdirSync('.');
const { filename } = await inquirer.prompt([
{
type: 'list',
name: 'filename',
message: `Select a file to ${choice}`,
choices: dirContents,
}
]);
switch (choice) {
case 'Extract': extract(filename); break;
case 'Compress': compress(filename); break;
}
}
function extract(filename) {
const { name } = path.parse(filename);
const outFilename = `${name}_extracted`;
const zip = new AdmZip(`./${filename}`);
zip.extractAllTo(`./${outFilename}`, true);
console.log('Extraction completed');
}
function compress(filename) {
const { name } = path.parse(filename);
const outFilename = `${name}.zip`;
const zip = new AdmZip();
zip.addLocalFolder(`./${filename}`);
zip.writeZip(`./${outFilename}`);
console.log('Compression completed');
}
main();