Skip to content

Commit

Permalink
Support reading and writing from stdin (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
  • Loading branch information
Kas-tle authored Mar 11, 2024
1 parent 6b2d048 commit 457a088
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package-lock.json
*.nbt
*.dat
*.json
!package.json
!package.json
yarn.lock
39 changes: 30 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ Write an JSON file to uncompressed NBT (defaults to big endian):
nbt-dump write level.json to level.dat
nbt-dump write level.json to level.dat as little
You can also pipe the input to nbt-dump:
cat level.dat | nbt-dump
cat level.dat | nbt-dump to level.json
cat level.json | nbt-dump write
cat level.json | nbt-dump write to level.dat
`

function main(args, argsStr) {
if (args.length == 0 || argsStr.includes('help')) {
async function main(args, argsStr) {
if ((args.length == 0 || argsStr.includes('help')) && !!process.stdin.isTTY) {
console.warn(usage)
process.exit(1)
}
if (args[0] == 'read') args.shift()
let files = []
if (!process.stdin.isTTY) files.push(undefined)
for (var arg of args) {
if (arg.includes('.')) files.push(arg)
}
Expand All @@ -35,8 +42,8 @@ function main(args, argsStr) {
}
if (args[0] == 'write') {
if (!files.length && args[1]) files.push(args[1])
if (files.length == 1) files.push(files[0] + '.nbt')
if (files.length == 2) return write(...files, getFmt())
if (files.length == 1) files.push(files[0] || 'stdin' + '.nbt')
if (files.length == 2) return (await write(...files, getFmt()))
} else {
if (!files.length) files = [args[0], args[0] + '.json']
return read(files[0], files[1], getFmt())
Expand All @@ -46,10 +53,17 @@ function main(args, argsStr) {
console.warn(arguments)
}

function write(inpf, outf, fmt) {
console.log(`* Writing JSON from "${inpf}" to "${outf}" as ${fmt || 'big'} endian`)
async function write(inpf, outf, fmt) {
console.log(`* Writing JSON from "${inpf || 'stdin'}" to "${outf}" as ${fmt || 'big'} endian`)

const json = JSON.parse(fs.readFileSync(inpf))
let json;
if (!inpf) {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk);
json = JSON.parse(Buffer.concat(chunks).toString())
} else {
json = JSON.parse(fs.readFileSync(inpf))
}
const outBuffer = fs.createWriteStream(outf)

try {
Expand All @@ -64,9 +78,16 @@ function write(inpf, outf, fmt) {
}

async function read(inpf, outf, fmt) {
console.log(`* Dumping NBT file "${inpf}" to "${outf || 'stdout'}" as JSON ${fmt ? 'as ' + fmt : ''}`)
console.log(`* Dumping NBT ${inpf ? 'file "' + inpf + '"' : '"stdin"'} to "${outf || 'stdout'}" as JSON ${fmt ? 'as ' + fmt : ''}`)

const buffer = await fs.promises.readFile(inpf)
let buffer;
if (!inpf) {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk);
buffer = Buffer.concat(chunks)
} else {
buffer = await fs.promises.readFile(inpf)
}
const { parsed, type } = await nbt.parse(buffer, fmt)

if (!fmt) console.log(`(as ${type} endian)`)
Expand Down

0 comments on commit 457a088

Please sign in to comment.