-
Notifications
You must be signed in to change notification settings - Fork 0
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
6e60c82
commit 7f8d8e7
Showing
13 changed files
with
334 additions
and
340 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extension": ["ts"], | ||
"node-option": ["experimental-loader=ts-node/esm"] | ||
"$schema": "https://json.schemastore.org/mocharc", | ||
"spec": ["test/**/*.test.ts"], | ||
"require": ["ts-node/register"] | ||
} |
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 |
---|---|---|
@@ -1,2 +1,74 @@ | ||
#!/usr/bin/env node | ||
require("../dist/cli")(process.argv.slice(2), "decode"); | ||
"use strict"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { program } = require("commander"); | ||
const dencodeme = require("../dist"); | ||
|
||
const encodingOption = program | ||
.createOption("-e, --encoding <format>", "The encoding to write the output data in") | ||
.default("utf8") | ||
.choices(["ascii", "binary", "latin1", "ucs2", "utf8", "utf16le"]); | ||
const fileFlag = program | ||
.createOption("-f, --file", "Interprets the input as a file path and decodes the file at the path"); | ||
const inputArgument = program | ||
.createArgument("<input>", "The input data to decode"); | ||
const outOption = program | ||
.createOption("-o, --out <path>", "The file path to write the output data to"); | ||
|
||
program.name("decodeme") | ||
.usage("[command] [options]") | ||
.description("Decode data using various encoding schemes.") | ||
.version(require("../package.json").version, "-v, --version", "Outputs the current version") | ||
.helpOption("-h, --help", "Outputs this help menu") | ||
.addHelpCommand("help [command]", "Outputs help for command"); | ||
|
||
program.command("base") | ||
.description("Decodes the specified input data from the specified base/radix") | ||
.usage("<radix> [options] <input>") | ||
.argument("<radix>", "The base/radix to decode from, clamped to range 2-36", x => { | ||
const parsed = parseInt(x, 10); | ||
return Number.isNaN(parsed) ? program.error("Base/Radix is not a valid base 10 number") : parsed; | ||
}) | ||
.addArgument(inputArgument) | ||
.addOption(encodingOption) | ||
.addOption(fileFlag) | ||
.addOption(outOption) | ||
.alias("radix") | ||
.action((radix, input, options) => { | ||
try { | ||
const output = dencodeme.base(radix).decode(options.file ? | ||
fs.readFileSync(path.resolve(process.cwd(), input), "utf8") : input); | ||
options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : | ||
process.stdout.write(output.toString(options.encoding)); | ||
} catch (err) { | ||
program.error(String(err)); | ||
} | ||
}); | ||
|
||
// Create a command for each base/radix in the dencodeme object | ||
for (const [command, base] of Object.entries(dencodeme).map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) | ||
.filter(x => !Number.isNaN(x[1]))) { | ||
program.command(command) | ||
.summary(`Decodes the specified input data from base ${base}`) | ||
.description(`Decodes the specified input data from a base/radix of ${base}`) | ||
.usage("[options] <input>") | ||
.addArgument(inputArgument) | ||
.addOption(encodingOption) | ||
.addOption(fileFlag) | ||
.addOption(outOption) | ||
.aliases(command.startsWith("base") ? [`b${command.slice(4)}`] : [command.slice(0, 3), `base${base}`, `b${base}`]) | ||
.action((input, options) => { | ||
try { | ||
const output = dencodeme[command].decode(options.file ? | ||
fs.readFileSync(path.resolve(process.cwd(), input), "utf8") : input); | ||
options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : | ||
process.stdout.write(output.toString(options.encoding)); | ||
} catch (err) { | ||
program.error(String(err)); | ||
} | ||
}); | ||
} | ||
|
||
// Parse the given arguments and run the program | ||
program.parse(process.argv.slice(2), { from: "user" }); |
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 |
---|---|---|
@@ -1,2 +1,74 @@ | ||
#!/usr/bin/env node | ||
require("../dist/cli")(process.argv.slice(2), "encode"); | ||
"use strict"; | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { program } = require("commander"); | ||
const dencodeme = require("../dist"); | ||
|
||
const encodingOption = program | ||
.createOption("-e, --encoding <format>", "The encoding to read the input data in") | ||
.default("utf8") | ||
.choices(["ascii", "binary", "latin1", "ucs2", "utf8", "utf16le"]); | ||
const fileFlag = program | ||
.createOption("-f, --file", "Interprets the input as a file path and encodes the file at the path"); | ||
const inputArgument = program | ||
.createArgument("<input>", "The input data to encode"); | ||
const outOption = program | ||
.createOption("-o, --out <path>", "The file path to write the output data to"); | ||
|
||
program.name("encodeme") | ||
.usage("[command] [options]") | ||
.description("Encode data using various encoding schemes.") | ||
.version(require("../package.json").version, "-v, --version", "Outputs the current version") | ||
.helpOption("-h, --help", "Outputs this help menu") | ||
.addHelpCommand("help [command]", "Outputs help for command"); | ||
|
||
program.command("base") | ||
.description("Encodes the specified input data with the specified base/radix") | ||
.usage("<radix> [options] <input>") | ||
.argument("<radix>", "The base/radix to encode with, clamped to range 2-36", x => { | ||
const parsed = parseInt(x, 10); | ||
return Number.isNaN(parsed) ? program.error("Base/Radix is not a valid base 10 number") : parsed; | ||
}) | ||
.addArgument(inputArgument) | ||
.addOption(encodingOption) | ||
.addOption(fileFlag) | ||
.addOption(outOption) | ||
.alias("radix") | ||
.action((radix, input, options) => { | ||
try { | ||
const output = dencodeme.base(radix).encode(options.file ? | ||
fs.readFileSync(path.resolve(process.cwd(), input), options.encoding) : input); | ||
options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : | ||
process.stdout.write(output.toString(options.encoding)); | ||
} catch (err) { | ||
program.error(String(err)); | ||
} | ||
}); | ||
|
||
// Create a command for each base/radix in the dencodeme object | ||
for (const [command, base] of Object.entries(dencodeme).map(x => [x[0], typeof x[1] == "function" ? NaN : x[1].radix]) | ||
.filter(x => !Number.isNaN(x[1]))) { | ||
program.command(command) | ||
.summary(`Encodes the specified input data with base ${base}`) | ||
.description(`Encodes the specified input data with a base/radix of ${base}`) | ||
.usage("[options] <input>") | ||
.addArgument(inputArgument) | ||
.addOption(encodingOption) | ||
.addOption(fileFlag) | ||
.addOption(outOption) | ||
.aliases(command.startsWith("base") ? [`b${command.slice(4)}`] : [command.slice(0, 3), `base${base}`, `b${base}`]) | ||
.action((input, options) => { | ||
try { | ||
const output = dencodeme[command].encode(options.file ? | ||
fs.readFileSync(path.resolve(process.cwd(), input), options.encoding) : input); | ||
options.out ? fs.writeFileSync(path.resolve(process.cwd(), options.out), output, options.encoding) : | ||
process.stdout.write(output.toString(options.encoding)); | ||
} catch (err) { | ||
program.error(String(err)); | ||
} | ||
}); | ||
} | ||
|
||
// Parse the given arguments and run the program | ||
program.parse(process.argv.slice(2), { from: "user" }); |
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
Oops, something went wrong.