initial commit.

This commit is contained in:
2022-06-06 02:04:50 -04:00
commit 62de970149
1557 changed files with 256441 additions and 0 deletions

183
node_modules/json2csv/bin/json2csv.js generated vendored Executable file
View File

@@ -0,0 +1,183 @@
#!/usr/bin/env node
'use strict';
const { promisify } = require('util');
const { createReadStream, createWriteStream, readFile: readFileOrig, writeFile: writeFileOrig } = require('fs');
const os = require('os');
const { isAbsolute, join } = require('path');
const program = require('commander');
const pkg = require('../package');
const json2csv = require('../lib/json2csv');
const parseNdJson = require('./utils/parseNdjson');
const TablePrinter = require('./utils/TablePrinter');
const readFile = promisify(readFileOrig);
const writeFile = promisify(writeFileOrig);
const { unwind, flatten } = json2csv.transforms;
const JSON2CSVParser = json2csv.Parser;
const Json2csvTransform = json2csv.Transform;
program
.version(pkg.version)
.option('-i, --input <input>', 'Path and name of the incoming json file. Defaults to stdin.')
.option('-o, --output <output>', 'Path and name of the resulting csv file. Defaults to stdout.')
.option('-c, --config <path>', 'Specify a file with a valid JSON configuration.')
.option('-n, --ndjson', 'Treat the input as NewLine-Delimited JSON.')
.option('-s, --no-streaming', 'Process the whole JSON array in memory instead of doing it line by line.')
.option('-f, --fields <fields>', 'List of fields to process. Defaults to field auto-detection.')
.option('-v, --default-value <defaultValue>', 'Default value to use for missing fields.')
.option('-q, --quote <quote>', 'Character(s) to use as quote mark. Defaults to \'"\'.')
.option('-Q, --escaped-quote <escapedQuote>', 'Character(s) to use as a escaped quote. Defaults to a double `quote`, \'""\'.')
.option('-d, --delimiter <delimiter>', 'Character(s) to use as delimiter. Defaults to \',\'.', ',')
.option('-e, --eol <eol>', 'Character(s) to use as End-of-Line for separating rows. Defaults to \'\\n\'.', os.EOL)
.option('-E, --excel-strings','Wraps string data to force Excel to interpret it as string even if it contains a number.')
.option('-H, --no-header', 'Disable the column name header.')
.option('-a, --include-empty-rows', 'Includes empty rows in the resulting CSV output.')
.option('-b, --with-bom', 'Includes BOM character at the beginning of the CSV.')
.option('-p, --pretty', 'Print output as a pretty table. Use only when printing to console.')
// Built-in transforms
.option('--unwind [paths]', 'Creates multiple rows from a single JSON document similar to MongoDB unwind.')
.option('--unwind-blank', 'When unwinding, blank out instead of repeating data. Defaults to false.', false)
.option('--flatten-objects', 'Flatten nested objects. Defaults to false.', false)
.option('--flatten-arrays', 'Flatten nested arrays. Defaults to false.', false)
.option('--flatten-separator <separator>', 'Flattened keys separator. Defaults to \'.\'.', '.')
.parse(process.argv);
function makePathAbsolute(filePath) {
return (filePath && !isAbsolute(filePath))
? join(process.cwd(), filePath)
: filePath;
}
program.input = makePathAbsolute(program.input);
program.output = makePathAbsolute(program.output);
program.config = makePathAbsolute(program.config);
// don't fail if piped to e.g. head
/* istanbul ignore next */
process.stdout.on('error', (error) => {
if (error.code === 'EPIPE') process.exit(1);
});
function getInputStream(inputPath) {
if (inputPath) return createReadStream(inputPath, { encoding: 'utf8' });
process.stdin.resume();
process.stdin.setEncoding('utf8');
return process.stdin;
}
function getOutputStream(outputPath, config) {
if (outputPath) return createWriteStream(outputPath, { encoding: 'utf8' });
if (config.pretty) return new TablePrinter(config).writeStream();
return process.stdout;
}
async function getInput(inputPath, ndjson) {
if (!inputPath) return getInputFromStdin();
if (ndjson) return parseNdJson(await readFile(inputPath, 'utf8'));
return require(inputPath);
}
async function getInputFromStdin() {
return new Promise((resolve, reject) => {
process.stdin.resume();
process.stdin.setEncoding('utf8');
let inputData = '';
process.stdin.on('data', chunk => (inputData += chunk));
/* istanbul ignore next */
process.stdin.on('error', err => reject(new Error('Could not read from stdin', err)));
process.stdin.on('end', () => {
try {
resolve(program.ndjson ? parseNdJson(inputData) : JSON.parse(inputData));
} catch (err) {
reject(new Error('Invalid data received from stdin', err));
}
});
});
}
async function processOutput(outputPath, csv, config) {
if (!outputPath) {
// eslint-disable-next-line no-console
config.pretty ? (new TablePrinter(config)).printCSV(csv) : console.log(csv);
return;
}
await writeFile(outputPath, csv);
}
async function processInMemory(config, opts) {
const input = await getInput(program.input, config.ndjson);
const output = new JSON2CSVParser(opts).parse(input);
await processOutput(program.output, output, config);
}
async function processStream(config, opts) {
const input = getInputStream(program.input);
const transform = new Json2csvTransform(opts);
const output = getOutputStream(program.output, config);
await new Promise((resolve, reject) => {
input.pipe(transform).pipe(output);
input.on('error', reject);
transform.on('error', reject);
output.on('error', reject)
.on('finish', resolve);
});
}
(async (program) => {
try {
const config = Object.assign({}, program.config ? require(program.config) : {}, program);
const transforms = [];
if (config.unwind) {
transforms.push(unwind({
paths: config.unwind === true ? undefined : config.unwind.split(','),
blankOut: config.unwindBlank
}));
}
if (config.flattenObjects || config.flattenArrays) {
transforms.push(flatten({
objects: config.flattenObjects,
arrays: config.flattenArrays,
separator: config.flattenSeparator
}));
}
const opts = {
transforms,
fields: config.fields
? (Array.isArray(config.fields) ? config.fields : config.fields.split(','))
: config.fields,
defaultValue: config.defaultValue,
quote: config.quote,
escapedQuote: config.escapedQuote,
delimiter: config.delimiter,
eol: config.eol,
excelStrings: config.excelStrings,
header: config.header,
includeEmptyRows: config.includeEmptyRows,
withBOM: config.withBom
};
await (config.streaming ? processStream : processInMemory)(config, opts);
} catch(err) {
let processedError = err;
if (program.input && err.message.includes(program.input)) {
processedError = new Error(`Invalid input file. (${err.message})`);
} else if (program.output && err.message.includes(program.output)) {
processedError = new Error(`Invalid output file. (${err.message})`);
} else if (program.config && err.message.includes(program.config)) {
processedError = new Error(`Invalid config file. (${err.message})`);
}
// eslint-disable-next-line no-console
console.error(processedError);
process.exit(1);
}
})(program);

