@verbatims/cli 0.1.12 → 0.1.14
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 +1 -1
- package/src/commands/authors.ts +14 -1
- package/src/commands/references.ts +14 -1
- package/src/utils/image.ts +37 -0
package/package.json
CHANGED
package/src/commands/authors.ts
CHANGED
|
@@ -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
|
|
@@ -48,6 +49,7 @@ export function registerAuthorsCommand(program: Command) {
|
|
|
48
49
|
.option('--job <job>', 'Job title')
|
|
49
50
|
.option('--description <text>', 'Description')
|
|
50
51
|
.option('--image-url <url>', 'Image URL')
|
|
52
|
+
.option('--image-path <path>', 'Local image file path')
|
|
51
53
|
.option('--fictional', 'Mark as fictional')
|
|
52
54
|
.action(async (options: Record<string, string>) => {
|
|
53
55
|
const client = await getClient()
|
|
@@ -56,12 +58,18 @@ export function registerAuthorsCommand(program: Command) {
|
|
|
56
58
|
const name = options.name ?? await text({ message: 'Author name', validate: (v) => !v ? 'Required' : undefined })
|
|
57
59
|
if (isCancel(name)) cancel('Cancelled')
|
|
58
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
|
+
|
|
59
67
|
const { data } = await withSpinner('Creating author', () =>
|
|
60
68
|
client.authors.create({
|
|
61
69
|
name: name as string,
|
|
62
70
|
job: options.job ?? null,
|
|
63
71
|
description: options.description ?? null,
|
|
64
|
-
image_url
|
|
72
|
+
image_url,
|
|
65
73
|
fictional: options.fictional !== undefined ? true : undefined,
|
|
66
74
|
}),
|
|
67
75
|
format,
|
|
@@ -77,6 +85,7 @@ export function registerAuthorsCommand(program: Command) {
|
|
|
77
85
|
.option('--job <job>', 'Job title')
|
|
78
86
|
.option('--description <text>', 'Description')
|
|
79
87
|
.option('--image-url <url>', 'Image URL')
|
|
88
|
+
.option('--image-path <path>', 'Local image file path')
|
|
80
89
|
.option('--fictional', 'Mark as fictional')
|
|
81
90
|
.option('--no-fictional', 'Mark as not fictional')
|
|
82
91
|
.action(async (id: string, options: Record<string, string>) => {
|
|
@@ -88,6 +97,10 @@ export function registerAuthorsCommand(program: Command) {
|
|
|
88
97
|
if (options.job !== undefined) data_.job = options.job || null
|
|
89
98
|
if (options.description !== undefined) data_.description = options.description || null
|
|
90
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
|
+
}
|
|
91
104
|
if (options.fictional !== undefined) data_.is_fictional = true
|
|
92
105
|
if (options.noFictional !== undefined) data_.is_fictional = false
|
|
93
106
|
|
|
@@ -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
|
|
@@ -51,6 +52,7 @@ export function registerReferencesCommand(program: Command) {
|
|
|
51
52
|
.option('--language <lang>', 'Original language')
|
|
52
53
|
.option('--release-date <date>', 'Release date')
|
|
53
54
|
.option('--image-url <url>', 'Image URL')
|
|
55
|
+
.option('--image-path <path>', 'Local image file path')
|
|
54
56
|
.action(async (options: Record<string, string>) => {
|
|
55
57
|
const client = await getClient()
|
|
56
58
|
const format = getFormat(program)
|
|
@@ -61,6 +63,12 @@ export function registerReferencesCommand(program: Command) {
|
|
|
61
63
|
const primaryType = options.type ?? await text({ message: 'Primary type' })
|
|
62
64
|
if (isCancel(primaryType)) cancel('Cancelled')
|
|
63
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
|
+
|
|
64
72
|
const { data } = await withSpinner('Creating reference', () =>
|
|
65
73
|
client.references.create({
|
|
66
74
|
name: name as string,
|
|
@@ -70,7 +78,7 @@ export function registerReferencesCommand(program: Command) {
|
|
|
70
78
|
language: options.language,
|
|
71
79
|
original_language: options.language,
|
|
72
80
|
release_date: options.releaseDate ?? null,
|
|
73
|
-
image_url
|
|
81
|
+
image_url,
|
|
74
82
|
}),
|
|
75
83
|
format,
|
|
76
84
|
)
|
|
@@ -87,6 +95,7 @@ export function registerReferencesCommand(program: Command) {
|
|
|
87
95
|
.option('--language <lang>', 'Language')
|
|
88
96
|
.option('--release-date <date>', 'Release date')
|
|
89
97
|
.option('--image-url <url>', 'Image URL')
|
|
98
|
+
.option('--image-path <path>', 'Local image file path')
|
|
90
99
|
.action(async (id: string, options: Record<string, string>) => {
|
|
91
100
|
const client = await getClient()
|
|
92
101
|
const format = getFormat(program)
|
|
@@ -98,6 +107,10 @@ export function registerReferencesCommand(program: Command) {
|
|
|
98
107
|
if (options.language !== undefined) data_.language = options.language || null
|
|
99
108
|
if (options.releaseDate !== undefined) data_.release_date = options.releaseDate || null
|
|
100
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
|
+
}
|
|
101
114
|
|
|
102
115
|
const { data } = await withSpinner('Updating reference', () => client.references.update(Number(id), data_ as any), format)
|
|
103
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
|
+
}
|