joyent-portal/ui/docs/webpack/embed-markdown-loader/src/compile/detach.js
Sérgio Ramos 557cd5a291 wip rm css
2016-12-12 10:28:47 +00:00

51 lines
872 B
JavaScript

const spawn = require('child_process').spawn;
const path = require('path');
const executable = path.join(__dirname, 'detached.js');
module.exports = ({
source,
entrypoint,
configFullpath
}, fn) => {
let out = '';
let err = '';
const child = spawn('node', [
executable,
`--entrypoint=${entrypoint}`,
`--config=${configFullpath}`
]);
child.stdin.write(source);
child.stdin.end();
child.stdout.on('data', (data) => {
out += data;
});
child.stderr.on('data', (data) => {
err += data;
});
child.on('close', (code) => {
if (code !== 0) {
return fn(new Error(err));
}
const res = {
style: '',
body: ''
};
try {
const _res = JSON.parse(out);
res.style = _res.style;
res.body = _res.body;
} catch(err) {
console.error(err);
}
fn(null, res);
});
};