Skip to content

Commit

Permalink
Repo maintenance updates (lint, fix tests) (#4)
Browse files Browse the repository at this point in the history
* maintenence updates (lint, fix tests)

* update readme and file names

* fix gitignore + badge

* test

* fix pipe handling triggering on CI

* debug

* fix

* ci is pain
  • Loading branch information
extremeheat authored Mar 11, 2024
1 parent 457a088 commit 29d9477
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules/
package-lock.json
*.nbt
*.dat
*.json
!package.json
yarn.lock
yarn.lock

# Generated test output
test/bigtest.json
6 changes: 6 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
tasks:
- init: npm install
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 extremeheat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# nbt-dump
[![NPM version](https://img.shields.io/npm/v/nbt-dump.svg)](http://npmjs.com/package/nbt-dump)
[![Build Status](https://github.com/extremeheat/nbt-dump/actions/workflows/ci.yml/badge.svg)](https://github.com/extremeheat/nbt-dump/actions/workflows/)
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/extremeheat/nbt-dump)

A simple command line Node.js tool to read and write NBT files to JSON and back. Supports big, little and little-varint encoding.

Expand All @@ -23,14 +26,23 @@ Parse an NBT file to JSON:
nbt-dump <path-to-nbt-file> [out-json-file] [little|big|varint]
nbt-dump level.dat
(Dump the contents of level.dat to terminal)
nbt-dump level.dat to level.json
(Dump the contents of level.dat to JSON)
nbt-dump level.dat as little to level.json
(Dump the contents of little endian encoded level.dat to JSON)
Write an JSON file to uncompressed NBT (defaults to big endian):
nbt-dump write <path-to-json> [out-nbt-file] [little|big|varint]
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
```

### Example
Expand Down
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
const { main } = require('./lib')
const args = process.argv.slice(2)
main(args, args.join(' '))
47 changes: 29 additions & 18 deletions index.js → lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
const fs = require('fs')
const nbt = require('prismarine-nbt')

Expand All @@ -23,15 +22,29 @@ You can also pipe the input to nbt-dump:
cat level.json | nbt-dump write to level.dat
`

async function main(args, argsStr) {
if ((args.length == 0 || argsStr.includes('help')) && !!process.stdin.isTTY) {
// async function isPipedInput (fd = 0) {
// return new Promise((resolve, reject) => {
// fs.fstat(fd, function (err, stats) {
// if (err) {
// reject(err)
// } else {
// resolve(stats.isFIFO())
// }
// })
// })
// }

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()
if (args[0] === 'read') args.shift()
let files = []
if (!process.stdin.isTTY) files.push(undefined)
for (var arg of args) {
if (!process.stdin.isTTY && !process.env.CI) {
files.push(undefined)
}
for (const arg of args) {
if (arg.includes('.')) files.push(arg)
}
const getFmt = () => {
Expand All @@ -40,10 +53,10 @@ async function main(args, argsStr) {
if (args.includes('little')) return 'little'
return undefined
}
if (args[0] == 'write') {
if (args[0] === 'write') {
if (!files.length && args[1]) files.push(args[1])
if (files.length == 1) files.push(files[0] || 'stdin' + '.nbt')
if (files.length == 2) return (await 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 @@ -53,13 +66,13 @@ async function main(args, argsStr) {
console.warn(arguments)
}

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

let json;
let json
if (!inpf) {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk);
for await (const chunk of process.stdin) chunks.push(chunk)
json = JSON.parse(Buffer.concat(chunks).toString())
} else {
json = JSON.parse(fs.readFileSync(inpf))
Expand All @@ -74,16 +87,15 @@ async function write(inpf, outf, fmt) {
console.warn('Failed to write. Make sure that the JSON schema matches the prismarine-nbt ProtoDef schema. See https://github.com/PrismarineJS/prismarine-nbt')
throw e
}
return
}

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

let buffer;
let buffer
if (!inpf) {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk);
for await (const chunk of process.stdin) chunks.push(chunk)
buffer = Buffer.concat(chunks)
} else {
buffer = await fs.promises.readFile(inpf)
Expand All @@ -101,5 +113,4 @@ async function read(inpf, outf, fmt) {
return true
}

const args = process.argv.slice(2)
main(args, args.join(' '))
module.exports = { main, write, read }
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
"name": "nbt-dump",
"version": "1.0.2",
"description": "Read and write Minecraft Named Binary Tag files in big, little and little-varint encoding",
"main": "index.js",
"main": "lib.js",
"bin": {
"nbt-dump": "./index.js"
"nbt-dump": "./cli.js"
},
"scripts": {
"test": "node test.js"
"lint": "standard",
"fix": "standard --fix",
"pretest": "npm run lint",
"test": "mocha --bail --exit"
},
"repository": {
"type": "git",
Expand All @@ -30,5 +33,10 @@
"homepage": "https://github.com/extremeheat/nbt-dump#readme",
"dependencies": {
"prismarine-nbt": "^2.0.0"
},
"devDependencies": {
"mocha": "^10.3.0",
"standard": "^17.1.0",
"nbt-dump": "file:."
}
}
4 changes: 0 additions & 4 deletions test.js

This file was deleted.

16 changes: 16 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env mocha */
/* eslint-disable n/no-path-concat */
const lib = require('nbt-dump')
const fs = require('fs')
const assert = require('assert')

describe('basic tests', () => {
it('writes bigtest.nbt to bigtest.json', async () => {
const bigNBT = `${__dirname}/bigtest.nbt`
const bigJSON = `${__dirname}/bigtest.json`
fs.rmSync(bigJSON, { force: true })
const args = [bigNBT, bigJSON]
await lib.main(args, args.join(' '))
assert(fs.existsSync(bigJSON))
})
})
Binary file added test/bigtest.nbt
Binary file not shown.

0 comments on commit 29d9477

Please sign in to comment.