Last active
March 18, 2018 09:59
-
-
Save urmastalimaa/70edc0728cb711234f42 to your computer and use it in GitHub Desktop.
Script to zip a javascript project without node_modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archiver = require('archiver'); // `yarn add --dev archiver` or `npm install --save-dev archiver` | |
const fs = require('fs'); | |
const IGNORE_DIR_PATTERNS = [/node_modules/]; | |
const IGNORE_FILE_PATTERNS = []; | |
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment