picslim 0.0.8 → 1.0.0
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 +44 -8
- package/package.json +1 -1
- package/picslim.js +87 -56
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# PicSlim
|
|
2
2
|
|
|
3
|
-
**Picslim** is a Node.js package that allows you to optimize images
|
|
3
|
+
**Picslim** is a Node.js package that allows you to efficiently optimize images within a specified directory. It supports **JPEG** and **PNG** images, image formats, offering fine-grained control over image quality and resizing options during the optimization process. With **Picslim**, you can effortlessly reduce file sizes and enhance the loading performance of your images.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,6 +10,12 @@ You can install picslim globally using npm:
|
|
|
10
10
|
npm install -g picslim
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
or
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx picslim
|
|
17
|
+
```
|
|
18
|
+
|
|
13
19
|
# Usage
|
|
14
20
|
|
|
15
21
|
Once installed, you can use the optimizimage command in your terminal. Here's how you can use it:
|
|
@@ -18,17 +24,43 @@ Once installed, you can use the optimizimage command in your terminal. Here's ho
|
|
|
18
24
|
picslim [options]
|
|
19
25
|
```
|
|
20
26
|
|
|
21
|
-
Options
|
|
27
|
+
### Options
|
|
28
|
+
|
|
29
|
+
- `-c, --config <path>`: Path to the configuration file. (default: 'config.json')
|
|
30
|
+
- `-q, --quality <number>`: Image quality (0-100).
|
|
31
|
+
- `-l, --compressionLevel <number>`: PNG compression level (0-9).
|
|
32
|
+
- `-w, --maxWidth <number>`: Maximum width allowed for images.
|
|
33
|
+
- `-h, --maxHeight <number>`: Maximum height allowed for images.
|
|
34
|
+
- `-i, --input <path>`: Path to the input directory.
|
|
35
|
+
- `-o, --output <path>`: Path to the output directory.
|
|
36
|
+
|
|
37
|
+
### Configuration File
|
|
38
|
+
|
|
39
|
+
You can create a `config.json` file in your project directory to specify default settings. Here's an example configuration:
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"inputDir": "./in",
|
|
44
|
+
"outputDir": "./min",
|
|
45
|
+
"quality": 80,
|
|
46
|
+
"maxWidth": null,
|
|
47
|
+
"maxHeight": null,
|
|
48
|
+
"compressionLevel": 9
|
|
49
|
+
}
|
|
50
|
+
```
|
|
22
51
|
|
|
23
|
-
|
|
24
|
-
**-mw, --maxWidth [value]:** Set the maximum width allowed (default: null).<br>
|
|
25
|
-
**-mh, --maxHeight [value]:** Set the maximum height allowed (default: null).<br>
|
|
26
|
-
**-c, --compressionLevel [value]:** Set the compression level (0 to 9, default: 9).
|
|
52
|
+
### Example:
|
|
27
53
|
|
|
28
|
-
|
|
54
|
+
Optimize images using default settings from the configuration file:
|
|
29
55
|
|
|
30
56
|
```bash
|
|
31
|
-
picslim
|
|
57
|
+
picslim
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Optimize images with custom settings:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
picslim -q 90 -w 800 -h 600 -l 4 -i input_images -o output_images
|
|
32
64
|
```
|
|
33
65
|
|
|
34
66
|
This will optimize all JPEG and PNG images in the current directory, and the optimized images will be saved in a 'min' directory.
|
|
@@ -65,3 +97,7 @@ This project is licensed under the MIT License. See the LICENSE file for details
|
|
|
65
97
|
### Author
|
|
66
98
|
|
|
67
99
|
Ivan Mercedes
|
|
100
|
+
|
|
101
|
+
### Contributors
|
|
102
|
+
|
|
103
|
+
- [Elminson De Oleo Baez](https://github.com/elminson)
|
package/package.json
CHANGED
package/picslim.js
CHANGED
|
@@ -14,14 +14,56 @@ function parseArguments () {
|
|
|
14
14
|
demandOption: false,
|
|
15
15
|
type: 'string',
|
|
16
16
|
default: 'config.json'
|
|
17
|
+
},
|
|
18
|
+
q: {
|
|
19
|
+
alias: 'quality',
|
|
20
|
+
describe: 'Image quality',
|
|
21
|
+
demandOption: false,
|
|
22
|
+
type: 'number',
|
|
23
|
+
default: null
|
|
24
|
+
},
|
|
25
|
+
l: {
|
|
26
|
+
alias: 'compressionLevel',
|
|
27
|
+
describe: 'PNG compression level',
|
|
28
|
+
demandOption: false,
|
|
29
|
+
type: 'number',
|
|
30
|
+
default: null
|
|
31
|
+
},
|
|
32
|
+
w: {
|
|
33
|
+
alias: 'maxWidth',
|
|
34
|
+
describe: 'Maximum width allowed',
|
|
35
|
+
demandOption: false,
|
|
36
|
+
type: 'number',
|
|
37
|
+
default: null
|
|
38
|
+
},
|
|
39
|
+
h: {
|
|
40
|
+
alias: 'maxHeight',
|
|
41
|
+
describe: 'Maximum height allowed',
|
|
42
|
+
demandOption: false,
|
|
43
|
+
type: 'number',
|
|
44
|
+
default: null
|
|
45
|
+
},
|
|
46
|
+
i: {
|
|
47
|
+
alias: 'input',
|
|
48
|
+
describe: 'Path to the input directory',
|
|
49
|
+
demandOption: false,
|
|
50
|
+
type: 'string',
|
|
51
|
+
default: null
|
|
52
|
+
},
|
|
53
|
+
o: {
|
|
54
|
+
alias: 'output',
|
|
55
|
+
describe: 'Path to the output directory',
|
|
56
|
+
demandOption: false,
|
|
57
|
+
type: 'string',
|
|
58
|
+
default: null
|
|
17
59
|
}
|
|
18
60
|
}).argv
|
|
19
61
|
}
|
|
20
62
|
|
|
21
63
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
64
|
+
* Creates an output directory if it doesn't exist.
|
|
65
|
+
* @param {string} dir - The directory path to create.
|
|
66
|
+
* @returns {Promise<void>}
|
|
25
67
|
*/
|
|
26
68
|
async function createOutputDirectory (dir) {
|
|
27
69
|
try {
|
|
@@ -61,64 +103,37 @@ async function processImage (inputPath, outputPath, file, maxWidth, maxHeight, q
|
|
|
61
103
|
console.log(`Optimized PNG image: ${file}`)
|
|
62
104
|
}
|
|
63
105
|
} catch (error) {
|
|
64
|
-
console.error(`Optimization error ${file}: `, error)
|
|
106
|
+
// console.error(`Optimization error ${file}: `, error)
|
|
65
107
|
}
|
|
66
108
|
}
|
|
67
109
|
|
|
68
110
|
/**
|
|
69
|
-
* Load settings from a configuration file.
|
|
111
|
+
* Load settings from a configuration file. If the specified configuration file doesn't exist,
|
|
112
|
+
* it will use the default 'config.json' from the package.
|
|
70
113
|
*
|
|
71
114
|
* @param {string} configFile - Path to the configuration file.
|
|
72
115
|
*/
|
|
73
116
|
async function loadConfig (configFile) {
|
|
117
|
+
let config
|
|
74
118
|
try {
|
|
75
119
|
const configData = await fs.readFile(configFile, 'utf8')
|
|
76
|
-
|
|
77
|
-
if (configData.trim() === '') {
|
|
78
|
-
console.error('Configuration file is empty.')
|
|
79
|
-
process.exit(1)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!validateJson(configData)) {
|
|
83
|
-
console.error('Configuration file is not a valid JSON object.')
|
|
84
|
-
process.exit(1)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const config = JSON.parse(configData)
|
|
88
|
-
|
|
89
|
-
validateConfig(config)
|
|
90
|
-
|
|
91
|
-
if (typeof config !== 'object' || Array.isArray(config)) {
|
|
92
|
-
console.error('Configuration file is not a valid JSON object.')
|
|
93
|
-
process.exit(1)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!config.inputDir || !config.outputDir) {
|
|
97
|
-
console.error("Configuration error: 'inputDir' and 'outputDir' are required fields.")
|
|
98
|
-
process.exit(1)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return config
|
|
120
|
+
config = JSON.parse(configData)
|
|
102
121
|
} catch (error) {
|
|
103
|
-
console.error('
|
|
104
|
-
|
|
122
|
+
console.error('Using the default configuration from the package. ✅')
|
|
123
|
+
config = require('./config.json') // Load default configuration from the package
|
|
105
124
|
}
|
|
106
|
-
}
|
|
107
125
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
} catch (e) {
|
|
113
|
-
// Error
|
|
114
|
-
// JSON is not okay
|
|
115
|
-
return false
|
|
116
|
-
}
|
|
126
|
+
if (!validateConfigObject(config)) {
|
|
127
|
+
console.error('Invalid configuration object.')
|
|
128
|
+
process.exit(1)
|
|
129
|
+
}
|
|
117
130
|
|
|
118
|
-
|
|
131
|
+
if (!config.inputDir || !config.outputDir) {
|
|
132
|
+
console.error("Configuration error: 'inputDir' and 'outputDir' are required fields.")
|
|
133
|
+
process.exit(1)
|
|
119
134
|
}
|
|
120
135
|
|
|
121
|
-
return
|
|
136
|
+
return config
|
|
122
137
|
}
|
|
123
138
|
|
|
124
139
|
/**
|
|
@@ -126,11 +141,28 @@ function validateJson (json) {
|
|
|
126
141
|
*
|
|
127
142
|
* @param {object} config - The loaded configuration object.
|
|
128
143
|
*/
|
|
129
|
-
function
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
144
|
+
function validateConfigObject (config) {
|
|
145
|
+
return (
|
|
146
|
+
typeof config === 'object' &&
|
|
147
|
+
!Array.isArray(config) &&
|
|
148
|
+
config.inputDir &&
|
|
149
|
+
config.outputDir
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns the input directory path based on the provided arguments and configuration.
|
|
155
|
+
* If the provided arguments or configuration are '.', returns the current working directory.
|
|
156
|
+
* @param {string} argvInput - The input directory path provided as an argument.
|
|
157
|
+
* @param {string} configInputDir - The input directory path provided in the configuration.
|
|
158
|
+
* @returns {string} - The input directory path.
|
|
159
|
+
*/
|
|
160
|
+
function getInputDirectory (argvInput, configInputDir) {
|
|
161
|
+
if (argvInput === '.' || configInputDir === '.') {
|
|
162
|
+
return process.cwd()
|
|
133
163
|
}
|
|
164
|
+
|
|
165
|
+
return argvInput || configInputDir
|
|
134
166
|
}
|
|
135
167
|
|
|
136
168
|
/**
|
|
@@ -139,13 +171,12 @@ function validateConfig (config) {
|
|
|
139
171
|
async function main () {
|
|
140
172
|
const argv = parseArguments()
|
|
141
173
|
const config = await loadConfig(argv.config)
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
const compressionLevel = config.compressionLevel
|
|
174
|
+
const inputDir = getInputDirectory(argv.input, config.inputDir)
|
|
175
|
+
const outputDir = getInputDirectory(argv.output, config.outputDir)
|
|
176
|
+
const quality = argv.quality ? argv.quality : config.quality
|
|
177
|
+
const maxWidth = argv.maxWidth ? argv.maxWidth : config.maxWidth
|
|
178
|
+
const maxHeight = argv.maxHeight ? argv.maxHeight : config.maxHeight
|
|
179
|
+
const compressionLevel = argv.compressionLevel ? argv.compressionLevel : config.compressionLevel
|
|
149
180
|
|
|
150
181
|
await createOutputDirectory(outputDir)
|
|
151
182
|
|