-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipHomework.js
27 lines (20 loc) · 1.03 KB
/
zipHomework.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const Archiver = require("archiver"); // `yarn add --dev archiver` or `npm install --save-dev archiver`
const fs = require("fs");
const IGNORE_DIR_PATTERNS = [/node_modules/, /.git/, /.nyc_output/];
const IGNORE_FILE_PATTERNS = [/public\/app.js/, /public\/app.js.map/];
const TARGET_FILE_NAME = "homework.zip";
const matchesAnyPattern = (patterns, testString) => {
return patterns.some((pattern) => testString.match(pattern));
};
const pathsInCurrentDirectory = fs.readdirSync(".");
const files = pathsInCurrentDirectory
.filter((path) => fs.statSync(path).isFile())
.filter((filePath) => !matchesAnyPattern(IGNORE_FILE_PATTERNS, filePath));
const folders = pathsInCurrentDirectory
.filter((path) => fs.statSync(path).isDirectory())
.filter((dirPath) => !matchesAnyPattern(IGNORE_DIR_PATTERNS, dirPath));
const archive = Archiver.create("zip", {});
files.forEach((filePath) => archive.file(filePath));
folders.forEach((folderPath) => archive.directory(folderPath));
archive.pipe(fs.createWriteStream(TARGET_FILE_NAME));
archive.finalize();