|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +/* eslint-disable no-process-exit */ |
| 6 | + |
| 7 | +/* |
| 8 | + * Dependencies. |
| 9 | + */ |
| 10 | +import { |
| 11 | + onlyArchive |
| 12 | +} from '../lib/index.mjs'; |
| 13 | +import minimist from 'minimist'; |
| 14 | +import { |
| 15 | + createRequire |
| 16 | +} from 'module'; |
| 17 | + |
| 18 | +const require = createRequire( |
| 19 | + import.meta.url); |
| 20 | +const pack = require('../package.json'); |
| 21 | + |
| 22 | +/* |
| 23 | + * Arguments. |
| 24 | + */ |
| 25 | +let argv = minimist(process.argv.slice(2)); |
| 26 | + |
| 27 | +/* |
| 28 | + * Command. |
| 29 | + */ |
| 30 | +var command = Object.keys(pack.bin)[6]; |
| 31 | + |
| 32 | +/** |
| 33 | + * Help. |
| 34 | + * |
| 35 | + * @return {string} |
| 36 | + */ |
| 37 | +function help() { |
| 38 | + return [ |
| 39 | + 'Extracts only the selected files from an archive to the current directory, or in an output directory', |
| 40 | + 'Usage: ' + command + ' [filepath] [destination] [files] ...Options', |
| 41 | + '', |
| 42 | + pack.description, |
| 43 | + '', |
| 44 | + ' [filepath] - Path to the archive.', |
| 45 | + ' [destination] - Output destination path.', |
| 46 | + ' [files] - Files in archive to extract.', |
| 47 | + '', |
| 48 | + 'Options:', |
| 49 | + '', |
| 50 | + ' -h, --help output usage information', |
| 51 | + ' -v, --version output version number', |
| 52 | + '', |
| 53 | + ' Any of these 7zip switches this command accepts:', |
| 54 | + '', |
| 55 | + ' -ai (Include archive filenames)', |
| 56 | + ' -an (Disable parsing of archive_name)', |
| 57 | + ' -ao (Overwrite mode)', |
| 58 | + ' -ax (Exclude archive filenames)', |
| 59 | + ' -i (Include filenames)', |
| 60 | + ' -m (Set compression Method)', |
| 61 | + ' -p (Set Password)', |
| 62 | + ' -r (Recurse subdirectories)', |
| 63 | + ' -si (read data from stdin)', |
| 64 | + ' -sni (Store NT security information)', |
| 65 | + ' -sns (Store NTFS alternate Streams)', |
| 66 | + ' -so (write data to stdout)', |
| 67 | + ' -spf (Use fully qualified file paths)', |
| 68 | + ' -t (set Type of archive)', |
| 69 | + ' -x (Exclude filenames)', |
| 70 | + ' -y (assume Yes on all queries)', |
| 71 | + '', |
| 72 | + 'Example:', |
| 73 | + '> ' + command + ' disc/master.7z home/support.js -y', |
| 74 | + '' |
| 75 | + ].join('\n ') + '\n'; |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | + * Program. |
| 80 | + */ |
| 81 | +if (argv.help || argv.h) { |
| 82 | + console.log(help()); |
| 83 | +} else if (argv.version || argv.v) { |
| 84 | + console.log(pack.version); |
| 85 | +} else if (argv) { |
| 86 | + let options = {}; |
| 87 | + let files = argv._; |
| 88 | + let filepath = files.shift(); |
| 89 | + destination = files.shift(); |
| 90 | + delete argv._; |
| 91 | + if (filepath && destination && files) { |
| 92 | + options = Object.assign(options, argv) |
| 93 | + console.log("Extracting files..."); |
| 94 | + onlyArchive(filepath, destination, files, options) |
| 95 | + .progress((info) => { |
| 96 | + console.log(info); |
| 97 | + }) |
| 98 | + .then(() => { |
| 99 | + console.log('Extraction of files from archive ' + filepath + ' to ' + destination + ' done!'); |
| 100 | + }) |
| 101 | + .catch((error) => { |
| 102 | + console.log('--- error:'); |
| 103 | + console.log(error); |
| 104 | + }); |
| 105 | + } else { |
| 106 | + console.log(help()); |
| 107 | + } |
| 108 | +} else { |
| 109 | + console.log(help()); |
| 110 | +} |
0 commit comments