Skip to content

Instantly share code, notes, and snippets.

@urmastalimaa
Last active March 18, 2018 09:59
Show Gist options
  • Save urmastalimaa/70edc0728cb711234f42 to your computer and use it in GitHub Desktop.
Save urmastalimaa/70edc0728cb711234f42 to your computer and use it in GitHub Desktop.
Script to zip a javascript project without node_modules
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