optimo 0.0.10 → 0.0.11
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 +2 -0
- package/bin/help.js +2 -0
- package/bin/index.js +2 -0
- package/package.json +1 -1
- package/src/index.js +25 -2
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
npx -y optimo public/media # for a directory
|
|
17
17
|
npx -y optimo public/media/banner.png # for a file
|
|
18
18
|
npx -y optimo public/media/banner.png -f jpeg # convert + optimize
|
|
19
|
+
npx -y optimo public/media/banner.png -r 50% # resize + optimize
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
## Optimization Strategy
|
|
@@ -34,6 +35,7 @@ const optimo = require('optimo')
|
|
|
34
35
|
await optimo.file('/absolute/path/image.jpg', {
|
|
35
36
|
dryRun: false,
|
|
36
37
|
format: 'webp',
|
|
38
|
+
resize: '50%',
|
|
37
39
|
onLogs: console.log
|
|
38
40
|
})
|
|
39
41
|
|
package/bin/help.js
CHANGED
|
@@ -10,10 +10,12 @@ Usage
|
|
|
10
10
|
Options
|
|
11
11
|
-d, --dry-run Show what would be optimized without making changes
|
|
12
12
|
-f, --format Convert output format (e.g. jpeg, webp, avif)
|
|
13
|
+
-r, --resize Resize image by percentage (e.g. 50 or 50%)
|
|
13
14
|
|
|
14
15
|
Examples
|
|
15
16
|
$ optimo image.jpg
|
|
16
17
|
$ optimo image.png --dry-run
|
|
17
18
|
$ optimo image.jpg -d
|
|
18
19
|
$ optimo image.png -f jpeg
|
|
20
|
+
$ optimo image.png -r 50%
|
|
19
21
|
`)
|
package/bin/index.js
CHANGED
|
@@ -12,6 +12,7 @@ async function main () {
|
|
|
12
12
|
alias: {
|
|
13
13
|
'dry-run': 'd',
|
|
14
14
|
format: 'f',
|
|
15
|
+
resize: 'r',
|
|
15
16
|
silent: 's'
|
|
16
17
|
}
|
|
17
18
|
})
|
|
@@ -33,6 +34,7 @@ async function main () {
|
|
|
33
34
|
await fn(input, {
|
|
34
35
|
dryRun: argv['dry-run'],
|
|
35
36
|
format: argv.format,
|
|
37
|
+
resize: argv.resize,
|
|
36
38
|
onLogs: logger
|
|
37
39
|
})
|
|
38
40
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -151,14 +151,30 @@ const getMagickFlags = filePath => {
|
|
|
151
151
|
return MAGICK_GENERIC_FLAGS
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
const parseResize = resize => {
|
|
155
|
+
if (resize === undefined || resize === null || resize === '') return null
|
|
156
|
+
|
|
157
|
+
const normalized = String(resize).trim().replace(/%$/, '')
|
|
158
|
+
const value = Number(normalized)
|
|
159
|
+
|
|
160
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
161
|
+
throw new TypeError(
|
|
162
|
+
'Resize percentage must be a number greater than 0 (e.g. 50 or 50%)'
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return `${value}%`
|
|
167
|
+
}
|
|
168
|
+
|
|
154
169
|
const file = async (
|
|
155
170
|
filePath,
|
|
156
|
-
{ onLogs = () => {}, dryRun, format: outputFormat } = {}
|
|
171
|
+
{ onLogs = () => {}, dryRun, format: outputFormat, resize } = {}
|
|
157
172
|
) => {
|
|
158
173
|
if (!magickPath) {
|
|
159
174
|
throw new Error('ImageMagick is not installed')
|
|
160
175
|
}
|
|
161
176
|
const outputPath = getOutputPath(filePath, outputFormat)
|
|
177
|
+
const resizePercentage = parseResize(resize)
|
|
162
178
|
const flags = getMagickFlags(outputPath)
|
|
163
179
|
|
|
164
180
|
const optimizedPath = `${outputPath}.optimized`
|
|
@@ -166,9 +182,16 @@ const file = async (
|
|
|
166
182
|
|
|
167
183
|
let originalSize
|
|
168
184
|
try {
|
|
185
|
+
const magickArgs = [
|
|
186
|
+
filePath,
|
|
187
|
+
...(resizePercentage ? ['-resize', resizePercentage] : []),
|
|
188
|
+
...flags,
|
|
189
|
+
optimizedPath
|
|
190
|
+
]
|
|
191
|
+
|
|
169
192
|
;[originalSize] = await Promise.all([
|
|
170
193
|
(await stat(filePath)).size,
|
|
171
|
-
await $('magick',
|
|
194
|
+
await $('magick', magickArgs)
|
|
172
195
|
])
|
|
173
196
|
} catch {
|
|
174
197
|
onLogs(formatLog('[unsupported]', yellow, filePath))
|