-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add batch and split-to-files commands.
- Loading branch information
1 parent
a74e719
commit 9aa10b3
Showing
8 changed files
with
238 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
const { run } = require("../build/lib/command"); | ||
const { main, documentation } = require('../build/cli/batch.js'); | ||
|
||
run(main, documentation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
const { run } = require("../build/lib/command"); | ||
const { main, documentation } = require('../build/cli/split-to-files.js'); | ||
|
||
run(main, documentation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { inputData, outputData } from "../lib/io"; | ||
import { invokeUserFn, loadUserFn } from "./lib/user-fn"; | ||
import { verifyInputArray } from "../lib/verify"; | ||
import { standardCmdInputs, standardCmdOutputs, standardInputFileHelp, standardOutputFileHelp } from "./lib/help"; | ||
|
||
// | ||
// Create batches from an array. | ||
// | ||
function createBatches(array: any[], batchSize: number): any[] { | ||
|
||
const batches = []; | ||
|
||
for (let i = 0; i < array.length; i += batchSize) { | ||
batches.push(array.slice(i, i + batchSize)); | ||
} | ||
|
||
return batches; | ||
} | ||
|
||
export async function main(argv: string[]): Promise<void> { | ||
|
||
if (argv.length < 1) { | ||
throw new Error(`Expected <number> argument for for batch size.`); | ||
} | ||
|
||
const batchSize = parseInt(argv.shift()!); | ||
|
||
const data = await inputData(argv); | ||
verifyInputArray(data, "batch"); | ||
|
||
const output = createBatches(data, batchSize); | ||
await outputData(argv, output); | ||
} | ||
|
||
export const documentation = { | ||
name: "batch", | ||
desc: "Outputs the input dataset as an array of arrays, each sub-array containing the specified number of records.", | ||
syntax: "batch <batch-size> [<input-file>] [<output-file>]", | ||
inputs: standardCmdInputs, | ||
outputs: [ | ||
"JSON file", | ||
"YAML file", | ||
"JSON formatted data on standard output.", | ||
], | ||
args: [ | ||
{ | ||
name: "batch-size", | ||
desc: "Specifies the size for each batch.", | ||
}, | ||
standardInputFileHelp, | ||
standardOutputFileHelp, | ||
], | ||
examples: [ | ||
{ | ||
name: "Reads JSON data from standard input, batches the records into groups of 5 and writes to standard output", | ||
cmd: 'command-that-produces-json | batch 5', | ||
}, | ||
{ | ||
name: "Reads data from a file, batches the records into groups of 5 and writes to standard output", | ||
cmd: 'batch 5 input-file.csv', | ||
}, | ||
{ | ||
name: "Reads data from a file, batches the records into groups of 5 and writes output to another file", | ||
cmd: 'batch 5 input-file.csv output-file.csv' | ||
}, | ||
{ | ||
name: "Reads JSON data from standard input, applies the transformation and writes output to another file", | ||
cmd: 'command-that-produces-json | batch 5 - output-file.csv' | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { inputData, outputData, outputDataToFile } from "../lib/io"; | ||
import { verifyInputArray } from "../lib/verify"; | ||
import { Flags, pullOptions } from "./lib/args"; | ||
import { standardCmdInputs, standardInputFileHelp } from "./lib/help"; | ||
|
||
export async function main(argv: string[], flags: Flags): Promise<void> { | ||
|
||
if (argv.length < 1) { | ||
throw new Error(`Expected <number> argument for output file name prefix.`); | ||
} | ||
|
||
const outputFileNamePrefix = argv.shift()!; | ||
let outputFileNameSuffix = "json"; | ||
|
||
if (argv.length > 0) { | ||
outputFileNameSuffix = argv.shift()!; | ||
} | ||
|
||
const data = await inputData(argv); | ||
verifyInputArray(data, "split-to-files"); | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
const record = data[i]; | ||
await outputDataToFile(record, `${outputFileNamePrefix}-${i+1}.${outputFileNameSuffix}`); | ||
} | ||
} | ||
|
||
export const documentation = { | ||
name: "split-to-files", | ||
desc: "Writes a separate file for each record in the dataset. This command does not write to standard output.", | ||
syntax: "split-to-files [<output-file-prefix>] [<output-file-suffix>] [<input-file>]", | ||
inputs: standardCmdInputs, | ||
outputs: [ | ||
"JSON files", | ||
"CSV files", | ||
"YAML files", | ||
], | ||
args: [ | ||
{ | ||
name: "output-file-prefix", | ||
desc: "Specifies the prefix for output files. Each file created is numbered sequentially, starting with 1.", | ||
}, | ||
{ | ||
name: "output-file-suffix", | ||
desc: "Specifies the suffix for output files. Either 'csv', 'json', 'yaml' or 'yml'. Defaults to 'json'.", | ||
}, | ||
standardInputFileHelp, | ||
], | ||
examples: [ | ||
{ | ||
name: "Reads JSON data from standard input and writes each records to a set of JSON files my-output-1.json, my-output-2.json and so on. Replace 'json' with 'csv' or 'yaml' to write CSV or YAML files instead.", | ||
cmd: 'command-that-produces-json | split-to-files my-output- json', | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters