-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add streaming API * chore: lint fix * fix: fix error handling in streaming API * fix: strip ANSI codes from stream * fix: fix event listeners in dependencies add/remove * feat: add color * feat: add utf8-stream * fix: switch back to pre * fix: make sure output doesn't overflow * feat: use ansi-html-stream * feat: add rainbow test file
- Loading branch information
Showing
18 changed files
with
235 additions
and
55 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
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,15 +1,15 @@ | ||
const { send } = require('micro'); | ||
const hasYarn = require('has-yarn'); | ||
const execa = require('execa'); | ||
const ansiCommandStream = require('../../../src/ansiCommandStream'); | ||
|
||
module.exports = async ({ params: { script }, on }, res) => { | ||
module.exports = async (req, res) => { | ||
const { params: { script } } = req; | ||
try { | ||
const command = hasYarn() ? 'yarn' : 'npm'; | ||
const { stdout, kill } = execa(command, ['run', script]); | ||
stdout.pipe(process.stdout); | ||
send(res, 200, stdout); | ||
on('close', kill); | ||
const { stream, kill } = ansiCommandStream({ args: ['run', script] }); | ||
// stream.pipe(process.stdout); | ||
send(res, 200, stream); | ||
req.on('close', kill); | ||
} catch (err) { | ||
process.stderr.write(err.toString()); | ||
send(res, 404, err); | ||
} | ||
}; |
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,48 @@ | ||
const hasYarn = require('has-yarn'); | ||
const execa = require('execa'); | ||
const merge = require('merge-stream'); | ||
const ansi = require('ansi-html-stream'); | ||
|
||
const theme = { | ||
resets: { | ||
'0': false, | ||
}, | ||
bold: { | ||
'1': { style: 'font-weight:bold' }, | ||
}, | ||
underline: { | ||
'4': { style: 'text-decoration:underline' }, | ||
}, | ||
foregrounds: { | ||
'30': { style: 'color:black' }, // black | ||
'31': { style: 'color:red' }, // red | ||
'32': { style: 'color:green' }, // green | ||
'33': { style: 'color:#FDE541' }, // yellow | ||
'34': { style: 'color:blue' }, // blue | ||
'35': { style: 'color:magenta' }, // magenta | ||
'36': { style: 'color:cyan' }, // cyan | ||
'37': { style: 'color:white' }, // white | ||
'39': false, // default | ||
}, | ||
backgrounds: { | ||
'40': { style: 'background-color:black' }, // black | ||
'41': { style: 'background-color:red' }, // red | ||
'42': { style: 'background-color:green' }, // green | ||
'43': { style: 'background-color:yellow' }, // yellow | ||
'44': { style: 'background-color:blue' }, // blue | ||
'45': { style: 'background-color:magenta' }, // magenta | ||
'46': { style: 'background-color:cyan' }, // cyan | ||
'47': { style: 'background-color:white' }, // white | ||
'49': false, // default | ||
}, | ||
}; | ||
|
||
module.exports = function ansiCommandStream({ args }) { | ||
const command = hasYarn() ? 'yarn' : 'npm'; | ||
const { stdout, stderr, kill } = execa(command, [ | ||
// '--color="always"', | ||
...args, | ||
]); | ||
const stream = merge(stdout, stderr).pipe(ansi({ chunked: true, theme })); | ||
return { stream, kill }; | ||
}; |
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,15 @@ | ||
#!/bin/bash | ||
|
||
printf " " | ||
for b in 0 1 2 3 4 5 6 7; do printf " 4${b}m "; done | ||
echo | ||
for f in "" 30 31 32 33 34 35 36 37; do | ||
for s in "" "1;"; do | ||
printf "%4sm" "${s}${f}" | ||
printf " \033[%sm%s\033[0m" "$s$f" "gYw " | ||
for b in 0 1 2 3 4 5 6 7; do | ||
printf " \033[4%s;%sm%s\033[0m" "$b" "$s$f" " gYw " | ||
done | ||
echo | ||
done | ||
done |
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 +1,4 @@ | ||
export const get = path => fetch(path).then(response => response.json()); | ||
export const getJson = path => fetch(path).then(response => response.json()); | ||
|
||
export const getStream = path => | ||
fetch(path).then(response => response.body.getReader()); |
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 |
---|---|---|
@@ -1,23 +1,38 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
|
||
import { selectors } from '../ducks/scripts'; | ||
import { selectors, actions } from '../ducks/scripts'; | ||
import ScriptList from './ScriptList'; | ||
import { Grid } from './styled'; | ||
const Scripts = ({ scripts }) => ( | ||
import { Grid, Log, Wrapper } from './styled'; | ||
|
||
const Scripts = ({ scripts, log, onScriptClick }) => ( | ||
<Grid col="2"> | ||
<ScriptList scripts={scripts} /> | ||
<div> | ||
<ScriptList scripts={scripts} onScriptClick={onScriptClick} /> | ||
<Wrapper> | ||
<h3>Terminal output:</h3> | ||
</div> | ||
<Log dangerouslySetInnerHTML={{ __html: log }} /> | ||
</Wrapper> | ||
</Grid> | ||
); | ||
|
||
Scripts.propTypes = { | ||
scripts: PropTypes.array.isRequired, | ||
log: PropTypes.string.isRequired, | ||
onScriptClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default connect(state => ({ | ||
scripts: selectors.getScripts(state.scripts), | ||
}))(Scripts); | ||
export default connect( | ||
state => ({ | ||
scripts: selectors.getScripts(state.scripts), | ||
log: selectors.getLog(state.scripts), | ||
}), | ||
dispatch => | ||
bindActionCreators( | ||
{ | ||
onScriptClick: actions.runScript, | ||
}, | ||
dispatch, | ||
), | ||
)(Scripts); |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { call, put } from 'redux-saga/effects'; | ||
|
||
import { actions } from '../ducks/dependencies'; | ||
import { get } from '../api'; | ||
import { getJson } from '../api'; | ||
|
||
export function* fetchDependencies() { | ||
const dependencies = yield call(get, '/dependencies'); | ||
const dependencies = yield call(getJson, '/dependencies'); | ||
yield put(actions.setDependencies(dependencies)); | ||
} |
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,13 +1,14 @@ | ||
import { takeLatest } from 'redux-saga/effects'; | ||
import { takeLatest, fork } from 'redux-saga/effects'; | ||
|
||
import { FETCH_SCRIPTS } from '../ducks/scripts'; | ||
import { fetchScripts } from './scripts'; | ||
import { FETCH_SCRIPTS, RUN_SCRIPT } from '../ducks/scripts'; | ||
import { fetchScripts, runScript } from './scripts'; | ||
import { FETCH_DEPENDENCIES } from '../ducks/dependencies'; | ||
import { fetchDependencies } from './dependencies'; | ||
|
||
export default function* index() { | ||
yield* fetchScripts(); | ||
yield* fetchDependencies(); | ||
takeLatest(FETCH_SCRIPTS, fetchScripts); | ||
takeLatest(FETCH_DEPENDENCIES, fetchDependencies); | ||
yield fork(fetchScripts); | ||
yield fork(fetchDependencies); | ||
yield takeLatest(FETCH_SCRIPTS, fetchScripts); | ||
yield takeLatest(RUN_SCRIPT, runScript); | ||
yield takeLatest(FETCH_DEPENDENCIES, fetchDependencies); | ||
} |
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,9 +1,15 @@ | ||
import { call, put } from 'redux-saga/effects'; | ||
|
||
import { actions } from '../ducks/scripts'; | ||
import { get } from '../api'; | ||
import { getJson, getStream } from '../api'; | ||
import { streamToDispatch } from './utils'; | ||
|
||
export function* fetchScripts() { | ||
const { scripts } = yield call(get, '/scripts'); | ||
const { scripts } = yield call(getJson, '/scripts'); | ||
yield put(actions.setScripts(scripts)); | ||
} | ||
|
||
export function* runScript({ name }) { | ||
const reader = yield call(getStream, `/scripts/run/${name}`); | ||
yield* streamToDispatch(reader, actions.scriptLogAppend); | ||
} |
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,10 @@ | ||
import { call, put } from 'redux-saga/effects'; | ||
|
||
export function* streamToDispatch(reader, actionCreator) { | ||
while (true) { | ||
const result = yield call([reader, reader.read]); | ||
if (result.done) break; | ||
const text = String.fromCodePoint(...result.value); | ||
yield put(actionCreator(text)); | ||
} | ||
} |
Oops, something went wrong.