From 60e60b72da5a17954b3b0bc441fdce781c4e96a0 Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Thu, 24 Jan 2019 07:43:27 -0600 Subject: [PATCH] fix: refactored to use native new Function --- index.js | 75 +++++++++------------------------------------------- package.json | 4 +-- test/test.js | 8 +++--- 3 files changed, 17 insertions(+), 70 deletions(-) diff --git a/index.js b/index.js index 14e7252..9462a4c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const { spawn } = require('child_process'); const fs = require('fs'); const path = require('path'); const pug = require('pug'); @@ -15,7 +14,7 @@ class CachePugTemplates { this.config = { app: false, views: false, - concurrency: 2, + concurrency: 1, ...config }; @@ -74,7 +73,8 @@ class CachePugTemplates { (filename, fn) => { this.writeCache(filename, fn); }, - () => { + err => { + if (err) console.error(err); debug('done with queue'); } ); @@ -82,67 +82,16 @@ class CachePugTemplates { writeCache(filename, fn) { debug(`compiling template located at ${filename}`); - - const cat = spawn('node_modules/.bin/shx', ['cat', filename], { - cwd: __dirname - }); - - const cli = spawn( - 'node_modules/.bin/pug', - ['--return-function-only', '-p', filename], - { - cwd: __dirname + fs.readFile(filename, 'utf8', (err, str) => { + if (err) return fn(err); + try { + const options = { cache: true, filename }; + if (pug.cache[filename]) return fn(); + pug.cache[filename] = pug.compile(str, options); + fn(); + } catch (err2) { + fn(err2); } - ); - - let tmpl = ''; - - cat.on('close', code => { - debug(`child worker for cat ${filename} exited with code ${code}`); - cli.stdin.end(); - }); - - cat.on('error', err => { - debug(`child worker for cat ${filename} had error ${err}`); - console.error(err); - }); - - cat.stdout.on('data', data => { - cli.stdin.write(data); - }); - - cli.on('close', code => { - debug(`child worker for pug ${filename} exited with code ${code}`); - (function() { - try { - tmpl = tmpl.trim(); - if (tmpl) { - tmpl = tmpl.replace('function template(locals) {', ''); - tmpl = tmpl.replace('return pug_html;}', 'return pug_html;'); - // eslint-disable-next-line no-new-func - pug.cache[filename] = new Function('locals', tmpl); - } - } catch (err) { - err.message = `${filename}: ${err.message}`; - console.error(err); - } - })(); - - delete this.queuedFiles[filename]; - fn(); - }); - - cli.on('error', err => { - debug(`child worker for pug ${filename} had error ${err}`); - console.error(err); - }); - - cli.stdout.on('data', data => { - tmpl += data.toString(); - }); - - cli.stderr.on('data', data => { - debug(`child worker for pug ${filename} had an error ${data}`); }); } diff --git a/package.json b/package.json index 96913c9..3741054 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,7 @@ ], "dependencies": { "async": "^2.6.1", - "debug": "^4.1.1", - "pug-cli": "git+https://github.com/niftylettuce/pug-cli.git", - "shx": "^0.3.2" + "debug": "^4.1.1" }, "devDependencies": { "ava": "^1.1.0", diff --git a/test/test.js b/test/test.js index d5b0dcb..eefec90 100644 --- a/test/test.js +++ b/test/test.js @@ -22,7 +22,7 @@ test.cb('rendering works', t => { t.log(`str 2 ${pug.compileFile(home)()}`); t.is(pug.cache[home](), pug.compileFile(home)()); t.end(); - }, 15000); + }, 3000); }); }); @@ -36,7 +36,7 @@ test.cb('email-templates', t => { setTimeout(() => { t.is(Object.keys(pug.cache).length, 3); t.end(); - }, 15000); + }, 3000); }); }); @@ -55,7 +55,7 @@ test.cb('koa', t => { setTimeout(() => { t.is(Object.keys(pug.cache).length, 3); t.end(); - }, 15000); + }, 3000); }); }); }); @@ -71,7 +71,7 @@ test.cb('express', t => { setTimeout(() => { t.is(Object.keys(pug.cache).length, 3); t.end(); - }, 15000); + }, 3000); }); }); });