dir-archiver 1.2.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.
Files changed (5) hide show
  1. package/README.md +16 -5
  2. package/cli.js +19 -27
  3. package/index.js +105 -92
  4. package/package.json +13 -3
  5. package/CHANGELOG.md +0 -28
package/README.md CHANGED
@@ -25,9 +25,14 @@ const excludes = ['directory_name', 'file.extension'];
25
25
  * Create a dir-archiver object.
26
26
  * @param {string} directoryPath - The path of the folder to archive.
27
27
  * @param {string} zipPath - The path of the zip file to create.
28
+ * @param {Boolean} includeBaseDirectory - Includes a base directory at the root of the archive.
29
+ * For example, if the root folder of your project is named "your-project", setting
30
+ * includeBaseDirectory to true will create an archive that includes this base directory.
31
+ * If this option is set to false the archive created will unzip its content to
32
+ * the current directory.
28
33
  * @param {array} excludes - A list with the names of the files and folders to exclude.
29
34
  */
30
- var archive = new DirArchiver('path/to/directory', 'path/to/desination/zipfile.zip', excludes);
35
+ var archive = new DirArchiver('path/to/directory', 'path/to/desination/zipfile.zip', true, excludes);
31
36
 
32
37
  // Create the zip file.
33
38
  archive.createZip();
@@ -35,12 +40,18 @@ archive.createZip();
35
40
  ## Command Line Interface
36
41
 
37
42
  ```sh
38
- Usage: dir-archiver --src <path-to-directory> --dest <path-to-file>.zip --exclude folder-name file-name.extention
43
+ Usage: dir-archiver --src <path-to-directory> --dest <path-to-file>.zip --includebasedir true|false --exclude folder-name file-name.extention
39
44
 
40
45
  Options:
41
- --src The path of the folder to archive.
42
- --dest The path of the zip file to create.
43
- --exclude Specify a list with the names of the files and folders to exclude
46
+ --src The path of the folder to archive. [string][required]
47
+ --dest The path of the zip file to create. [string][required]
48
+ --includebasedir Includes a base directory at the root of the archive.
49
+ For example, if the root folder of your project is named
50
+ "your-project", setting this option to true will create
51
+ an archive that includes this base directory.
52
+ If this option is set to false the archive created will
53
+ unzip its content to the current directory. [bool]
54
+ --exclude A list with the names of the files and folders to exclude. [array]
44
55
  ```
45
56
 
46
57
 
package/cli.js CHANGED
@@ -1,36 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var DirArchiver = require('./index');
3
+ const DirArchiver = require( './index' );
4
+ const parseArgs = require( 'argv-flags' );
4
5
 
5
- const arguments = process.argv;
6
- var directoryPath = '';
7
- var zipPath = '';
8
- 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' ) || [];
9
10
 
10
- if ( ! arguments.includes( '--src' ) || ! arguments.includes( '--dest' ) ) {
11
- console.log(` Dir Archiver could not be executed. Some arguments are missing.
11
+ if ( directoryPath === false || zipPath === false ) {
12
+ console.log( ` Dir Archiver could not be executed. Some arguments are missing.
12
13
 
13
14
  Options:
14
- --src The path of the folder to archive. [string][required]
15
- --dest The path of the zip file to create. [string][required]
16
- --exclude A list with the names of the files and folders to exclude. [array]`);
17
- process.exit();
15
+ --src The path of the folder to archive. [string][required]
16
+ --dest The path of the zip file to create. [string][required]
17
+ --includebasedir Includes a base directory at the root of the archive.
18
+ For example, if the root folder of your project is named
19
+ "your-project", setting this option to true will create
20
+ an archive that includes this base directory.
21
+ If this option is set to false the archive created will
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
+ process.exit(); // eslint-disable-line n/no-process-exit
18
25
  }
19
26
 
20
- for ( argumentIndex in arguments ) {
21
- if( arguments[argumentIndex] === '--src' ) {
22
- directoryPath = arguments[parseInt(argumentIndex) + 1];
23
- }
24
- if( arguments[argumentIndex] === '--dest' ) {
25
- zipPath = arguments[parseInt(argumentIndex) + 1];
26
- }
27
- if( afterExclude === true ) {
28
- excludes.push( arguments[argumentIndex] );
29
- }
30
- if( arguments[argumentIndex] === '--exclude' ) {
31
- var afterExclude = true;
32
- }
33
- }
34
-
35
- const archive = new DirArchiver(directoryPath, zipPath, excludes);
27
+ const archive = new DirArchiver( directoryPath, zipPath, includeBaseDirectory, excludes );
36
28
  archive.createZip();
package/index.js CHANGED
@@ -5,97 +5,110 @@ const fs = require( 'fs' );
5
5
  const archiver = require( 'archiver' );
6
6
 
7
7
  class DirArchiver {
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 {array} excludes - The name of the files and foldes to exclude.
13
- */
14
- constructor(directoryPath, zipPath, excludes){
15
-
16
- // Contains the excluded files and folders.
17
- this.excludes = excludes;
18
-
19
- this.directoryPath = directoryPath;
20
-
21
- this.zipPath = zipPath;
22
- }
23
-
24
- /**
25
- * Recursively traverse the directory tree and append the files to the archive.
26
- * @param {string} directoryPath - The path of the directory being looped through.
27
- */
28
- traverseDirectoryTree( directoryPath ) {
29
- const files = fs.readdirSync( directoryPath );
30
- for ( const i in files ) {
31
- const currentPath = directoryPath + '/' + files[i];
32
- const stats = fs.statSync( currentPath );
33
- let relativePath = path.relative(process.cwd(), currentPath);
34
- if ( stats.isFile() && ! this.excludes.includes( relativePath ) ) {
35
- this.archive.file(currentPath, {
36
- name: `${relativePath}`
37
- });
38
- } else if ( stats.isDirectory() && ! this.excludes.includes( relativePath ) ) {
39
- this.traverseDirectoryTree( currentPath );
40
- }
41
- }
42
- }
43
-
44
- prettyBytes( bytes ) {
45
- if( bytes > 1000 && bytes < 1000000 ) {
46
- return Math.round( ( ( bytes / 1000 ) + Number.EPSILON ) * 100 ) / 100 + ' KB';
47
- }
48
- if( bytes > 1000000 && bytes < 1000000000 ) {
49
- return Math.round( ( ( bytes / 1000000 ) + Number.EPSILON ) * 100 ) / 100 + ' MB';
50
- }
51
- if( bytes > 1000000000 ) {
52
- return Math.round( ( ( bytes / 1000000000 ) + Number.EPSILON ) * 100 ) / 100 + ' GB';
53
- }
54
- return bytes + ' bytes';
55
- }
56
-
57
- createZip (){
58
- // Remove the destination zip if it exists.
59
- // see : https://github.com/Ismail-elkorchi/dir-archiver/issues/5
60
- if(fs.existsSync(this.zipPath)){
61
- fs.unlinkSync(this.zipPath);
62
- }
63
- // Create a file to stream archive data to.
64
- this.output = fs.createWriteStream( this.zipPath );
65
- this.archive = archiver('zip', {
66
- zlib: { level: 9 }
67
- });
68
-
69
- // Catch warnings during archiving.
70
- this.archive.on( 'warning', function( err ) {
71
- if ( err.code === 'ENOENT' ) {
72
- // log warning
73
- console.log(err);
74
- } else {
75
- // throw error
76
- throw err;
77
- }
78
- });
79
-
80
- // Catch errors during archiving.
81
- this.archive.on( 'error', function( err ){
82
- throw err;
83
- });
84
-
85
- // Pipe archive data to the file.
86
- this.archive.pipe( this.output );
87
-
88
- // Recursively traverse the directory tree and append the files to the archive.
89
- this.traverseDirectoryTree( this.directoryPath );
90
-
91
- // Finalize the archive.
92
- this.archive.finalize();
93
-
94
- const self = this;
95
- // Listen for all archive data to be written.
96
- this.output.on( 'close', function () {
97
- console.log( `Created ${path.resolve( self.zipPath )} of ${self.prettyBytes(self.archive.pointer())}` );
98
- });
99
- }
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
+ }
100
113
  }
101
114
  module.exports = DirArchiver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dir-archiver",
3
- "version": "1.2.0",
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.2.0"
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
  }
package/CHANGELOG.md DELETED
@@ -1,28 +0,0 @@
1
- # Changes to Dir Archiver
2
-
3
- ### 1.2.0 (February 28, 2021)
4
-
5
- * Bump archiver from 4.0.2 to 5.2.0.
6
- * Make exclude paths relative to run directory ([#4](https://github.com/Ismail-elkorchi/dir-archiver/pull/4))
7
- * Remove the destination zip if it exists already ([#7](https://github.com/Ismail-elkorchi/dir-archiver/pull/7))
8
-
9
- ### 1.1.2 (July 21, 2020)
10
-
11
- * Bump lodash from 4.17.15 to 4.17.19.
12
- * Bump archiver from 4.0.1 to 4.0.2.
13
-
14
- ### 1.1.1 (May 14, 2020)
15
-
16
- * CLI : prevent execution if the required arguments are missing.
17
-
18
- ### 1.1.0 (May 13, 2020)
19
-
20
- * Add cli script.
21
-
22
- ### 1.0.1 (May 12, 2020)
23
-
24
- * Fix the installation instructions.
25
-
26
- ### 1.0.0 (May 12, 2020)
27
-
28
- * initial release.