| 1 | #!/usr/bin/env node
|
|---|
| 2 |
|
|---|
| 3 | 'use strict'
|
|---|
| 4 |
|
|---|
| 5 | const fs = require('fs')
|
|---|
| 6 | const path = require('path')
|
|---|
| 7 | const check = require('check-types')
|
|---|
| 8 | const bfj = require('../src')
|
|---|
| 9 |
|
|---|
| 10 | const inPath = getDataPath('.json');
|
|---|
| 11 |
|
|---|
| 12 | let time = process.hrtime()
|
|---|
| 13 |
|
|---|
| 14 | if (process.argv.length === 4) {
|
|---|
| 15 | const stuff = []
|
|---|
| 16 | const stream = bfj.match(fs.createReadStream(inPath), process.argv[3])
|
|---|
| 17 | stream.on('data', thing => stuff.push(thing))
|
|---|
| 18 | stream.on('end', () => {
|
|---|
| 19 | reportTime()
|
|---|
| 20 | console.log('hooray!', stuff.length)
|
|---|
| 21 | fs.writeFileSync(getDataPath('-result.ndjson'), stuff.map(s => JSON.stringify(s)).join('\n'), {
|
|---|
| 22 | encoding: 'utf8',
|
|---|
| 23 | })
|
|---|
| 24 | process.exit(0)
|
|---|
| 25 | })
|
|---|
| 26 | stream.on('error', error => {
|
|---|
| 27 | console.error('error!', error.stack)
|
|---|
| 28 | process.exit(1)
|
|---|
| 29 | })
|
|---|
| 30 | stream.on('dataError', error => {
|
|---|
| 31 | console.error('dataError!', error.stack)
|
|---|
| 32 | process.exit(2)
|
|---|
| 33 | })
|
|---|
| 34 | } else {
|
|---|
| 35 | console.log('reading json')
|
|---|
| 36 | bfj.read(inPath)
|
|---|
| 37 | .then(data => {
|
|---|
| 38 | reportTime()
|
|---|
| 39 | console.log('writing json')
|
|---|
| 40 | return bfj.write(getDataPath('-result.json'), data)
|
|---|
| 41 | })
|
|---|
| 42 | .then(() => done('succeeded'))
|
|---|
| 43 | .catch(error => done(error.stack, 1))
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | function getDataPath (suffix) {
|
|---|
| 47 | return path.resolve(__dirname, process.argv[2] + suffix)
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | function reportTime () {
|
|---|
| 51 | let interimTime = process.hrtime(time)
|
|---|
| 52 | console.log('%d seconds and %d nanoseconds', interimTime[0], interimTime[1])
|
|---|
| 53 | time = process.hrtime()
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | function done (message, code) {
|
|---|
| 57 | reportTime()
|
|---|
| 58 | console.log(message)
|
|---|
| 59 | process.exit(code)
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|