Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Touch Elm files and recompiles
/*
This script touches each elm file
And recopiles it, then stores the time taken
*/
const r = require("ramda")
const dir = "./src/Elm"
const fs = require("fs")
const childProcess = require('child_process')
const async = require('async')
const touch = require("touch")
const prettyHrtime = require('pretty-hrtime')
const klaw = require('klaw')
const through2 = require('through2')
const resultFile = 'elm-time-stats.txt'
const excludeDirFilter = through2.obj(function (item, enc, next) {
if (!item.stats.isDirectory()) this.push(item)
next()
})
const ignoreApi = through2.obj(function (item, enc, next) {
if (!item.path.match(/Api\//)) this.push(item)
next()
})
const ignoreTest = through2.obj(function (item, enc, next) {
if (!item.path.match(/Test.elm/)) this.push(item)
next()
})
const files = []
const stats = []
klaw(dir)
.pipe(excludeDirFilter)
.pipe(ignoreApi)
.pipe(ignoreTest)
.on('data', item => files.push(item.path))
.on('end', processFiles)
function processFiles() {
var sample = r.take(6, files)
async.eachSeries(sample, processFile, onFinish)
}
function processFile(file, next) {
console.log("=== " + file)
// touch the file
touch.sync(file)
const start = process.hrtime()
const pro = childProcess.spawn('make', ['elm-build']);
pro.stdout.on('data', (data) => {
if (data.toString().match(/Compiled/) ) {
const end = process.hrtime(start)
console.log(end)
const stat = {
file: file,
time: end,
data: data,
}
console.log(outputForStat(stat))
// fs.appendFileSync(resultFile, line);
stats.push(stat)
}
});
pro.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
pro.on('close', (code) => {
// console.log(`child process exited with code ${code}`);
next(false, file)
});
}
function onFinish(err) {
var output = r.pipe(
r.sortBy(s => s.time[0]),
r.map(outputForStat),
r.join('\n')
)(stats)
fs.writeFileSync(resultFile, output)
}
function outputForStat(stat) {
return stat.file + "\n" + prettyHrtime(stat.time) + " \n " + stat.data + "\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.