117
node_modules/json2csv/bin/utils/TablePrinter.js generated vendored Normal file
View File

@@ -0,0 +1,117 @@
'use strict';
const { Writable } = require('stream');
const MIN_CELL_WIDTH = 15;
class TablePrinter {
constructor(opts) {
this.opts = opts;
this._hasWritten = false;
}
push(csv) {
const lines = csv.split(this.opts.eol);
if (!lines.length) return;
if (!this._hasWritten) this.setColumnWidths(lines[0]);
const top = this._hasWritten ? this.middleLine : this.topLine;
this.print(top, lines);
this._hasWritten = true;
}
end(csv) {
let lines = csv.split(this.opts.eol);
if (!this._hasWritten) this.setColumnWidths(lines[0]);
const top = this._hasWritten ? this.middleLine : this.topLine;
this.print(top, lines, this.bottomLine);
}
printCSV(csv) {
this.end(csv);
}
setColumnWidths(line) {
this.colWidths = line
.split(this.opts.delimiter)
.map(elem => Math.max(elem.length * 2, MIN_CELL_WIDTH));
this.topLine = `${this.colWidths.map(i => '─'.repeat(i)).join('┬')}`;
this.middleLine = `${this.colWidths.map(i => '─'.repeat(i)).join('┼')}`;
this.bottomLine = `${this.colWidths.map(i => '─'.repeat(i)).join('┴')}`;
}
print(top, lines, bottom) {
const table = `${top}\n`
+ lines
.map(row => this.formatRow(row))
.join(`\n${this.middleLine}\n`)
+ (bottom ? `\n${bottom}` : '');
// eslint-disable-next-line no-console
console.log(table);
}
formatRow(row) {
const wrappedRow = row
.split(this.opts.delimiter)
.map((cell, i) => cell.match(new RegExp(`(.{1,${this.colWidths[i] - 2}})`, 'g')) || []);
const height = wrappedRow.reduce((acc, cell) => Math.max(acc, cell.length), 0);
const processedCells = wrappedRow
.map((cell, i) => this.formatCell(cell, height, this.colWidths[i]));
return Array(height).fill('')
.map((_, i) => `${processedCells.map(cell => cell[i]).join('│')}`)
.join('\n');
}
formatCell(content, heigth, width) {
const paddedContent = this.padCellHorizontally(content, width);
return this.padCellVertically(paddedContent, heigth, width);
}
padCellVertically(content, heigth, width) {
const vertPad = heigth - content.length;
const vertPadTop = Math.ceil(vertPad / 2);
const vertPadBottom = vertPad - vertPadTop;
const emptyLine = ' '.repeat(width);
return [
...Array(vertPadTop).fill(emptyLine),
...content,
...Array(vertPadBottom).fill(emptyLine)
];
}
padCellHorizontally(content, width) {
return content.map((line) => {
const horPad = width - line.length - 2;
return ` ${line}${' '.repeat(horPad)} `;
});
}
writeStream() {
let csv = '';
const table = this;
return new Writable({
write(chunk, encoding, callback) {
csv += chunk.toString();
const index = csv.lastIndexOf(table.opts.eol);
let lines = csv.substring(0, index);
csv = csv.substring(index + 1);
if (lines) table.push(lines);
callback();
},
final() {
table.end(csv);
}
});
}
}
module.exports = TablePrinter;

11
node_modules/json2csv/bin/utils/parseNdjson.js generated vendored Normal file
View File

@@ -0,0 +1,11 @@
'use strict';
function parseNdJson(input) {
return input
.split('\n')
.map(line => line.trim())
.filter(line => line !== '')
.map(line=> JSON.parse(line));
}
module.exports = parseNdJson;