Created
August 25, 2022 10:56
-
-
Save tristancamejo/8426db9014f6564c8045cce3551c5957 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
/** @ts-check */ | |
import { exec } from "node:child_process"; | |
import { readFile } from "node:fs/promises"; | |
import { createInterface } from "node:readline"; | |
/** | |
* @param {string} question | |
*/ | |
async function prompt(question) { | |
const rl = createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
return new Promise((resolve) => { | |
rl.question(question, (answer) => { | |
rl.close(); | |
resolve(answer); | |
}); | |
}) | |
.then((answer) => { | |
return answer; | |
}) | |
.catch((err) => { | |
console.log(err); | |
}) | |
.finally(() => { | |
rl.close(); | |
}); | |
} | |
/** | |
* @param {string} str | |
* @param {boolean} def | |
*/ | |
function parseUserBool(str, def) { | |
switch (str.toLowerCase()) { | |
case "y": | |
case "yes": | |
return true; | |
case "n": | |
case "no": | |
return false; | |
default: | |
return def; | |
} | |
} | |
/** | |
* @param {string} str | |
*/ | |
function copyToClipboard(str) { | |
exec(`echo "${str}" | pbcopy`); | |
} | |
/** | |
* @name b64 | |
* @description Encodes a file's contents to base64. This is useful for storing JSON's as environment variables. | |
* @author Tristan Camejo | |
*/ | |
(async () => { | |
const fileName = await prompt("Enter the file name: "); | |
const data = await readFile(fileName); | |
const copy = parseUserBool(await prompt("Copy to clipboard? (Y/N): "), true); | |
if (copy) { | |
copyToClipboard(data.toString("base64")); | |
} | |
return void console.log(Buffer.from(data).toString("base64")); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment