Last active
August 5, 2021 12:39
-
-
Save matthewp/fa948a4a025f228e7145c670d9603216 to your computer and use it in GitHub Desktop.
Convert a file to its byte source equivalent
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
const fileName = Deno.args[0]; | |
if(!fileName) { | |
console.error('No file provided.'); | |
Deno.exit(1); | |
} | |
let file = await Deno.open(fileName); | |
let encoder = new TextEncoder(); | |
await Deno.stdout.write(encoder.encode('new Uint8Array([')); | |
let first = true; | |
let buf = new Uint8Array(100); | |
let numOfByteRead = await Deno.read(file?.rid, buf); | |
while(numOfByteRead) { | |
let text = ''; | |
for(let i = 0; i < numOfByteRead; i++) { | |
if(!first) { | |
text += ', '; | |
} else { | |
first = false; | |
} | |
text += buf[i]; | |
} | |
await Deno.stdout.write(encoder.encode(text)); | |
buf = new Uint8Array(100); | |
numOfByteRead = await Deno.read(file?.rid, buf); | |
} | |
await Deno.stdout.write(encoder.encode(']);')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment