-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add qifi command and parse args
- Loading branch information
1 parent
f70f0b1
commit 6a3a8d3
Showing
11 changed files
with
269 additions
and
114 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ words: | |
- luby | ||
- Nuxt | ||
- pako | ||
- Positionals | ||
- qifi | ||
- QRCODE | ||
- Soliton | ||
|
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
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,5 @@ | ||
#!/usr/bin/env node | ||
'use strict' | ||
import { main } from '../dist/cli.mjs' | ||
|
||
main() |
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
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,85 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import process from 'node:process' | ||
import readline from 'node:readline' | ||
import { appendFileHeaderMetaToBuffer, createGeneraterANSI, createGeneraterUnicode, createGeneraterUnicodeCompact } from '@qifi/generate' | ||
import mime from 'mime' | ||
import { | ||
black, | ||
boostEcc, | ||
border, | ||
compact, | ||
contentType, | ||
ecc, | ||
fps, | ||
invert, | ||
maskPattern, | ||
maxVersion, | ||
minVersion, | ||
positional, | ||
prefix, | ||
sliceSize, | ||
unicode, | ||
white, | ||
} from './parse-args' | ||
import { handleArgsError } from './shared' | ||
|
||
function chooseGenerater() { | ||
if (compact) { | ||
return createGeneraterUnicodeCompact | ||
} | ||
if (unicode) { | ||
return createGeneraterUnicode | ||
} | ||
return createGeneraterANSI | ||
} | ||
|
||
// Function to read file and generate QR codes | ||
export async function main() { | ||
const fullPath = path.resolve(positional) | ||
|
||
console.log('fullPath:', fullPath) | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
if (!fs.existsSync(fullPath)) { | ||
handleArgsError(new TypeError(`File not found: ${fullPath}`)) | ||
} | ||
|
||
const fileBuffer = fs.readFileSync(fullPath) | ||
const data = new Uint8Array(fileBuffer) | ||
const meta = { | ||
filename: path.basename(fullPath), | ||
contentType: contentType || mime.getType(fullPath) || 'application/octet-stream', | ||
} | ||
|
||
const merged = appendFileHeaderMetaToBuffer(data, meta) | ||
|
||
const generator = chooseGenerater()(merged, { | ||
sliceSize, | ||
urlPrefix: prefix, | ||
...{ | ||
whiteChar: white, | ||
blackChar: black, | ||
} as any, | ||
invert, | ||
ecc, | ||
maskPattern, | ||
boostEcc, | ||
minVersion, | ||
maxVersion, | ||
border, | ||
}) | ||
|
||
// Clear console function | ||
const clearConsole = () => { | ||
readline.cursorTo(process.stdout, 0, 0) | ||
readline.clearScreenDown(process.stdout) | ||
} | ||
|
||
// Display QR codes | ||
for (const blockQRCode of generator.fountain()) { | ||
clearConsole() | ||
process.stdout.write(`${blockQRCode}\n`) | ||
process.stdout.write(`${meta.filename} (${meta.contentType}) | size: ${data.length} bytes`) | ||
await new Promise(resolve => setTimeout(resolve, 1000 / fps)) | ||
} | ||
} |
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,67 +1,2 @@ | ||
#!/usr/bin/env tsx | ||
|
||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import process from 'node:process' | ||
import readline from 'node:readline' | ||
import { appendFileHeaderMetaToBuffer, createGeneraterANSI } from '@qifi/generate' | ||
import mime from 'mime' | ||
|
||
// Function to read file and generate QR codes | ||
async function generateQRCodes(filePath: string, sliceSize: number = 80, fps: number = 20) { | ||
const fullPath = path.resolve(filePath) | ||
|
||
console.log('fullPath:', fullPath) | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
if (!fs.existsSync(fullPath)) { | ||
console.error(`File not found: ${fullPath}`) | ||
process.exit(1) | ||
} | ||
|
||
const fileBuffer = fs.readFileSync(fullPath) | ||
const data = new Uint8Array(fileBuffer) | ||
const meta = { | ||
filename: path.basename(fullPath), | ||
contentType: mime.getType(fullPath) || 'application/octet-stream', | ||
} | ||
|
||
const merged = appendFileHeaderMetaToBuffer(data, meta) | ||
|
||
const generator = createGeneraterANSI(merged, { | ||
urlPrefix: 'https://qrss.netlify.app/#', | ||
sliceSize, | ||
border: 2, | ||
}) | ||
|
||
// Clear console function | ||
const clearConsole = () => { | ||
readline.cursorTo(process.stdout, 0, 0) | ||
readline.clearScreenDown(process.stdout) | ||
} | ||
|
||
// Display QR codes | ||
for (const blockQRCode of generator.fountain()) { | ||
clearConsole() | ||
process.stdout.write(`${blockQRCode}\n`) | ||
process.stdout.write(`${meta.filename} (${meta.contentType}) | size: ${data.length} bytes`) | ||
await new Promise(resolve => setTimeout(resolve, 1000 / fps)) | ||
} | ||
} | ||
|
||
// Parse command line arguments | ||
const args = process.argv.slice(2) | ||
if (args.length < 1) { | ||
console.error('Usage: qr-file-transfer <file-path> [slice-size]') | ||
process.exit(1) | ||
} | ||
|
||
const [filePath, sliceSizeStr, fpsStr] = args | ||
const sliceSize = sliceSizeStr ? Number.parseInt(sliceSizeStr, 10) : undefined | ||
const fps = fpsStr ? Number.parseInt(fpsStr, 10) : undefined | ||
|
||
if (!filePath) { | ||
console.error('File path is required') | ||
process.exit(1) | ||
} | ||
|
||
generateQRCodes(filePath, sliceSize, fps) | ||
console.warn('qifi is a cli package, it should not be imported in other packages') | ||
export default function () {} |
Oops, something went wrong.