Skip to content

Commit

Permalink
Parse input arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
roim committed May 13, 2021
1 parent d00f7b4 commit 90ebf54
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

```console
$ npm install --global btoj
$ btoj location_data.bin
generated location_data.js
You can now delete location_data.bin from your project. The js file is all you need.
$ btoj location_data.bin -o location_data.js
generated 'location_data.js'
You can now delete 'location_data.bin' from your project. The js file is all you need.
```

```js
Expand All @@ -19,8 +19,14 @@ import location_data from "location_data";
// const buffer = await fs.readFile(
// path.resolve("./location_data.bin")
// );
//
// Note that the binary file is no longer needed--the js module has its data.
```

## Compatibility

**btoj** is tested in Unix and OSX environments. It likely does not work on Windows yet, but feel free to send a PR.

## Why?

It can be tricky to load binary files in some environments, either because a file system is not available, or because of some transpilation step such as webpack. **btoj** was made to _simplify_ binary file usage in these cases, by making the file be treated as any other JavaScript code.
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btoj",
"version": "1.0.0",
"version": "0.1.0",
"description": "btoj (Binary to JavaScript) is a utility that converts binary files into pure JavaScript modules exporting those files as Node.js Buffers.",
"main": "src/btoj.js",
"engines": {
Expand All @@ -25,5 +25,8 @@
"bugs": {
"url": "https://github.com/statsig-io/btoj/issues"
},
"homepage": "https://github.com/statsig-io/btoj#readme"
"homepage": "https://github.com/statsig-io/btoj#readme",
"dependencies": {
"yargs": "^17.0.1"
}
}
27 changes: 24 additions & 3 deletions src/btoj.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#!/usr/bin/env node

const yargs = require("yargs");
const fs = require("fs");
const { exit } = require("process");

fs.readFile("./binary.bin", "latin1", (re, d) => {
const argv = yargs
.usage(
`Usage:
$ btoj someInput.bin -o binary.js`
)
.option("output", {
alias: "o",
description: "Path to the output js module.",
required: true,
type: "string",
})
.help()
.alias("help", "h").argv;

const inputPath = argv._[0];
if (!inputPath) {
yargs.showHelp();
exit(1);
}

fs.readFile(inputPath, "latin1", (re, d) => {
if (re) {
console.error(re);
exit(1);
Expand All @@ -20,7 +41,7 @@ fs.readFile("./binary.bin", "latin1", (re, d) => {
.replaceAll("\n", "\\n");

fs.writeFile(
"./binary.js",
argv.output,
`const data = Buffer.from("${data}", "latin1");
module.exports = data;
`,
Expand All @@ -32,7 +53,7 @@ module.exports = data;
}

console.info(
`Generated binary.js\nYou can now delete binary.bin from your project. The js file is all you need.`
`Generated '${argv.output}'\nYou can now delete '${inputPath}' from your project. The js file is all you need.`
);
}
);
Expand Down

0 comments on commit 90ebf54

Please sign in to comment.