UNPKG

@sammwy/milkshake

Version:

<p align="center"> <img src="https://raw.githubusercontent.com/sammwyy/milkshake/master/docs/milkshake.png" height="64px"> <h1 align="center">MilkShake</h1> <p align="center"> <b>Automatize Tasks, Compile, concatenate and minify automaticall

166 lines (139 loc) 4.99 kB
const fs = require("fs"); const path = require("path"); const Watcher = require("./utils/watcher"); const { concat } = require("./preprocessors/concatenation"); const Minifiers = require("./preprocessors/minifiers.js"); const Compressors = require("./preprocessors/compressors"); const Compilers = require("./preprocessors/compilers"); const { dir } = require("console"); let watcher = new Watcher(); exports.watchAndCompile = (extension, directory, target, main, useES6, callback) => { if (extension == "css") { watchForCSS(directory, target, callback); } else if (extension == "js") { watchForJS(directory, target, useES6, callback); } else if (extension == "image") { watchForImages(directory, target, callback); } else if (extension == "sass") { watchForSass(directory, main, target, callback); } else if (extension == "less") { watchForLess(directory, main, target, callback); } else if (extension == "stylus") { watchForStylus(directory, main, target, callback); } } function watchForImages (directory, target, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".png") && !file.toLowerCase().endsWith(".jpg")) { return; } if (directory == dir) { await Compressors.compressImage(directory, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".jpg"); } function watchForSass (directory, main, target, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".sass")) { return; } if (directory == dir) { let compiled = await Compilers.compileSass(path.join(directory, main)); if (compiled == null) { return; } let minified = Minifiers.minifyCSS(compiled); await saveInToFile(minified, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".sass"); } function watchForStylus (directory, main, target, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".styl")) { return; } if (directory == dir) { let compiled = await Compilers.compileStylus(directory, path.join(directory, main)); if (compiled == null) { console.error("Failed to compile stylus:\n" + compiled) return; } let minified = Minifiers.minifyCSS(compiled); await saveInToFile(minified, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".styl"); } function watchForLess (directory, main, target, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".less")) { return; } if (directory == dir) { let compiled = await Compilers.compileLess(directory, path.join(directory, main)); if (compiled == null || compiled.css == null) { console.error("Failed to compile less:\n" + compiled) return; } let minified = Minifiers.minifyCSS(compiled.css); await saveInToFile(minified, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".less"); } function watchForCSS (directory, target, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".css")) { return; } if (directory == dir) { let results = await concat(directory); let minified = Minifiers.minifyCSS(results.join("\n")); await saveInToFile(minified, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".css"); } function watchForJS (directory, target, useES6, callback) { const handle = async (dir, file) => { if (!file.toLowerCase().endsWith(".js")) { return; } if (directory == dir) { let results = await concat(directory); let minified = Minifiers.minifyJS(results.join("\n"), useES6); await saveInToFile(minified, target); callback(); } } watcher.watch(directory); watcher.on("*", handle); handle(directory, ".js"); } function saveInToFile (content, target) { return new Promise (( resolve, reject ) => { fs.writeFile(target, content, () => { resolve(); }); }) }