Skip to content

Commit

Permalink
convert-action: Adjust how stream is outputted
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 1, 2024
1 parent 8aa3d16 commit a4d4bc7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/jskos-convert
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ if (type) {

const files = args.length ? args : ["-"]
const options = program.opts()
options.exitProcess = true

convert(options, type, files)
22 changes: 19 additions & 3 deletions lib/convert-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import fs from "fs"
import needle from "needle"
import convert from "./jskos-convert.js"
import { ConceptScheme } from "jskos-tools"
import { Writable } from "stream"

export default (options, type, files) => {

const to = options.to || "ndjson"
let { scheme, destination } = options
let { scheme, destination, exitProcess = false } = options

let registry = {}
if (options.registry) {
let json = JSON.parse(fs.readFileSync(options.registry))
registry = Array.isArray(json) ? { schemes: json } : json
}
const output = process.stdout
const output = new Writable({
write(chunk, encoding, callback) {
process.stdout.write(chunk,callback)
},
})

output.on("error", err => {
if (err.code == "EPIPE") {
Expand All @@ -30,6 +35,8 @@ export default (options, type, files) => {
destination = new ConceptScheme(JSON.parse(fs.readFileSync(destination).toString()))
}

let errorCode = 0, lastStream

while (files.length > 0) {
let file = files.shift()
let input = process.stdin
Expand Down Expand Up @@ -58,9 +65,18 @@ export default (options, type, files) => {
for (let i=0; i<steps.length; i++) {
stream = stream.pipe(steps[i])
}
stream.pipe(output)
lastStream = stream.pipe(output)
} catch(e) {
console.error(e.message)
errorCode = 1
}
}

// Exit process with error code if option is given
if (exitProcess) {
lastStream?.on("finish", () => {
process.exit(errorCode)
}) || process.exit(errorCode)
}

}

0 comments on commit a4d4bc7

Please sign in to comment.