UNPKG

gotta-go-fast

Version:
65 lines (54 loc) 1.2 kB
"use strict"; const fs = require("fs"); function writeFile(path, data) { return new Promise((resolve, reject) => { fs.writeFile(path, data, (err) => { if (err) { console.error(err); reject(err); return; } resolve(); }); }); } function readFile(path) { return new Promise((resolve, reject) => { fs.readFile(path, "utf8", (err, body) => { if (err) { console.error(err); reject(err); return; } resolve(body); }); }) } function isFileSync(path) { try { let stat = fs.statSync(path); return stat.isFile(); } catch(e) { return false; } } function filterCopyFile(path, out, filter) { return readFile(path) .then(filter) .then(result => writeFile(out, result)); } function dataCopyFile(inPath, outPath, data) { return filterCopyFile(inPath, outPath, (body) => { return body.replace(/{{([^}]+)}}/g, (whole, key) => { return data[key]; }); }); } function mkdir(path) { return new Promise((resolve, reject) => { fs.mkdir(path, (err) => { resolve(); }); }); } module.exports = {writeFile, readFile, mkdir, filterCopyFile, dataCopyFile, isFileSync};