Skip to content

Commit

Permalink
Write stringified JSON from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 23, 2016
1 parent 2924480 commit 8136bbd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bin/obj2json.js
Expand Up @@ -8,7 +8,7 @@ var filename = process.argv[2]
// If a filename was specified, read it and write to stdout
if (filename) {
var wavefrontString = fs.readFileSync(path.resolve(process.cwd(), filename)).toString('utf8')
console.log(obj2json(wavefrontString))
console.log(JSON.stringify(obj2json(wavefrontString)))
}

// If no filename was specified, read from stdin and write to stdout
Expand All @@ -18,6 +18,6 @@ if (!filename) {
bufferedWavefrontString += chunk
})
process.stdin.on('end', function () {
console.log(obj2json(bufferedWavefrontString.toString('utf8')))
console.log(JSON.stringify(obj2json(bufferedWavefrontString.toString('utf8'))))
})
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"obj2json": "./bin/obj2json.js"
},
"scripts": {
"test": "standard && node test/wavefront-obj-parser.js"
"test": "standard && node test/index.js"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion test/wavefront-obj-parser.js → test/api.js
Expand Up @@ -5,7 +5,7 @@ var path = require('path')
var objParse = require('../')

var cubeWavefrontObjString = fs.readFileSync(path.resolve(__dirname, './fixture/checkercube.obj')).toString('utf8')
var expectedCubeJSON = require('./checkercube-expected.js')
var expectedCubeJSON = require('./expected/checkercube-expected.js')

test('Parse vertex, uv, normal .obj string', function (t) {
t.deepEqual(objParse(cubeWavefrontObjString), expectedCubeJSON)
Expand Down
39 changes: 39 additions & 0 deletions test/cli.js
@@ -0,0 +1,39 @@
var test = require('tape')

var child_process = require('child_process')
var fs = require('fs')
var path = require('path')
var stream = require('stream')

var parserCLI = path.resolve(__dirname, '../bin/obj2json.js')

var checkerCubeFixtureName = path.resolve(__dirname, './fixture/checkercube.obj')
var expectedObj = require('./expected/checkercube-expected.js')
var checkerCubeFixtureData = fs.readFileSync(checkerCubeFixtureName).toString('utf8')

test('read file name', function (t) {
t.plan(1)
child_process.execFile(parserCLI, [checkerCubeFixtureName], function (_, stdout) {
t.deepEqual(expectedObj, JSON.parse(stdout))
})
})

test('read from stdin', function (t) {
t.plan(1)
var parsedJSON = ''

var wfStream = new stream.Writable()
wfStream._write = function (chunk, encoding, next) {
parsedJSON += chunk
next()
}
wfStream.on('unpipe', function () {
t.deepEqual(expectedObj, JSON.parse(parsedJSON))
})

var testStdin = child_process.spawn(parserCLI)

testStdin.stdout.pipe(wfStream)
testStdin.stdin.write(checkerCubeFixtureData)
testStdin.stdin.end()
})
File renamed without changes.
5 changes: 5 additions & 0 deletions test/index.js
@@ -0,0 +1,5 @@
// Test api
require('./api.js')

// Test cli
require('./cli.js')

0 comments on commit 8136bbd

Please sign in to comment.