zip-a-folder
Advanced tools
Comparing version 1.1.7 to 2.0.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
import * as fs from 'fs'; | ||
import * as isGlob from 'is-glob'; | ||
@@ -25,10 +26,10 @@ export enum COMPRESSION_LEVEL { | ||
/** | ||
* Tars a given folder into a gzipped tar archive. | ||
* Tars a given folder or a glob into a gzipped tar archive. | ||
* If no zipAFolderOptions are passed in, the default compression level is high. | ||
* @param srcFolder | ||
* @param tarFilePath | ||
* @param src can be a string path or a glob | ||
* @param tarFilePath path to the zip file | ||
* @param zipAFolderOptions | ||
*/ | ||
static async tar( | ||
srcFolder: string, | ||
src: string, | ||
tarFilePath: string | undefined, | ||
@@ -42,6 +43,6 @@ zipAFolderOptions?: ZipAFolderOptions | ||
if (o.compression === COMPRESSION_LEVEL.uncompressed) { | ||
await ZipAFolder.compress({srcFolder, targetFilePath: tarFilePath, format: 'tar', zipAFolderOptions}); | ||
await ZipAFolder.compress({src, targetFilePath: tarFilePath, format: 'tar', zipAFolderOptions}); | ||
} else { | ||
await ZipAFolder.compress({ | ||
srcFolder, | ||
src, | ||
targetFilePath: tarFilePath, | ||
@@ -61,10 +62,10 @@ format: 'tar', | ||
/** | ||
* Zips a given folder into a zip archive. | ||
* Zips a given folder or a glob into a zip archive. | ||
* If no zipAFolderOptions are passed in, the default compression level is high. | ||
* @param srcFolder | ||
* @param zipFilePath | ||
* @param src can be a string path or a glob | ||
* @param zipFilePath path to the zip file | ||
* @param zipAFolderOptions | ||
*/ | ||
static async zip( | ||
srcFolder: string, | ||
src: string, | ||
zipFilePath: string | undefined, | ||
@@ -79,3 +80,3 @@ zipAFolderOptions?: ZipAFolderOptions | ||
await ZipAFolder.compress({ | ||
srcFolder, | ||
src, | ||
targetFilePath: zipFilePath, | ||
@@ -90,3 +91,3 @@ format: 'zip', | ||
await ZipAFolder.compress({ | ||
srcFolder, | ||
src, | ||
targetFilePath: zipFilePath, | ||
@@ -105,3 +106,3 @@ format: 'zip', | ||
private static async compress({ | ||
srcFolder, | ||
src, | ||
targetFilePath, | ||
@@ -112,3 +113,3 @@ format, | ||
}: { | ||
srcFolder: string; | ||
src: string; | ||
targetFilePath?: string; | ||
@@ -124,7 +125,9 @@ format: archiver.Format; | ||
if (targetBasePath === srcFolder) { | ||
if (targetBasePath === src) { | ||
throw new Error('Source and target folder must be different.'); | ||
} | ||
try { | ||
await fs.promises.access(srcFolder, fs.constants.R_OK); //eslint-disable-line no-bitwise | ||
if (!isGlob(src)) { | ||
await fs.promises.access(src, fs.constants.R_OK); //eslint-disable-line no-bitwise | ||
} | ||
await fs.promises.access(targetBasePath, fs.constants.R_OK | fs.constants.W_OK); //eslint-disable-line no-bitwise | ||
@@ -148,3 +151,10 @@ } catch (e: any) { | ||
zipArchive.pipe(output); | ||
zipArchive.directory(srcFolder, false); | ||
if (isGlob(src)) { | ||
src.split(',').forEach((globPart) => { | ||
zipArchive.glob(globPart); | ||
}); | ||
} else { | ||
zipArchive.directory(src, false); | ||
} | ||
zipArchive.finalize(); | ||
@@ -151,0 +161,0 @@ }); |
{ | ||
"name": "zip-a-folder", | ||
"version": "1.1.7", | ||
"description": "Zip/Tar a complete folder into a zip/tgz file", | ||
"version": "2.0.0", | ||
"description": "Zip/Tar a complete folder or a glob list into a zip/tgz file", | ||
"author": "Marius Augenstein", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/maugenst/zip-a-folder/issues" | ||
}, | ||
"homepage": "https://github.com/maugenst/zip-a-folder#readme", | ||
"main": "dist/lib/ZipAFolder.js", | ||
@@ -27,10 +33,5 @@ "typings": "dist/lib/ZipAFolder.d.ts", | ||
], | ||
"author": "Marius Augenstein", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/maugenst/zip-a-folder/issues" | ||
}, | ||
"homepage": "https://github.com/maugenst/zip-a-folder#readme", | ||
"devDependencies": { | ||
"@types/archiver": "^5.3.2", | ||
"@types/is-glob": "^4.0.2", | ||
"@types/jest": "^29.5.2", | ||
@@ -57,4 +58,5 @@ "@types/node": "^20.2.5", | ||
"dependencies": { | ||
"archiver": "^5.3.1" | ||
"archiver": "^5.3.1", | ||
"is-glob": "^4.0.3" | ||
} | ||
} |
@@ -7,5 +7,8 @@ [![NPM](https://nodei.co/npm/zip-a-folder.png)](https://nodei.co/npm/zip-a-folder/) | ||
# zip-a-folder | ||
Easy to use zip (or tar) a complete folder plain into a zip file | ||
Easy to use zip (or tar) a complete folder or a list of globs plain into a zip/tar/tgz file | ||
including compression ratio handling and custom write streams. | ||
Version 2 adds glob lists handling as a src. So please be aware that using globs intentionally | ||
breaks up the "create-a-zip/tar-file-from-a-folder" approach. | ||
## Basic Usage | ||
@@ -74,5 +77,5 @@ | ||
ATTENTION: customWritestreams cannot be checked. So it is up to the user to check | ||
on non existing target folders or if the targetfolder equals to the sourcefolder | ||
(which leads to a circularity). | ||
ATTENTION: `customWriteStream` is not checked. So it is up to the user to check | ||
on non-existing target folders or if the targetfolder equals to the sourcefolder | ||
(ending up in circularity). | ||
@@ -93,2 +96,23 @@ ```js | ||
### Glob handling | ||
The first parameter can be either a path or a glob. Globs are separated by comma. | ||
```js | ||
import { zip} from 'zip-a-folder'; | ||
class TestMe { | ||
static async main() { | ||
// zip all json into an archive | ||
await zip('**/*.json', '/path/to/archive.zip'); | ||
// zip all json AND txt files into a second archive | ||
await zip('**/*.json, **/*.txt', '/path/to/archive2.zip'); | ||
} | ||
} | ||
TestMe.main(); | ||
``` | ||
### Tests | ||
@@ -104,2 +128,6 @@ | ||
* Thanks to ratbeard | ||
* Thanks to Xotabu4 | ||
* Thanks to Xotabu4 | ||
* Thanks to dallenbaldwin | ||
* Thanks to wiralegawa | ||
* Thanks to karan-gaur | ||
* Thanks to malthe |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63593
398
129
2
21
+ Addedis-glob@^4.0.3
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)