-
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.
feat: convert to typescript & export a binary
- Loading branch information
1 parent
d7a5477
commit c808c0b
Showing
10 changed files
with
389 additions
and
42 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,2 +1,3 @@ | ||
node_modules | ||
lib | ||
*.log |
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,11 @@ | ||
import * as QRCode from 'qrcode'; | ||
|
||
/** | ||
* Encodes an ascii string into a data URL-embedded QR code. | ||
*/ | ||
export const atoqr = async (ascii: string, output: 'dataUrl' | string = 'dataUrl'): Promise<string> => { | ||
if (output === 'dataUrl') { | ||
return QRCode.toDataURL(ascii); | ||
} | ||
return QRCode.toFile(output, ascii); | ||
}; |
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,69 @@ | ||
#!/usr/bin/env node | ||
import yargs from 'yargs'; | ||
import { readFileSync } from 'fs'; | ||
import { normalize } from 'path'; | ||
import { atoqr, qrtoa } from './'; | ||
|
||
const argv = yargs(process.argv.slice(2)) | ||
.scriptName(require('../package.json').name) | ||
.usage('Usage: $0 <command> [options]') | ||
.command('decode <qr_code>', 'Decode qr_code back into an ascii string', _yargs => { | ||
_yargs.positional('qr_code', { | ||
describe: 'A data url or a path to an image file', | ||
type: 'string', | ||
}); | ||
}) | ||
.alias('qrtoa', 'decode') | ||
.command('encode <ascii> [options]', 'Encode an ascii string into a qr code', _yargs => { | ||
_yargs.positional('ascii', { | ||
describe: 'An ascii string', | ||
}).option('file', { | ||
alias: 'f', | ||
describe: 'Path to save output to (as *.{png,svg,txt})', | ||
normalize: true, | ||
type: 'string', | ||
}); | ||
}) | ||
.alias('atoqr', 'encode') | ||
.demandCommand(1, 'You must specify a command') | ||
.option('clean', { | ||
alias: 'c', | ||
describe: 'Omit the trailing newline', | ||
type: 'boolean', | ||
}) | ||
.help('h') | ||
.alias('h', 'help') | ||
.argv; | ||
|
||
const [command] = argv._; | ||
|
||
const main = async () => { | ||
switch (command) { | ||
case 'decode': { | ||
const dataUrlOrFile = argv.qr_code as string; | ||
const [, content] = dataUrlOrFile.match(/^data:image\/(?:jpeg|png|bmp|tiff|gif);base64,(.+)$/) || []; | ||
const isDataUrl = !!content; | ||
const buffer = isDataUrl | ||
? Buffer.from(content, 'base64') | ||
: readFileSync(normalize(dataUrlOrFile)); | ||
const ascii = await qrtoa(buffer); | ||
process.stdout.write(ascii); | ||
break; | ||
} | ||
case 'encode': { | ||
const ascii = argv.ascii as string; | ||
const file = argv.file as string | undefined; | ||
const qrCode = await atoqr(ascii, file); | ||
process.stdout.write(qrCode); | ||
break; | ||
} | ||
} | ||
if (!argv.clean) { | ||
process.stdout.write('\n'); | ||
} | ||
}; | ||
|
||
main().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
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,2 @@ | ||
export * from './atoqr'; | ||
export * from './qrtoa'; |
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,14 @@ | ||
import Jimp from 'jimp'; | ||
import jsQR from 'jsqr'; | ||
|
||
/** | ||
* Decodes a buffer containing a QR code into an ascii string. | ||
*/ | ||
export const qrtoa = async (buffer: Buffer): Promise<string> => { | ||
const image = await Jimp.read(buffer); | ||
const result = jsQR(new Uint8ClampedArray(image.bitmap.data), image.bitmap.width, image.bitmap.height); | ||
if (!result) { | ||
return Promise.reject(new Error('Failed to parse qr_code')); | ||
} | ||
return result.data; | ||
}; |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"lib": [ | ||
"es6" | ||
], | ||
"moduleResolution": "node", | ||
"outDir": "lib", | ||
"resolveJsonModule": true, | ||
"rootDir": "src", | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"target": "es5" | ||
}, | ||
"include": [ | ||
"src/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"package.json" | ||
] | ||
} |
Oops, something went wrong.