This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin-example): make the example plugin work with async code
Execute the examples in child processes in order to be able to determine when they truly end. fix #11
- Loading branch information
Showing
6 changed files
with
98 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
module.exports = hookConsoleLog | ||
|
||
const callsites = require('callsites') | ||
const removeLastEOL = require('../../remove-last-eol') | ||
|
||
const originalLog = console.log | ||
|
||
function hookConsoleLog (filePath) { | ||
console.log = function () { | ||
const site = callsiteForFile(filePath) | ||
|
||
originalLog({ | ||
message: getRealConsoleOutput.apply(null, arguments), | ||
line: site.line, | ||
}) | ||
} | ||
} | ||
|
||
function getRealConsoleOutput () { | ||
const originalWrite = process.stdout.write | ||
|
||
let message | ||
process.stdout.write = msg => { message = msg } | ||
|
||
originalLog.apply(console, arguments) | ||
|
||
process.stdout.write = originalWrite | ||
|
||
return removeLastEOL(message) | ||
} | ||
|
||
function callsiteForFile (fileName) { | ||
const stack = trace() | ||
return stack.find(callsite => callsite.file === fileName) | ||
} | ||
|
||
function trace () { | ||
return callsites().map(callsite => ({ | ||
file: callsite.getFileName() || '?', | ||
line: callsite.getLineNumber(), | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict' | ||
require('./hook-console-log') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,64 @@ | ||
'use strict' | ||
module.exports = stdoutToComments | ||
|
||
const removeLastEOL = require('../../remove-last-eol') | ||
const fs = require('fs') | ||
const callsites = require('callsites') | ||
const spawn = require('cross-spawn-async') | ||
const path = require('path') | ||
const hookPath = path.resolve(__dirname, './hook-console-log') | ||
|
||
function stdoutToComments (filePath) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filePath, 'utf8', (err, content) => { | ||
if (err) return reject(err) | ||
|
||
const originalLog = console.log | ||
const outputs = [] | ||
|
||
console.log = function () { | ||
const site = callsiteForFile(filePath) | ||
|
||
outputs.push({ | ||
message: getRealConsoleOutput.apply(null, arguments), | ||
line: site.line, | ||
}) | ||
} | ||
|
||
function getRealConsoleOutput () { | ||
const originalWrite = process.stdout.write | ||
|
||
let message | ||
process.stdout.write = msg => { message = msg } | ||
|
||
originalLog.apply(console, arguments) | ||
const tmpFileName = filePath + Math.random() + '.js' | ||
fs.writeFileSync(tmpFileName, addHook({ | ||
code: content, | ||
filePath: tmpFileName, | ||
}), 'utf8') | ||
|
||
process.stdout.write = originalWrite | ||
const outputs = [] | ||
let failed = false | ||
|
||
return removeLastEOL(message) | ||
} | ||
const cp = spawn('node', [tmpFileName]) | ||
cp.stdout.setEncoding('utf8') | ||
cp.stderr.setEncoding('utf8') | ||
cp.stdout.on('data', data => { | ||
try { | ||
eval(`outputs.push(${data})`) // eslint-disable-line no-eval | ||
} catch (err) { | ||
failed = true | ||
reject(err) | ||
} | ||
}) | ||
cp.stderr.on('data', data => console.error(data)) | ||
cp.on('close', code => { | ||
fs.unlinkSync(tmpFileName) | ||
|
||
try { | ||
require(filePath) | ||
} catch (err) { | ||
throw err | ||
} finally { | ||
console.log = originalLog | ||
} | ||
if (failed) { | ||
return | ||
} | ||
|
||
resolve(content.split('\n').reduce((contentLines, line, index) => { | ||
contentLines.push(line) | ||
resolve(content.split('\n').reduce((contentLines, line, index) => { | ||
contentLines.push(line) | ||
|
||
const lineNo = index + 1 | ||
while (outputs.length && outputs[0].line === lineNo) { | ||
const matches = (contentLines[contentLines.length - 1] || '').match(/^(\s*)/) | ||
const linePadding = matches && matches[0] || '' | ||
contentLines.push(linePadding + '//> ' + | ||
outputs.shift().message.replace(/\r?\n/g, '\n' + linePadding + '// ')) | ||
} | ||
return contentLines | ||
}, []).join('\n')) | ||
const lineNo = index + 1 | ||
while (outputs.length && outputs[0].line === lineNo) { | ||
const matches = (contentLines[contentLines.length - 1] || '').match(/^(\s*)/) | ||
const linePadding = matches && matches[0] || '' | ||
contentLines.push(linePadding + '//> ' + | ||
outputs.shift().message.replace(/\r?\n/g, '\n' + linePadding + '// ')) | ||
} | ||
return contentLines | ||
}, []).join('\n')) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
function callsiteForFile (fileName) { | ||
const stack = trace() | ||
return stack.find(callsite => callsite.file === fileName) | ||
} | ||
|
||
function trace () { | ||
return callsites().map(callsite => ({ | ||
file: callsite.getFileName() || '?', | ||
line: callsite.getLineNumber(), | ||
})) | ||
function addHook (opts) { | ||
if (!opts.code.match(/['"]use strict['"]/)) { | ||
return `require('${hookPath}')('${opts.filePath}');` + opts.code | ||
} | ||
return `'use strict';require('${hookPath}')('${opts.filePath}');` + opts.code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters