mediasnacks 0.30.1 → 0.30.2
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/base64.js +35 -21
package/package.json
CHANGED
package/src/base64.js
CHANGED
|
@@ -9,45 +9,59 @@ SYNOPSIS
|
|
|
9
9
|
|
|
10
10
|
DESCRIPTION
|
|
11
11
|
Encodes image to data URI
|
|
12
|
+
|
|
13
|
+
OPTIONS
|
|
14
|
+
--css Outputs CSS background-image
|
|
15
|
+
--img Outputs HTML img tag with encoded src
|
|
12
16
|
|
|
13
17
|
EXAMPLES
|
|
14
18
|
mediasnacks base64 img
|
|
15
|
-
mediasnacks base64 --img img
|
|
16
19
|
mediasnacks base64 --css img
|
|
20
|
+
mediasnacks base64 --img img
|
|
17
21
|
`
|
|
18
22
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
const mimes = new class {
|
|
24
|
+
#MIMES = {
|
|
25
|
+
apng: 'image/apng',
|
|
26
|
+
avif: 'image/avif',
|
|
27
|
+
bmp: 'image/bmp',
|
|
28
|
+
gif: 'image/gif',
|
|
29
|
+
heic: 'image/heic',
|
|
30
|
+
heif: 'image/heif',
|
|
31
|
+
ico: 'image/vnd.microsoft.icon',
|
|
32
|
+
jpeg: 'image/jpeg',
|
|
33
|
+
jpg: 'image/jpeg',
|
|
34
|
+
jxl: 'image/jxl',
|
|
35
|
+
png: 'image/png',
|
|
36
|
+
svg: 'image/svg+xml',
|
|
37
|
+
tga: 'image/x-targa',
|
|
38
|
+
tif: 'image/tiff',
|
|
39
|
+
webp: 'image/webp',
|
|
40
|
+
}
|
|
41
|
+
mimeFor(file) {
|
|
42
|
+
return this.#MIMES[parse(file).ext.toLowerCase().replace('.', '')] || 'application/octect'
|
|
43
|
+
}
|
|
27
44
|
}
|
|
28
45
|
|
|
46
|
+
|
|
29
47
|
export default async function main() {
|
|
30
48
|
const { values, files } = await parseOptions(HELP, {
|
|
31
49
|
css: { type: 'boolean' },
|
|
32
50
|
img: { type: 'boolean' },
|
|
33
51
|
})
|
|
34
52
|
|
|
35
|
-
if (files.length
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const { data, mime } = await base64(files[0])
|
|
53
|
+
if (files.length === 0) throw 'Missing or invalid file'
|
|
54
|
+
if (files.length !== 1) throw 'Only one file is accepted'
|
|
39
55
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
else if (values.img)
|
|
43
|
-
|
|
44
|
-
else
|
|
45
|
-
console.log(data)
|
|
56
|
+
const { data, mime } = base64(files[0])
|
|
57
|
+
if (values.css) console.log(`background-image: url(data:${mime};base64,${data});`)
|
|
58
|
+
else if (values.img) console.log(`<img src="data:${mime};base64,${data}" />`)
|
|
59
|
+
else console.log(data)
|
|
46
60
|
}
|
|
47
61
|
|
|
48
|
-
export
|
|
62
|
+
export function base64(file) {
|
|
49
63
|
return {
|
|
50
64
|
data: readFileSync(file).toString('base64'),
|
|
51
|
-
mime:
|
|
65
|
+
mime: mimes.mimeFor(file)
|
|
52
66
|
}
|
|
53
67
|
}
|