Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to include the base directory in the archive root #11

Merged
merged 7 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,33 @@ const excludes = ['directory_name', 'file.extension'];
* Create a dir-archiver object.
* @param {string} directoryPath - The path of the folder to archive.
* @param {string} zipPath - The path of the zip file to create.
* @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.
* @param {array} excludes - A list with the names of the files and folders to exclude.
*/
var archive = new DirArchiver('path/to/directory', 'path/to/desination/zipfile.zip', excludes);
var archive = new DirArchiver('path/to/directory', 'path/to/desination/zipfile.zip', true, excludes);

// Create the zip file.
archive.createZip();
```
## Command Line Interface

```sh
Usage: dir-archiver --src <path-to-directory> --dest <path-to-file>.zip --exclude folder-name file-name.extention
Usage: dir-archiver --src <path-to-directory> --dest <path-to-file>.zip --includebasedir true|false --exclude folder-name file-name.extention

Options:
--src The path of the folder to archive.
--dest The path of the zip file to create.
--exclude Specify a list with the names of the files and folders to exclude
--src The path of the folder to archive. [string][required]
--dest The path of the zip file to create. [string][required]
--includebasedir Includes a base directory at the root of the archive.
For example, if the root folder of your project is named
"your-project", setting this option 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. [bool]
--exclude A list with the names of the files and folders to exclude. [array]
```


Expand Down
18 changes: 14 additions & 4 deletions cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ var DirArchiver = require('./index');
const arguments = process.argv;
var directoryPath = '';
var zipPath = '';
var includeBaseDirectory = true;
var excludes = [];

if ( ! arguments.includes( '--src' ) || ! arguments.includes( '--dest' ) ) {
console.log(` Dir Archiver could not be executed. Some arguments are missing.

Options:
--src The path of the folder to archive. [string][required]
--dest The path of the zip file to create. [string][required]
--exclude A list with the names of the files and folders to exclude. [array]`);
--src The path of the folder to archive. [string][required]
--dest The path of the zip file to create. [string][required]
--includebasedir Includes a base directory at the root of the archive.
For example, if the root folder of your project is named
"your-project", setting this option 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. [bool]
--exclude A list with the names of the files and folders to exclude. [array]`);
process.exit();
}

Expand All @@ -24,6 +31,9 @@ for ( argumentIndex in arguments ) {
if( arguments[argumentIndex] === '--dest' ) {
zipPath = arguments[parseInt(argumentIndex) + 1];
}
if( arguments[argumentIndex] === '--includebasedir' ) {
includeBaseDirectory = ( arguments[parseInt(argumentIndex) + 1] === 'true' );
}
if( afterExclude === true ) {
excludes.push( arguments[argumentIndex] );
}
Expand All @@ -32,5 +42,5 @@ for ( argumentIndex in arguments ) {
}
}

const archive = new DirArchiver(directoryPath, zipPath, excludes);
const archive = new DirArchiver(directoryPath, zipPath, includeBaseDirectory, excludes);
archive.createZip();
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ class DirArchiver {
* The constructor.
* @param {string} directoryPath - the path of the folder to archive.
* @param {string} zipPath - The path of the zip file to create.
* @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.
* @param {array} excludes - The name of the files and foldes to exclude.
*/
constructor(directoryPath, zipPath, excludes){
constructor(directoryPath, zipPath, includeBaseDirectory, excludes){

// Contains the excluded files and folders.
this.excludes = excludes;

this.directoryPath = directoryPath;

this.zipPath = zipPath;

this.includeBaseDirectory = includeBaseDirectory;

this.baseDirectory = path.basename(path.resolve(directoryPath));
}

/**
Expand All @@ -32,9 +37,15 @@ class DirArchiver {
const stats = fs.statSync( currentPath );
let relativePath = path.relative(process.cwd(), currentPath);
if ( stats.isFile() && ! this.excludes.includes( relativePath ) ) {
this.archive.file(currentPath, {
name: `${relativePath}`
});
if( this.includeBaseDirectory === true) {
this.archive.file(currentPath, {
name: `${this.baseDirectory}/${relativePath}`
});
} else {
this.archive.file(currentPath, {
name: `${relativePath}`
});
}
} else if ( stats.isDirectory() && ! this.excludes.includes( relativePath ) ) {
this.traverseDirectoryTree( currentPath );
}
Expand Down