UNPKG

mush-format

Version:

A javascript library that minifies pretty formatted mushcode.

113 lines (104 loc) 3.51 kB
const path = require("path"); const fs = require("fs"); const util = require("util"); const fetch = require("node-fetch"); const url = require("url"); const stringReplaceAsync = require("string-replace-async"); module.exports = async (data, next) => { const readFile = util.promisify(fs.readFile); const checkCache = async () => { // Check the cache for the file, else retrieve it. if (data.cache.get(data.curFile)) { data.logger(`Loading cached file: ${data.curFile}`); return data.cash.get(data.curFile); } else { if (data.type === "file" || data.type === "directory") { data.logger(`Reading file: ${data.curFile}`); data.cache.set(data.curFile, await readFile(data.curFile, "utf-8")); return data.cache.get(data.curFile); } else if (data.type === "github") { data.logger(`Downloading file: ${data.curFile}`); res = await fetch(data.curFile); body = await res.json(); let buff = new Buffer.from(body.content, "base64"); let text = buff.toString("utf-8"); data.cache.set(data.curFile, text); return data.cache.get(data.curFile); } } }; // Stitch multiple files together. Recursion, go! const include = async file => { file = await stringReplaceAsync( file, /#include\s+?(.*)/gi, async (...args) => { if (data.type === "file" || data.type === "directory") { data.curFile = path.resolve(data.base, args[1]); } else { data.curFile = url.resolve(data.base, args[1]); } const incFile = await checkCache(); return await include(incFile); } ); return file; }; data.logger("Building file."); if (data.type === "file") { // Set the starting file. data.curFile = path.resolve(__dirname, "../../" + data.input); data.base = path.dirname(data.curFile); try { const file = await checkCache(); data.raw = await include(file); next(null, data); } catch (error) { next(error); } } else if (data.type === "github") { const match = data.input.match(/^https:\/\/github\.com\/(.*)\/(.*)/); data.github.user = match[1]; data.github.repo = match[2]; data.base = `https://api.github.com/repos/${data.github.user}/${data.github.repo}/contents/`; data.curFile = `${data.base}index.mu`; try { const file = await checkCache(); data.raw = await include(file); next(null, data); } catch (error) { next(error); } } else if (data.type === "directory") { data.base = data.input; data.curFIle = data.base + "/index.mu"; try { const file = await checkCache(); data.raw = await include(file); next(null, data); } catch (error) { next(error); } } else { data.raw = data.input; } // process files let file; data.raw = await stringReplaceAsync( data.raw, /#file\s+?(.*)/gi, async (...args) => { if (data.type === "file" || data.type === "directory") { data.curFile = path.resolve(data.base, args[1]); file = await checkCache(); } else { data.curFile = url.resolve(data.base, args[1]); file = await checkCache(); } file = file.split(/\n|\r\n/).reduce((acc, curr) => { return (acc += `@@ ${curr}\n`); }, ""); return file; } ); };