Wraps gid.encodeGid, prints the packed-u32 as decimal on stdout, emits usage on stderr on bad arity, and a labelled error on out-of-range or non-integer input.
33 lines
803 B
JavaScript
33 lines
803 B
JavaScript
'use strict';
|
|
|
|
const { encodeGid } = require('../gid');
|
|
|
|
const USAGE = 'usage: sporel-mapper encode <atlas_idx> <tile_id> [rotation]\n';
|
|
|
|
function parseIntStrict(s, field) {
|
|
if (!/^-?\d+$/.test(s)) {
|
|
throw new RangeError(`${field} '${s}' is not an integer`);
|
|
}
|
|
return parseInt(s, 10);
|
|
}
|
|
|
|
function run(args, { out, err }) {
|
|
if (args.length < 2 || args.length > 3) {
|
|
err.write(USAGE);
|
|
return 1;
|
|
}
|
|
try {
|
|
const atlas = parseIntStrict(args[0], 'atlas');
|
|
const tile = parseIntStrict(args[1], 'tile');
|
|
const rotation = args.length === 3 ? parseIntStrict(args[2], 'rotation') : 0;
|
|
const gid = encodeGid(atlas, tile, rotation);
|
|
out.write(`${gid}\n`);
|
|
return 0;
|
|
} catch (e) {
|
|
err.write(`error: ${e.message}\n`);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
module.exports = { run };
|