@verbatims/cli 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verbatims/cli",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "CLI for the Verbatims quotes API",
5
5
  "type": "module",
6
6
  "bin": {
@@ -5,6 +5,7 @@ import { getClient } from '../utils/client.js'
5
5
  import { output, type Format } from '../utils/format.js'
6
6
  import { withSpinner } from '../utils/spinner.js'
7
7
  import { browse } from '../utils/browse.js'
8
+ import { uploadImageFromPath } from '../utils/image.js'
8
9
 
9
10
  function getFormat(program: Command): Format {
10
11
  return (program.opts().format ?? 'table') as Format
@@ -47,6 +48,9 @@ export function registerAuthorsCommand(program: Command) {
47
48
  .option('--name <name>', 'Author name')
48
49
  .option('--job <job>', 'Job title')
49
50
  .option('--description <text>', 'Description')
51
+ .option('--image-url <url>', 'Image URL')
52
+ .option('--image-path <path>', 'Local image file path')
53
+ .option('--fictional', 'Mark as fictional')
50
54
  .action(async (options: Record<string, string>) => {
51
55
  const client = await getClient()
52
56
  const format = getFormat(program)
@@ -54,8 +58,20 @@ export function registerAuthorsCommand(program: Command) {
54
58
  const name = options.name ?? await text({ message: 'Author name', validate: (v) => !v ? 'Required' : undefined })
55
59
  if (isCancel(name)) cancel('Cancelled')
56
60
 
61
+ let image_url: string | null = options.imageUrl || null
62
+ const imagePath = options.imagePath
63
+ if (imagePath) {
64
+ image_url = await withSpinner('Uploading image', () => uploadImageFromPath(client, imagePath), format)
65
+ }
66
+
57
67
  const { data } = await withSpinner('Creating author', () =>
58
- client.authors.create({ name: name as string, job: options.job ?? null, description: options.description ?? null }),
68
+ client.authors.create({
69
+ name: name as string,
70
+ job: options.job ?? null,
71
+ description: options.description ?? null,
72
+ image_url,
73
+ fictional: options.fictional !== undefined ? true : undefined,
74
+ }),
59
75
  format,
60
76
  )
61
77
  console.log(chalk.green(' Author created'))
@@ -66,14 +82,27 @@ export function registerAuthorsCommand(program: Command) {
66
82
  .command('update <id>')
67
83
  .description('Update an author')
68
84
  .option('--name <name>', 'Author name')
85
+ .option('--job <job>', 'Job title')
69
86
  .option('--description <text>', 'Description')
87
+ .option('--image-url <url>', 'Image URL')
88
+ .option('--image-path <path>', 'Local image file path')
89
+ .option('--fictional', 'Mark as fictional')
90
+ .option('--no-fictional', 'Mark as not fictional')
70
91
  .action(async (id: string, options: Record<string, string>) => {
71
92
  const client = await getClient()
72
93
  const format = getFormat(program)
73
94
 
74
95
  const data_: Record<string, unknown> = {}
75
96
  if (options.name) data_.name = options.name
76
- if (options.description !== undefined) data_.description = options.description
97
+ if (options.job !== undefined) data_.job = options.job || null
98
+ if (options.description !== undefined) data_.description = options.description || null
99
+ if (options.imageUrl !== undefined) data_.image_url = options.imageUrl || null
100
+ const updateImagePath = options.imagePath
101
+ if (updateImagePath !== undefined) {
102
+ data_.image_url = await withSpinner('Uploading image', () => uploadImageFromPath(client, updateImagePath), format)
103
+ }
104
+ if (options.fictional !== undefined) data_.is_fictional = true
105
+ if (options.noFictional !== undefined) data_.is_fictional = false
77
106
 
78
107
  const { data } = await withSpinner('Updating author', () => client.authors.update(Number(id), data_ as any), format)
79
108
  console.log(chalk.green(' Author updated'))
@@ -5,6 +5,7 @@ import { getClient } from '../utils/client.js'
5
5
  import { output, type Format } from '../utils/format.js'
6
6
  import { withSpinner } from '../utils/spinner.js'
7
7
  import { browse } from '../utils/browse.js'
8
+ import { uploadImageFromPath } from '../utils/image.js'
8
9
 
9
10
  function getFormat(program: Command): Format {
10
11
  return (program.opts().format ?? 'table') as Format
@@ -49,6 +50,9 @@ export function registerReferencesCommand(program: Command) {
49
50
  .option('--type <type>', 'Primary type')
50
51
  .option('--description <text>', 'Description')
51
52
  .option('--language <lang>', 'Original language')
53
+ .option('--release-date <date>', 'Release date')
54
+ .option('--image-url <url>', 'Image URL')
55
+ .option('--image-path <path>', 'Local image file path')
52
56
  .action(async (options: Record<string, string>) => {
53
57
  const client = await getClient()
54
58
  const format = getFormat(program)
@@ -59,8 +63,23 @@ export function registerReferencesCommand(program: Command) {
59
63
  const primaryType = options.type ?? await text({ message: 'Primary type' })
60
64
  if (isCancel(primaryType)) cancel('Cancelled')
61
65
 
66
+ let image_url: string | null = options.imageUrl || null
67
+ const createImagePath = options.imagePath
68
+ if (createImagePath) {
69
+ image_url = await withSpinner('Uploading image', () => uploadImageFromPath(client, createImagePath), format)
70
+ }
71
+
62
72
  const { data } = await withSpinner('Creating reference', () =>
63
- client.references.create({ name: name as string, type: primaryType as string, primary_type: primaryType as string, description: options.description ?? null, language: options.language, original_language: options.language }),
73
+ client.references.create({
74
+ name: name as string,
75
+ type: primaryType as string,
76
+ primary_type: primaryType as string,
77
+ description: options.description ?? null,
78
+ language: options.language,
79
+ original_language: options.language,
80
+ release_date: options.releaseDate ?? null,
81
+ image_url,
82
+ }),
64
83
  format,
65
84
  )
66
85
  console.log(chalk.green(' Reference created'))
@@ -71,14 +90,27 @@ export function registerReferencesCommand(program: Command) {
71
90
  .command('update <id>')
72
91
  .description('Update a reference')
73
92
  .option('--name <name>', 'Reference name')
93
+ .option('--type <type>', 'Primary type')
74
94
  .option('--description <text>', 'Description')
95
+ .option('--language <lang>', 'Language')
96
+ .option('--release-date <date>', 'Release date')
97
+ .option('--image-url <url>', 'Image URL')
98
+ .option('--image-path <path>', 'Local image file path')
75
99
  .action(async (id: string, options: Record<string, string>) => {
76
100
  const client = await getClient()
77
101
  const format = getFormat(program)
78
102
 
79
103
  const data_: Record<string, unknown> = {}
80
- if (options.name) { data_.name = options.name }
81
- if (options.description !== undefined) data_.description = options.description
104
+ if (options.name) data_.name = options.name
105
+ if (options.type !== undefined) { data_.type = options.type; data_.primary_type = options.type }
106
+ if (options.description !== undefined) data_.description = options.description || null
107
+ if (options.language !== undefined) data_.language = options.language || null
108
+ if (options.releaseDate !== undefined) data_.release_date = options.releaseDate || null
109
+ if (options.imageUrl !== undefined) data_.image_url = options.imageUrl || null
110
+ const updateImagePath = options.imagePath
111
+ if (updateImagePath !== undefined) {
112
+ data_.image_url = await withSpinner('Uploading image', () => uploadImageFromPath(client, updateImagePath), format)
113
+ }
82
114
 
83
115
  const { data } = await withSpinner('Updating reference', () => client.references.update(Number(id), data_ as any), format)
84
116
  console.log(chalk.green(' Reference updated'))
@@ -0,0 +1,37 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import type { VerbatimsClient } from '@verbatims/sdk'
4
+
5
+ const MIME_TYPES: Record<string, string> = {
6
+ '.jpg': 'image/jpeg',
7
+ '.jpeg': 'image/jpeg',
8
+ '.png': 'image/png',
9
+ '.gif': 'image/gif',
10
+ '.webp': 'image/webp',
11
+ '.avif': 'image/avif',
12
+ '.svg': 'image/svg+xml',
13
+ }
14
+
15
+ function detectMimeType(filePath: string): string {
16
+ const ext = path.extname(filePath).toLowerCase()
17
+ return MIME_TYPES[ext] || 'image/jpeg'
18
+ }
19
+
20
+ export async function uploadImageFromPath(client: VerbatimsClient, filePath: string): Promise<string> {
21
+ const resolvedPath = path.resolve(filePath)
22
+ if (!fs.existsSync(resolvedPath)) {
23
+ throw new Error(`File not found: ${resolvedPath}`)
24
+ }
25
+
26
+ const stat = fs.statSync(resolvedPath)
27
+ if (!stat.isFile()) {
28
+ throw new Error(`Not a file: ${resolvedPath}`)
29
+ }
30
+
31
+ const buffer = fs.readFileSync(resolvedPath)
32
+ const mimeType = detectMimeType(resolvedPath)
33
+ const blob = new Blob([buffer], { type: mimeType })
34
+
35
+ const { data } = await client.uploadImage(blob)
36
+ return data.url
37
+ }