Skip to content

Commit

Permalink
feat: init cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSound committed Oct 11, 2024
1 parent bf723ab commit 87f2873
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# @qifi/generate

<!-- Some beautiful tags -->
<p align="left">
<a href="https://www.npmjs.com/package/@qifi/generate">
<img alt="npm" src="https://badgen.net/npm/v/@qifi/generate">
</a>
<a href="#usage">
<img alt="docs" src="https://img.shields.io/badge/-docs%20%26%20demos-1e8a7a">
</a>
<a href="https://github.com/sponsors/LittleSound">
<img alt="docs" src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86">
</a>
</p>

Stream Generated QR Codes for data transmission

## Sponsors

<p align="center">
<a href="https://github.com/sponsors/LittleSound">
<img src="https://cdn.jsdelivr.net/gh/littlesound/sponsors/sponsors.svg"/>
</a>
</p>

<p align="center">
This project is made possible by all the sponsors supporting my work <br>
You can join them at my sponsors profile:
</p>
<p align="center"><a href="https://github.com/sponsors/LittleSound"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86&style=for-the-badge" /></a></p>

## Usage

```javascript
import {
createGeneraterANSI,
createGeneraterUnicode,
createGeneraterUnicodeCompact,
createGeneraterSVG,
createGeneraterQRCodeArray,
} from '@qifi/generate'

const generaterSvg = createGeneraterSVG(new Uint8Array(file.buffer))

const generaterANSI = createGeneraterANSI(new Uint8Array(file.buffer), {
// Size of each data slice
sliceSize: 250,
// Error correction level
ecc: 'L',
// Border width
border: 2,
})

// display QR Code in terminal
for (const blockQRCode of generaterANSI()) {
console.log(blockQRCode)
}

```
19 changes: 19 additions & 0 deletions packages/cli/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: [
'src/index.ts',
],
declaration: true,
rollup: {
emitCJS: false,
dts: {
compilerOptions: {
paths: {},
},
},
},
externals: [
'pako',
],
})
38 changes: 38 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@qifi/cli",
"type": "module",
"version": "0.0.3",
"description": "Stream Generated QR Codes for file transmission in your terminal",
"author": "Rizumu Ayaka <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/qifi-dev/qrs#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/qifi-dev/qrs.git",
"directory": "packages/cli"
},
"bug": "https://github.com/qifi-dev/qrs/issues",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.mts",
"files": [
"dist"
],
"scripts": {
"dev": "tsx ./src",
"build": "unbuild",
"stub": "unbuild --stub"
},
"dependencies": {
"@qifi/generate": "workspace:*",
"mime": "^4.0.4",
"tsx": "^4.19.1"
}
}
63 changes: 63 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env tsx

import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { appendFileHeaderMetaToBuffer, createGeneraterANSI } from '@qifi/generate'

Check failure on line 6 in packages/cli/src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@qifi/generate' or its corresponding type declarations.
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 = () => process.stdout.write('\x1Bc')

// Display QR codes
for (const blockQRCode of generator.fountain()) {
clearConsole()
console.log(blockQRCode)
console.log(`${meta.filename} (${meta.contentType})`, '|', 'size:', data.length, 'bytes')
await new Promise(resolve => setTimeout(resolve, 1000 / fps)) // Display each QR code for 1 second
}
}

// 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)
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87f2873

Please sign in to comment.