|
| 1 | +#!/usr/bin/env node |
| 2 | +import { existsSync, readFileSync, writeFileSync } from 'node:fs' |
| 3 | +import { dirname, join } from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { parseArgs } from 'node:util' |
| 6 | +import { jsonquery } from '../lib/jsonquery.js' |
| 7 | +import { help } from './help.js' |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url) |
| 10 | +const __dirname = dirname(__filename) |
| 11 | + |
| 12 | +const options = { |
| 13 | + input: { type: 'string' }, |
| 14 | + query: { type: 'string' }, |
| 15 | + output: { type: 'string' }, |
| 16 | + format: { type: 'string', default: 'text' }, |
| 17 | + overwrite: { type: 'boolean', default: false }, |
| 18 | + indentation: { type: 'string', default: ' ' }, |
| 19 | + version: { type: 'boolean', short: 'v' }, |
| 20 | + help: { type: 'boolean', short: 'h' } |
| 21 | +} |
| 22 | + |
| 23 | +const { |
| 24 | + values, |
| 25 | + positionals: [inlineQuery] |
| 26 | +} = parseArgs({ options, allowPositionals: true }) |
| 27 | + |
| 28 | +await run({ ...values, inlineQuery }) |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {Options} options |
| 32 | + * @returns {Promise<void>} |
| 33 | + */ |
| 34 | +async function run(options) { |
| 35 | + if (options.version) { |
| 36 | + return writeVersion() |
| 37 | + } |
| 38 | + |
| 39 | + if (options.help) { |
| 40 | + return writeHelp() |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + const input = await readInput(options) |
| 45 | + const query = readQuery(options) |
| 46 | + |
| 47 | + const output = jsonquery(input, query) |
| 48 | + |
| 49 | + return writeOutput(options, output) |
| 50 | + } catch (err) { |
| 51 | + process.stderr.write(err.toString()) |
| 52 | + process.exit(1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @param {Options} options |
| 58 | + * @returns {Promise<string>} |
| 59 | + */ |
| 60 | +async function readInput(options) { |
| 61 | + const inputStr = options.input ? fileToString(options.input) : await streamToString(process.stdin) |
| 62 | + |
| 63 | + if (inputStr.trim() === '') { |
| 64 | + throw Error('No input data provided') |
| 65 | + } |
| 66 | + |
| 67 | + return JSON.parse(inputStr) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @param {Options} options |
| 72 | + * @returns {Promise<string>} |
| 73 | + */ |
| 74 | +function readQuery(options) { |
| 75 | + const queryStr = options.query |
| 76 | + ? fileToString(options.query) |
| 77 | + : options.inlineQuery |
| 78 | + ? options.inlineQuery |
| 79 | + : throwError('No query provided') |
| 80 | + |
| 81 | + return options.format === 'text' || options.format === undefined |
| 82 | + ? queryStr |
| 83 | + : options.format === 'json' |
| 84 | + ? JSON.parse(queryStr) |
| 85 | + : throwError(`Unknown format "${options.format}". Choose either "text" (default) or "json".`) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {Options} options |
| 90 | + * @param {JSON} output |
| 91 | + */ |
| 92 | +function writeOutput(options, output) { |
| 93 | + const outputStr = JSON.stringify(output, null, options.indentation) |
| 94 | + |
| 95 | + if (options.output) { |
| 96 | + if (existsSync(options.output) && !options.overwrite) { |
| 97 | + throwError(`Cannot overwrite existing file "${options.output}"`) |
| 98 | + } |
| 99 | + |
| 100 | + writeFileSync(options.output, outputStr) |
| 101 | + } else { |
| 102 | + process.stdout.write(outputStr) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function writeVersion() { |
| 107 | + const file = join(__dirname, '../package.json') |
| 108 | + const pkg = JSON.parse(String(readFileSync(file, 'utf-8'))) |
| 109 | + |
| 110 | + process.stdout.write(pkg.version) |
| 111 | +} |
| 112 | + |
| 113 | +function writeHelp() { |
| 114 | + process.stdout.write(help) |
| 115 | +} |
| 116 | + |
| 117 | +function fileToString(fileName) { |
| 118 | + return String(readFileSync(fileName)) |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * @param {ReadableStream} readableStream |
| 123 | + * @returns {Promise<string>} |
| 124 | + */ |
| 125 | +function streamToString(readableStream) { |
| 126 | + return new Promise((resolve, reject) => { |
| 127 | + let text = '' |
| 128 | + |
| 129 | + readableStream.on('data', (chunk) => { |
| 130 | + text += String(chunk) |
| 131 | + }) |
| 132 | + readableStream.on('end', () => { |
| 133 | + readableStream.destroy() |
| 134 | + resolve(text) |
| 135 | + }) |
| 136 | + readableStream.on('error', (err) => reject(err)) |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +function throwError(message) { |
| 141 | + throw new Error(message) |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @typedef {Object} Options |
| 146 | + * @property {boolean} [version] |
| 147 | + * @property {boolean} [help] |
| 148 | + * @property {string} [input] |
| 149 | + * @property {string} [query] |
| 150 | + * @property {string} [output] |
| 151 | + * @property {string} [inlineQuery] |
| 152 | + * @property {'text' | 'json'} [format='text'] |
| 153 | + * @property {string} [indentation=' '] |
| 154 | + * @property {boolean} [overwrite=false] |
| 155 | + */ |
0 commit comments