-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(stdout-to-demo): get outputs logic moved to separate function
- Loading branch information
Showing
5 changed files
with
66 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
module.exports = getOutputs | ||
|
||
const fs = require('fs') | ||
const spawn = require('cross-spawn-async') | ||
const path = require('path') | ||
const normalizePath = require('normalize-path') | ||
const hookConsoleLog = require('./hook-console-log') | ||
|
||
function getOutputs (opts) { | ||
const tmpFileName = normalizePath(path.resolve(opts.cwd, `_${Math.random()}.js`)) | ||
fs.writeFileSync(tmpFileName, hookConsoleLog.addHook({ | ||
code: opts.code, | ||
filePath: tmpFileName, | ||
}), 'utf8') | ||
|
||
return new Promise((resolve, reject) => { | ||
const outputs = [] | ||
let failed = false | ||
|
||
const cp = spawn('node', [tmpFileName]) | ||
cp.stdout.setEncoding('utf8') | ||
cp.stdout.on('data', data => { | ||
try { | ||
data.split(hookConsoleLog.messageSplitter) | ||
.filter(outputJSON => !!outputJSON) | ||
.map(outputJSON => JSON.parse(outputJSON)) | ||
.forEach(outputInfo => outputs.push(outputInfo)) | ||
} catch (err) { | ||
failed = true | ||
reject(err) | ||
} | ||
}) | ||
let errData = '' | ||
|
||
cp.stderr.setEncoding('utf8') | ||
cp.stderr.on('data', data => { errData += data }) | ||
|
||
cp.on('close', () => { | ||
fs.unlinkSync(tmpFileName) | ||
|
||
if (failed) { | ||
return | ||
} | ||
|
||
if (errData) { | ||
return reject(new Error(errData)) | ||
} | ||
|
||
return resolve(outputs) | ||
}) | ||
}) | ||
} |
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('./get-outputs') |
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