Skip to content

Instantly share code, notes, and snippets.

@reqshark
Created January 21, 2017 04:49
Show Gist options
  • Save reqshark/533b3c0e58e35aa501b79b9d1519c524 to your computer and use it in GitHub Desktop.
Save reqshark/533b3c0e58e35aa501b79b9d1519c524 to your computer and use it in GitHub Desktop.
my javascript build system: more front-end shenanigans
// $ npm i nanomsg browserify es6ify stylus
// $ node bld
const createWriteStream = require('fs').createWriteStream
const readFileSync = require('fs').readFileSync
const writeFile = require('fs').writeFile
const resolve = require('path').resolve
const browserify = require('browserify')
const join = require('path').join
const watch = require('fs').watch
const stat = require('fs').stat
const stylus = require('stylus')
const utf8 = {encoding:'utf8'}
switch (process.argv[2]) {
case '-watch':
case 'watch':
case '-w':
watch(join(__dirname, 'src'), utf8, fileChange)
watch(join(__dirname, 'css'), utf8, fileChange)
}
var ws = createWriteStream(join(__dirname, 'dot.js')), size
/**
* `fileChange()`
* try to watch `vdomsite/src` for changes and call `build()` to rebuild js
* also try to watch `vdomsite/css` for changes and call `styl()` to rebuild css
*/
function fileChange (eh, file) {
switch (file) {
case 'dot.js' : return
case 'style.css' : return
case 'main.styl' : return styl()
}
vdom(file)
}
/**
* `vdom ( filename )`
* build virtual-dom javascript (from entry point src/main.js)
* a global `b`, from which `bundle()` is called, generates new readables
*/
var b = browserify()
.add(join(__dirname, 'src/main.js'))
.transform(require('es6ify'))
function vdom (f) {
var readable = b.bundle()
stat(join(__dirname, 'dot.js'), (err, stats)=> size = stats.size)
// i was curious to bench the stream
// readable's `s` property is start time checked `.on('end')`
readable.s = Date.now()
readable
.on('end', perf )
.pipe(createWriteStream(join(__dirname, 'dot.js')))
.f = f
}
function perf () {
str = this.f ? `saved ${this.f}, rebuilt` : 'built'
console.log(`${str} dot.js in ${Date.now() - this.s}ms`)
if (!size)
console.log(`now open index.html in a browser:\n
$ open ${join(__dirname, 'index.html')}\n`)
}
/**
* builds css from the stylus in css/main.styl
*/
function styl() {
stylus( readFileSync( join(__dirname, 'css/main.styl'), 'utf8' ) )
.include( join(__dirname, 'css') )
.render( (err, css) =>
writeFile( join(__dirname, 'css/style.css'), css ) ) // .render()
console.log('generating new style.css')
}
vdom()
styl()
// this udp server is for receiving events from atom editor when a file saves
// lower latency than file watcher, and be sure to alleviate text editor load
const server = require('dgram').createSocket('udp4')
server.on('error', er); function er( err ) { server.close(); throw err }
server.on('listening', heard )
function heard(){
var addr = this.address()
console.log('server listening '+ addr.address + ':' + addr.port)
}
server.on('message', handler )
// this function will handle the buffer of our file path
function handler( buf ) {
// parse saved file path, on windows use node's `require('path')` api
var msg = String( buf ), s = msg.split('/');
console.log( `saved buf: ${msg}` )
// remove '','Users','reqshark' on unix/osx
s.shift();s.shift();s.shift();s.shift();
// look at dir names starting under the home folder
switch(s[0]){
// get more specific than the s[0] case, like s[1], s[2], for `myprojects` etc.
// custom cases for local file system project directory
// resolve only if we're in the `reqshark directory` this time
case 'money' : return r (s,require('path').resolve(msg)) // call r(file)
default : console.log('unregistered path')
}
}
// r() reads and formats our file data as a utf8 value assigned to `d` or `data` property
// on the outbound packet published over nanomsg. `p` key for path info needed for writing,
// reassemble user's project tree later (not too long w/ nanomsg) at remote server or sub sockets
function r ( s, dir ){
// require('fs').readFile(dir, 'utf8', function (er,dat) {
// pub.send( JSON.stringify({p:s.join('/'),d:dat}) )
// })
vdom(); styl() // may want to rebuild dom/css
}
server.bind( 46000,'127.0.0.1' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment