Skip to content

Commit

Permalink
refactor(stdout-to-demo): get outputs logic moved to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Apr 23, 2016
1 parent 9002871 commit ec64e41
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -72,6 +72,7 @@ Return a promise with the resulting file combined with output.
- [lodash.partition](https://github.com/lodash/lodash): The lodash method `_.partition` exported as a module.
- [normalize-newline](https://github.com/sindresorhus/normalize-newline): Normalize the newline characters in a string to `\n`
- [normalize-path](https://github.com/jonschlinkert/normalize-path): Normalize file path slashes to be unix-like forward slashes. Also condenses repeat slashes to a single slash and removes and trailing slashes.
- [promise.prototype.finally](https://github.com/matthew-andrews/Promise.prototype.finally): A polyfill for Promise.prototype.finally for ES6 compliant promises
- [rollup](https://github.com/rollup/rollup): Next-generation ES6 module bundler
- [rollup-plugin-babel](https://github.com/rollup/rollup-plugin-babel): Seamless integration between Rollup and Babel.
- [rollup-plugin-includepaths](https://github.com/dot-build/rollup-plugin-includepaths): Rollup plugin to use relative paths in your project files
Expand All @@ -92,6 +93,7 @@ Return a promise with the resulting file combined with output.
- [istanbul](https://github.com/gotwarlost/istanbul): Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests
- [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework
- [mos](https://github.com/zkochan/mos): A pluggable module that injects content into your markdown files via hidden JavaScript snippets
- [promise.prototype.finally](https://github.com/matthew-andrews/Promise.prototype.finally): A polyfill for Promise.prototype.finally for ES6 compliant promises
- [semantic-release](https://github.com/semantic-release/semantic-release): automated semver compliant package publishing
- [validate-commit-msg](https://github.com/kentcdodds/validate-commit-msg): Script to validate a commit message follows the conventional changelog standard

Expand Down
53 changes: 53 additions & 0 deletions lib/get-outputs.js
@@ -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)
})
})
}
2 changes: 2 additions & 0 deletions lib/get-outputs.spec.js
@@ -0,0 +1,2 @@
'use strict'
require('./get-outputs')
56 changes: 7 additions & 49 deletions lib/stdout-to-demo.js
Expand Up @@ -3,63 +3,22 @@ module.exports = stdoutToDemo

const acorn = require('acorn')
const walk = require('acorn/dist/walk')
const fs = require('fs')
const spawn = require('cross-spawn-async')
const path = require('path')
const SourceMapConsumer = require('source-map').SourceMapConsumer
const normalizePath = require('normalize-path')
const position = require('file-position')
const normalizeNewline = require('normalize-newline')
const consoleStringify = require('./console-stringify')
const partition = require('lodash.partition')
const hookConsoleLog = require('./hook-console-log')
const getOutputs = require('./get-outputs')

function stdoutToDemo (opts) {
if (typeof opts === 'string') opts = { code: opts }
opts.cwd = opts.cwd || process.cwd()
opts.code = normalizeNewline(opts.code)

const cwd = opts.cwd || process.cwd()

return new Promise((resolve, reject) => {
const code = normalizeNewline(opts.code)

const tmpFileName = normalizePath(path.resolve(cwd, `_${Math.random()}.js`))
fs.writeFileSync(tmpFileName, hookConsoleLog.addHook({
code,
filePath: tmpFileName,
}), 'utf8')

const outputs = []
let failed = false

const cp = spawn('node', [tmpFileName])
cp.stdout.setEncoding('utf8')
cp.stderr.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.on('data', data => { errData += data })
cp.on('close', () => {
fs.unlinkSync(tmpFileName)

if (errData) {
return reject(new Error(errData))
}

if (failed) {
return
}

return getOutputs(opts)
.then(outputs => {
if (!opts.map) {
return resolve(insertOutputsToCode(code, outputs))
return insertOutputsToCode(opts.code, outputs)
}
const consumer = new SourceMapConsumer(opts.map)
const originalOutputs = outputs
Expand All @@ -69,9 +28,8 @@ function stdoutToDemo (opts) {
})))
const sourcesContent = JSON.parse(opts.map).sourcesContent
const sourceContent = sourcesContent[sourcesContent.length - 1]
resolve(insertOutputsToCode(sourceContent, originalOutputs))
return insertOutputsToCode(sourceContent, originalOutputs)
})
})
}

function insertOutputsToCode (code, outputs) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"lodash.partition": "^4.3.0",
"normalize-newline": "2.0.0",
"normalize-path": "2.0.1",
"promise.prototype.finally": "^1.0.1",
"rollup": "0.25.8",
"rollup-plugin-babel": "2.4.0",
"rollup-plugin-includepaths": "0.1.2",
Expand All @@ -62,6 +63,7 @@
"istanbul": "^0.4.2",
"mocha": "^2.3.4",
"mos": "0.11.1",
"promise.prototype.finally": "^1.0.1",
"semantic-release": "^4.3.5",
"validate-commit-msg": "2.6.0"
},
Expand Down

0 comments on commit ec64e41

Please sign in to comment.