mediasnacks 0.30.0 → 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/README.md CHANGED
@@ -22,7 +22,7 @@ mediasnacks <command> <args>
22
22
  - `avif` Converts images to AVIF
23
23
  - `png` Optimizes PNG images with oxipng
24
24
  - `sqcrop` Square crops images
25
- - `base64` Encodes a file to data URI
25
+ - `base64` Encodes image to data URI
26
26
 
27
27
 
28
28
  - `resize` Resizes videos or images
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediasnacks",
3
- "version": "0.30.0",
3
+ "version": "0.30.2",
4
4
  "description": "Utilities for optimizing and preparing videos and images",
5
5
  "license": "MIT",
6
6
  "author": "Eric Fortis",
package/src/base64.js CHANGED
@@ -1,42 +1,67 @@
1
+ import { parse } from 'node:path'
1
2
  import { readFileSync } from 'node:fs'
2
3
  import { parseOptions } from './utils/parseOptions.js'
3
- import { mimeFor } from './utils/mimeFor.js'
4
+
4
5
 
5
6
  const HELP = `
6
7
  SYNOPSIS
7
- mediasnacks base64 [--css | --img] file
8
+ mediasnacks base64 [--css | --img] img
8
9
 
9
10
  DESCRIPTION
10
- Encodes a file to data URI.
11
+ Encodes image to data URI
12
+
13
+ OPTIONS
14
+ --css Outputs CSS background-image
15
+ --img Outputs HTML img tag with encoded src
11
16
 
12
17
  EXAMPLES
13
- mediasnacks base64 file
14
- mediasnacks base64 --img file
15
- mediasnacks base64 --css file
18
+ mediasnacks base64 img
19
+ mediasnacks base64 --css img
20
+ mediasnacks base64 --img img
16
21
  `
17
22
 
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
+ }
44
+ }
45
+
46
+
18
47
  export default async function main() {
19
48
  const { values, files } = await parseOptions(HELP, {
20
49
  css: { type: 'boolean' },
21
50
  img: { type: 'boolean' },
22
51
  })
23
52
 
24
- if (files.length !== 1)
25
- throw 'Only one file is accepted'
26
-
27
- 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'
28
55
 
29
- if (values.css)
30
- console.log(`background-image: url(data:${mime};base64,${data})`)
31
- else if (values.img)
32
- console.log(`<img src="data:${mime};base64,${data}" />`)
33
- else
34
- 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)
35
60
  }
36
61
 
37
- export async function base64(filePath) {
62
+ export function base64(file) {
38
63
  return {
39
- data: readFileSync(filePath).toString('base64'),
40
- mime: await mimeFor(filePath)
64
+ data: readFileSync(file).toString('base64'),
65
+ mime: mimes.mimeFor(file)
41
66
  }
42
67
  }
@@ -1,21 +0,0 @@
1
- import { parse } from 'node:path'
2
- import { runSilently } from './subprocess.js'
3
-
4
-
5
- export async function mimeFor(filePath) {
6
- return process.platform === 'win32'
7
- ? await windowsMime(filePath)
8
- : (await runSilently('file', ['--mime-type', '--brief', filePath])).stdout.trim()
9
- }
10
-
11
- async function windowsMime(filePath) { // TODO test on windows
12
- const ext = parse(filePath).ext.toLowerCase()
13
- try {
14
- const { stdout } = await runSilently('reg', ['query', `HKEY_CLASSES_ROOT\\${ext}`, '/v', 'Content Type'])
15
- const match = stdout.match(/Content Type\s+REG_SZ\s+(\S+)/)
16
- if (match)
17
- return match[1]
18
- }
19
- catch {}
20
- return 'application/octet-stream'
21
- }