Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading from gzipped files #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs"
import path from "path"
import chalk from "chalk"
import zlib from "zlib"
import {
CsvValidationOptions,
JsonValidatorOptions,
Expand All @@ -10,7 +11,7 @@ import {
import { ValidationResult } from "hpt-validator/src/types"
import { InvalidArgumentError } from "commander"

type FileFormat = "csv" | "json"
type FileFormat = "csv" | "json" | "json.gz" | "csv.gz"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove .gz extensions as acceptable format types.


export async function validate(
filepath: string,
Expand Down Expand Up @@ -68,15 +69,20 @@ async function validateFile(
validatorOptions: CsvValidationOptions | JsonValidatorOptions
): Promise<ValidationResult | null> {
const schemaVersion = version as "v1.1" | "v2.0"
if (format === "csv") {
const reader = format.endsWith(".gz")
? fs.createReadStream(filename).pipe(zlib.createGunzip()).setEncoding("utf-8")
: fs.createReadStream(filename, "utf-8")
;

if (format === "csv" || format === "csv.gz") {
return await validateCsv(
fs.createReadStream(filename, "utf-8"),
reader,
schemaVersion,
validatorOptions as CsvValidationOptions
)
} else if (format === "json") {
} else if (format === "json" || format === "json.gz") {
return await validateJson(
fs.createReadStream(filename, "utf-8"),
reader,
schemaVersion,
validatorOptions as JsonValidatorOptions
)
Expand All @@ -91,10 +97,14 @@ function getFileFormat(
): FileFormat | null {
if (fileFormat.format) return fileFormat.format as FileFormat

const fileExt = path.extname(filepath).toLowerCase().replace(".", "")
if (["csv", "json"].includes(fileExt)) {
return fileExt as FileFormat
const isGzipped = path.extname(filepath).toLowerCase() === ".gz"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially hoist this check up to determine if one of the acceptable formats is gzipped.

const fileExt = path.extname(path.basename(filepath, isGzipped ? ".gz" : ""))
switch (fileExt) {
case ".csv":
return (isGzipped ? "csv.gz" : "csv") as FileFormat
case ".json":
return (isGzipped ? "json.gz" : "json") as FileFormat
default:
return null;
}

return null
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ async function main() {
.addOption(
new Option("-f, --format <string>", "file format of file").choices([
"csv",
"csv.gz",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these new options as acceptable formats as the main branch of logic would handle this internally.

"json",
"json.gz",
])
)
.option(
Expand Down