Skip to content

Commit

Permalink
fix(build): missing json5
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed May 5, 2020
1 parent a2cfab6 commit 1343400
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
73 changes: 73 additions & 0 deletions lib/middleware/eventServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const http = require('http')

let broadcast = () => { }
let __payload = null
let __buildId = Date.now()



broadcast = createSSEServer()



__payload = payload
__buildId++
broadcast('payload', __payload, __buildId)






function createSSEServer() {
const clients = []

const server = http.createServer(function (req, res) {
if (req.url.match('^\/verify')) {
res.writeHead(200, {
'Content-Type': 'text/json',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*'
});
const id = req.url.replace(/^\/verify\//, '')
res.write(JSON.stringify({ verified: id == __buildId, providedId: id, currentId: __buildId }))
res.end()
}
if (req.url === '/stream') {
clients.push(res)
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*'
});
send(res, `connected`, 'hello', 0)
send(res, 'payload', __payload, __buildId)
}
})


const port = 13975
const maxTries = 25
let _port = port
server.on('error', (err) => {
if (err.message.match('EADDRINUSE:')) {
if (_port < port + maxTries) {
log(`port ${_port} is in use. Trying port ${_port++ + 1}`)
server.listen(_port);
}
else log('Error: could not find an empty port for server events')
}
})
server.listen(_port);


function broadcast(eventName, data, id) { clients.forEach(res => send(res, eventName, data, id)) }

function send(res, eventName, data, id) {
res.write(`event: ${eventName}\nid: ${id}\ndata: ${JSON.stringify(data)}`)
res.write(`\n\n`)
}

return broadcast
}
33 changes: 29 additions & 4 deletions lib/services/middlewareRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const { addPath, addId, removeUnderscoredDirs, removeNonSvelteFiles, defineFiles
const template = require('../middleware/createTemplate')
const { pipeAsync } = require('../utils/fp')

module.exports = async function middlewareRunner(payload) {
function initial(x) { Object.assign(x, payload)} //initial payload = {options, metaParser}
// @ts-check

return await pipeAsync(
/**
* @param {TreePayload} payload
* @returns
*/
module.exports = async function middlewareRunner(payload) {
console.log(payload.options)

function initial(x) { Object.assign(x, payload) } //initial payload = {options, metaParser}

const _middlewares = {
initial,
generateFileTree, // => children
removeUnderscoredDirs, // _private => false
Expand All @@ -21,5 +29,22 @@ module.exports = async function middlewareRunner(payload) {
addId, // ... => ({ id, ... })
attachComponent, // { component }; { path } for layouts
template
)
}

const middlewares = Object.entries(_middlewares)
.map(([name, middleware]) => ({ name, middleware }))


applyExtensions(payload, middlewares)

return await pipeAsync(...Object.values(middlewares))
}



async function applyExtensions(payload = [], middlewares) {
const plugins = payload || []
plugins.unshift(eventServer)
for (plugin of plugins)
await plugin(middlewares, payload)
}
6 changes: 3 additions & 3 deletions lib/utils/fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const someAsync = async (postulate, array) => {

const pipe = (...fns) => initial => fns.reduce((x, f) => f(x), initial)

async function pipeAsync (...fns) {
async function pipeAsync (...mws) {
let x = {}
for (const f of fns) {
await f(x)
for (const mw of mws) {
await mw.middleware(x)
}
return x
}
Expand Down
10 changes: 4 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dotenv": "^8.2.0",
"fs-extra": "^9.0.0",
"js-yaml": "^3.13.1",
"json5": "^2.1.3",
"log-symbols": "^3.0.0",
"picomatch": "^2.2.1",
"readdirp": "^3.3.0",
Expand Down

0 comments on commit 1343400

Please sign in to comment.