Skip to content

Commit

Permalink
feat: replaced individual commands with a single unified command
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Jul 8, 2022
1 parent 5bdb393 commit e73de13
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 841 deletions.
56 changes: 33 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,51 @@ Converts Arx Fatalis level data to JSON, YAML, BSON and vice versa.

`npm i arx-level-json-converter -g`

This will give you access to the following commands:
This will give you access to the following commands, both do the same:

- `to-json`
- `from-json`
- `to-yaml`
- `from-yaml`
- `to-bson`
- `from-bson`
- `combine`
- `separate`
- `arx-level-json-converter`
- `arx-convert`

## examples
### format of the commands

`arx-convert <inputfile> --from=<format> --to=<format> --output=<outputfile>`

`--version` or `-v` will give you the version for any tool
the inputfile and --output parameters can be omitted and then the code can be used in pipelines

`cat fast.fts | to-json --ext=fts --pretty > fast.fts.json`
`cat <inputfile> | arx-convert --from=<format> --to=<format> > <outputfile>`

`to-json level8.dlf --output=level8.dlf.min.json`
the format parameter can be one of the following arx formats: `dlf`, `llf`, `fts` and `ftl`(WIP)

`cat level8.dlf.min.json | from-json --ext=dlf > level8.dlf.repacked`
and it can also a data format for the other side: `json`, `bson` and `yaml`(can also be spelled as `yml`)

## examples

`from-json fast.fts.json --ext=fts --output=fast.fts.repacked`
```sh
# print out version
--version
-v

`cat level8.dlf.unpacked | to-yaml --ext=dlf > level8.dlf.yml`
# convert and fts file to a json through piping
cat fast.fts | arx-convert --from=fts --to=json --pretty > fast.fts.json

`cat level8.dlf.yml | from-yaml --ext=dlf > level8.dlf.unpacked`
# convert a dlf file to a minified json through files
arx-convert level8.dlf --from=dlf --to=json --output=level8.dlf.min.json

`cat level8.dlf.unpacked | to-bson --ext=dlf > level8.dlf.bson`
# convert a json to a dlf
cat level8.dlf.min.json | arx-convert --from=json --to=dlf > level8.dlf.repacked

`cat level8.dlf.bson | from-bson --ext=dlf > level8.dlf.unpacked`
# convert json to fts
arx-convert fast.fts.json --from=json --to=fts --output=fast.fts.repacked

`combine fast.fts.json level8.llf.json level8.dlf.json --pretty --output=level8.json`
# convert dlf to yaml
cat level8.dlf.unpacked | arx-convert --to=yaml --from=dlf > level8.dlf.yml

`combine fast.fts.json level8.llf.json level8.dlf.json --pretty > level8.json`
# convert yaml to dlf
cat level8.dlf.yml | arx-convert --from=yaml --to=dlf > level8.dlf.unpacked

`separate level8.json --pretty --llf=level8.llf.json --dlf=level8.dlf.json --fts=fast.fts.json`
# convert dlf to bson
cat level8.dlf.unpacked | arx-convert --to=bson --from=dlf > level8.dlf.bson

`cat level8.json | separate --pretty --llf=level8.llf.json --dlf=level8.dlf.json --fts=fast.fts.json`
# convert bson to dlf
cat level8.dlf.bson | arx-convert --from=bson --to=dlf > level8.dlf.unpacked
```
170 changes: 0 additions & 170 deletions bin/combine.js

This file was deleted.

6 changes: 4 additions & 2 deletions bin/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const SUPPORTED_EXTENSIONS = ["dlf", "fts", "llf", "ftl"];
const SUPPORTED_ARX_FORMATS = ["dlf", "fts", "llf", "ftl"];
const SUPPORTED_DATA_FORMATS = ["json", "yaml", "yml", "bson"];

module.exports = {
SUPPORTED_EXTENSIONS,
SUPPORTED_ARX_FORMATS,
SUPPORTED_DATA_FORMATS,
};
123 changes: 123 additions & 0 deletions bin/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env node

const fs = require("fs");
const minimist = require("minimist-lite");
const BSON = require("bson");
const YAML = require("yaml");
const {
fileExists,
getPackageVersion,
streamToBuffer,
stringifyYAML,
stringifyJSON,
stringifyBSON,
outputInChunks,
validateFromToPair,
} = require("./helpers.js");
const { DLF, FTS, LLF, FTL } = require("../src/index.js");

// ---------------------------

const args = minimist(process.argv.slice(2), {
string: ["output", "from", "to"],
boolean: ["version", "pretty"],
alias: {
v: "version",
p: "pretty",
},
});

if (args.version) {
console.log(getPackageVersion());
process.exit(0);
}

let filename = args._[0];
let output = args.output;
let hasErrors = false;

(async () => {
let input;
if (filename) {
if (await fileExists(filename)) {
input = fs.createReadStream(filename);
} else {
console.error("error: input file does not exist");
hasErrors = true;
}
} else {
input = process.openStdin();
}

try {
validateFromToPair(args.from, args.to);
} catch (e) {
console.error(`error: ${e.message}`);
hasErrors = true;
}

if (output) {
output = fs.createWriteStream(output);
} else {
output = process.stdout;
}

if (hasErrors) {
process.exit(1);
}

const rawIn = await streamToBuffer(input);
let parsedIn;
switch (args.from) {
case "json":
parsedIn = JSON.parse(rawIn);
break;
case "yml":
case "yaml":
parsedIn = YAML.parse(rawIn.toString());
break;
case "bson":
parsedIn = BSON.deserialize(rawIn);
break;
case "dlf":
parsedIn = DLF.load(rawIn);
break;
case "fts":
parsedIn = FTS.load(rawIn);
break;
case "llf":
parsedIn = LLF.load(rawIn);
break;
case "ftl":
parsedIn = FTL.load(rawIn);
break;
}

let rawOut;
switch (args.to) {
case "json":
rawOut = stringifyJSON(parsedIn, args.pretty);
break;
case "yml":
case "yaml":
rawOut = stringifyYAML(parsedIn);
break;
case "bson":
rawOut = stringifyBSON(parsedIn);
break;
case "dlf":
rawOut = DLF.save(parsedIn);
break;
case "fts":
rawOut = FTS.save(parsedIn);
break;
case "llf":
rawOut = LLF.save(parsedIn);
break;
case "ftl":
rawOut = FTL.save(parsedIn);
break;
}

outputInChunks(rawOut, output);
})();
Loading

0 comments on commit e73de13

Please sign in to comment.