-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaaa352
commit 9394a8e
Showing
6 changed files
with
282 additions
and
3 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
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,80 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require("fs"); | ||
const minimist = require("minimist-lite"); | ||
const BSON = require("bson"); | ||
const { DLF, FTS, LLF } = require("../src/index.js"); | ||
const { | ||
fileExists, | ||
getPackageVersion, | ||
streamToBuffer, | ||
outputInChunks, | ||
} = require("./helpers.js"); | ||
const { SUPPORTED_EXTENSIONS } = require("./constants.js"); | ||
|
||
const args = minimist(process.argv.slice(2), { | ||
string: ["output"], | ||
boolean: ["version"], | ||
alias: { | ||
v: "version", | ||
}, | ||
}); | ||
|
||
(async () => { | ||
if (args.version) { | ||
console.log(getPackageVersion()); | ||
process.exit(0); | ||
} | ||
|
||
let filename = args._[0]; | ||
let output = args.output; | ||
|
||
let hasErrors = false; | ||
|
||
let input; | ||
if (filename) { | ||
if (await fileExists(filename)) { | ||
input = fs.createReadStream(filename); | ||
} else { | ||
console.error("error: input file does not exist"); | ||
hasErrors = true; | ||
} | ||
} else { | ||
input = process.openStdin(); | ||
} | ||
|
||
if (output) { | ||
output = fs.createWriteStream(output); | ||
} else { | ||
output = process.stdout; | ||
} | ||
|
||
const json = BSON.deserialize(await streamToBuffer(input)); | ||
const extension = json.meta.type; | ||
|
||
if (!SUPPORTED_EXTENSIONS.includes(extension)) { | ||
console.error( | ||
'error: unsupported meta type, expected "dlf", "fts" or "llf"' | ||
); | ||
hasErrors = true; | ||
} | ||
|
||
if (hasErrors) { | ||
process.exit(1); | ||
} | ||
|
||
let binary; | ||
switch (extension) { | ||
case "fts": | ||
binary = FTS.save(json); | ||
break; | ||
case "dlf": | ||
binary = DLF.save(json); | ||
break; | ||
case "llf": | ||
binary = LLF.save(json); | ||
break; | ||
} | ||
|
||
outputInChunks(binary, output); | ||
})(); |
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,84 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require("fs"); | ||
const minimist = require("minimist-lite"); | ||
const { DLF, FTS, LLF } = require("../src/index.js"); | ||
const { | ||
fileExists, | ||
getPackageVersion, | ||
streamToBuffer, | ||
stringifyBSON, | ||
outputInChunks, | ||
} = require("./helpers.js"); | ||
const { SUPPORTED_EXTENSIONS } = require("./constants.js"); | ||
|
||
const args = minimist(process.argv.slice(2), { | ||
string: ["output", "ext"], | ||
boolean: ["version", "pretty"], | ||
alias: { | ||
v: "version", | ||
p: "pretty", | ||
}, | ||
}); | ||
|
||
(async () => { | ||
if (args.version) { | ||
console.log(getPackageVersion()); | ||
process.exit(0); | ||
} | ||
|
||
let filename = args._[0]; | ||
let extension = args.ext ? args.ext.toLowerCase() : ""; | ||
let output = args.output; | ||
|
||
let hasErrors = false; | ||
|
||
let input; | ||
if (filename) { | ||
if (await fileExists(filename)) { | ||
input = fs.createReadStream(filename); | ||
if (!extension) { | ||
extension = filename.match(/\.([a-zA-Z]+)$/)[1].toLowerCase(); | ||
} | ||
} else { | ||
console.error("error: input file does not exist"); | ||
hasErrors = true; | ||
} | ||
} else { | ||
input = process.openStdin(); | ||
} | ||
|
||
if (!SUPPORTED_EXTENSIONS.includes(extension)) { | ||
console.error( | ||
'error: unsupported extension, expected "dlf", "fts" or "llf"' | ||
); | ||
hasErrors = true; | ||
} | ||
|
||
if (output) { | ||
output = fs.createWriteStream(output); | ||
} else { | ||
output = process.stdout; | ||
} | ||
|
||
if (hasErrors) { | ||
process.exit(1); | ||
} | ||
|
||
const raw = await streamToBuffer(input); | ||
|
||
let json; | ||
switch (extension) { | ||
case "fts": | ||
json = FTS.load(raw); | ||
break; | ||
case "dlf": | ||
json = DLF.load(raw); | ||
break; | ||
case "llf": | ||
json = LLF.load(raw); | ||
break; | ||
} | ||
|
||
outputInChunks(stringifyBSON(json), output); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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