dir-archiver 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +11 -29
- package/index.js +105 -103
- package/package.json +13 -3
package/cli.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const DirArchiver = require( './index' );
|
|
4
|
+
const parseArgs = require( 'argv-flags' );
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var excludes = [];
|
|
6
|
+
const directoryPath = parseArgs( '--src', 'string' );
|
|
7
|
+
const zipPath = parseArgs( '--dest', 'string' );
|
|
8
|
+
const includeBaseDirectory = parseArgs( '--includebasedir', 'boolean' );
|
|
9
|
+
const excludes = parseArgs( '--exclude', 'array' ) || [];
|
|
10
10
|
|
|
11
|
-
if (
|
|
12
|
-
|
|
11
|
+
if ( directoryPath === false || zipPath === false ) {
|
|
12
|
+
console.log( ` Dir Archiver could not be executed. Some arguments are missing.
|
|
13
13
|
|
|
14
14
|
Options:
|
|
15
15
|
--src The path of the folder to archive. [string][required]
|
|
@@ -20,27 +20,9 @@ if ( ! arguments.includes( '--src' ) || ! arguments.includes( '--dest' ) ) {
|
|
|
20
20
|
an archive that includes this base directory.
|
|
21
21
|
If this option is set to false the archive created will
|
|
22
22
|
unzip its content to the current directory. [bool]
|
|
23
|
-
--exclude A list with the names of the files and folders to exclude. [array]`);
|
|
24
|
-
|
|
23
|
+
--exclude A list with the names of the files and folders to exclude. [array]` );
|
|
24
|
+
process.exit(); // eslint-disable-line n/no-process-exit
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
if( arguments[argumentIndex] === '--src' ) {
|
|
29
|
-
directoryPath = arguments[parseInt(argumentIndex) + 1];
|
|
30
|
-
}
|
|
31
|
-
if( arguments[argumentIndex] === '--dest' ) {
|
|
32
|
-
zipPath = arguments[parseInt(argumentIndex) + 1];
|
|
33
|
-
}
|
|
34
|
-
if( arguments[argumentIndex] === '--includebasedir' ) {
|
|
35
|
-
includeBaseDirectory = ( arguments[parseInt(argumentIndex) + 1] === 'true' );
|
|
36
|
-
}
|
|
37
|
-
if( afterExclude === true ) {
|
|
38
|
-
excludes.push( arguments[argumentIndex] );
|
|
39
|
-
}
|
|
40
|
-
if( arguments[argumentIndex] === '--exclude' ) {
|
|
41
|
-
var afterExclude = true;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const archive = new DirArchiver(directoryPath, zipPath, includeBaseDirectory, excludes);
|
|
27
|
+
const archive = new DirArchiver( directoryPath, zipPath, includeBaseDirectory, excludes );
|
|
46
28
|
archive.createZip();
|
package/index.js
CHANGED
|
@@ -5,108 +5,110 @@ const fs = require( 'fs' );
|
|
|
5
5
|
const archiver = require( 'archiver' );
|
|
6
6
|
|
|
7
7
|
class DirArchiver {
|
|
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
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The constructor.
|
|
10
|
+
* @param {string} directoryPath - the path of the folder to archive.
|
|
11
|
+
* @param {string} zipPath - The path of the zip file to create.
|
|
12
|
+
* @param {Boolean} includeBaseDirectory - Includes a base directory at the root of the archive. For example, if the root folder of your project is named "your-project", setting includeBaseDirectory to true will create an archive that includes this base directory. If this option is set to false the archive created will unzip its content to the current directory.
|
|
13
|
+
* @param {array} excludes - The name of the files and foldes to exclude.
|
|
14
|
+
*/
|
|
15
|
+
constructor( directoryPath, zipPath, includeBaseDirectory, excludes ) {
|
|
16
|
+
|
|
17
|
+
// Contains the excluded files and folders.
|
|
18
|
+
this.excludes = excludes.map( ( element ) => {
|
|
19
|
+
return path.normalize( element );
|
|
20
|
+
} );
|
|
21
|
+
|
|
22
|
+
this.directoryPath = path.resolve( directoryPath );
|
|
23
|
+
|
|
24
|
+
this.zipPath = path.resolve( zipPath );
|
|
25
|
+
|
|
26
|
+
this.includeBaseDirectory = includeBaseDirectory;
|
|
27
|
+
|
|
28
|
+
this.baseDirectory = path.basename( this.directoryPath );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Recursively traverse the directory tree and append the files to the archive.
|
|
33
|
+
* @param {string} directoryPath - The path of the directory being looped through.
|
|
34
|
+
*/
|
|
35
|
+
traverseDirectoryTree( directoryPath ) {
|
|
36
|
+
const files = fs.readdirSync( directoryPath );
|
|
37
|
+
for ( const i in files ) {
|
|
38
|
+
const currentPath = path.join( path.resolve( directoryPath ), files[ i ] );
|
|
39
|
+
const stats = fs.statSync( currentPath );
|
|
40
|
+
let relativePath = path.relative( this.directoryPath, currentPath );
|
|
41
|
+
if ( stats.isFile() && ! this.excludes.includes( relativePath ) ) {
|
|
42
|
+
if ( this.includeBaseDirectory === true ) {
|
|
43
|
+
this.archive.file( currentPath, {
|
|
44
|
+
name: path.join( this.baseDirectory, relativePath )
|
|
45
|
+
} );
|
|
46
|
+
} else {
|
|
47
|
+
this.archive.file( currentPath, {
|
|
48
|
+
name: relativePath
|
|
49
|
+
} );
|
|
50
|
+
}
|
|
51
|
+
} else if ( stats.isDirectory() && ! this.excludes.includes( relativePath ) ) {
|
|
52
|
+
this.traverseDirectoryTree( currentPath );
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
prettyBytes( bytes ) {
|
|
58
|
+
if ( bytes > 1000 && bytes < 1000000 ) {
|
|
59
|
+
return Math.round( ( ( bytes / 1000 ) + Number.EPSILON ) * 100 ) / 100 + ' KB';
|
|
60
|
+
}
|
|
61
|
+
if ( bytes > 1000000 && bytes < 1000000000 ) {
|
|
62
|
+
return Math.round( ( ( bytes / 1000000 ) + Number.EPSILON ) * 100 ) / 100 + ' MB';
|
|
63
|
+
}
|
|
64
|
+
if ( bytes > 1000000000 ) {
|
|
65
|
+
return Math.round( ( ( bytes / 1000000000 ) + Number.EPSILON ) * 100 ) / 100 + ' GB';
|
|
66
|
+
}
|
|
67
|
+
return bytes + ' bytes';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
createZip () {
|
|
71
|
+
// Remove the destination zip if it exists.
|
|
72
|
+
// see : https://github.com/Ismail-elkorchi/dir-archiver/issues/5
|
|
73
|
+
if ( fs.existsSync( this.zipPath ) ) {
|
|
74
|
+
fs.unlinkSync( this.zipPath );
|
|
75
|
+
}
|
|
76
|
+
// Create a file to stream archive data to.
|
|
77
|
+
this.output = fs.createWriteStream( this.zipPath );
|
|
78
|
+
this.archive = archiver( 'zip', {
|
|
79
|
+
zlib: { level: 9 }
|
|
80
|
+
} );
|
|
81
|
+
|
|
82
|
+
// Catch warnings during archiving.
|
|
83
|
+
this.archive.on( 'warning', function( err ) {
|
|
84
|
+
if ( err.code === 'ENOENT' ) {
|
|
85
|
+
// log warning
|
|
86
|
+
console.log( err );
|
|
87
|
+
} else {
|
|
88
|
+
// throw error
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
} );
|
|
92
|
+
|
|
93
|
+
// Catch errors during archiving.
|
|
94
|
+
this.archive.on( 'error', function( err ) {
|
|
95
|
+
throw err;
|
|
96
|
+
} );
|
|
97
|
+
|
|
98
|
+
// Pipe archive data to the file.
|
|
99
|
+
this.archive.pipe( this.output );
|
|
100
|
+
|
|
101
|
+
// Recursively traverse the directory tree and append the files to the archive.
|
|
102
|
+
this.traverseDirectoryTree( this.directoryPath );
|
|
103
|
+
|
|
104
|
+
// Finalize the archive.
|
|
105
|
+
this.archive.finalize();
|
|
106
|
+
|
|
107
|
+
const self = this;
|
|
108
|
+
// Listen for all archive data to be written.
|
|
109
|
+
this.output.on( 'close', function () {
|
|
110
|
+
console.log( `Created ${self.zipPath} of ${self.prettyBytes( self.archive.pointer() )}` );
|
|
111
|
+
} );
|
|
112
|
+
}
|
|
111
113
|
}
|
|
112
114
|
module.exports = DirArchiver;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dir-archiver",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Compress a whole directory (including subdirectories) into a zip file, with options to exclude specific files, or directories.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "index.js",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"LICENSE",
|
|
12
|
-
"index.js"
|
|
12
|
+
"index.js",
|
|
13
|
+
"cli.js"
|
|
13
14
|
],
|
|
14
15
|
"keywords": [
|
|
15
16
|
"zip",
|
|
@@ -31,7 +32,16 @@
|
|
|
31
32
|
},
|
|
32
33
|
"bugs": "https://github.com/Ismail-elkorchi/dir-archiver/issues",
|
|
33
34
|
"homepage": "https://github.com/Ismail-elkorchi/dir-archiver",
|
|
35
|
+
"scripts": {
|
|
36
|
+
"lint": "eslint cli.js index.js",
|
|
37
|
+
"lint:fix": "eslint --fix cli.js index.js"
|
|
38
|
+
},
|
|
34
39
|
"dependencies": {
|
|
35
|
-
"archiver": "^5.3.1"
|
|
40
|
+
"archiver": "^5.3.1",
|
|
41
|
+
"argv-flags": "^0.1.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"eslint": "^8.24.0",
|
|
45
|
+
"eslint-plugin-n": "^15.3.0"
|
|
36
46
|
}
|
|
37
47
|
}
|