aberlaas-compress 2.10.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Tim Carry (tim@pixelastic.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/lib/dummy.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * This is a temporary, dummy file to be able to test the top-level
3
+ * compress.run() function. I assume I'll add more compression types in addition
4
+ * to png, so this dummy compress is just to test that all compress are
5
+ * correctly called, but it doesn't do anything
6
+ */
7
+ export default {
8
+ async run() {
9
+ return true;
10
+ },
11
+ };
package/lib/main.js ADDED
@@ -0,0 +1,41 @@
1
+ import { _, pMap } from 'golgoth';
2
+ import { consoleError, firostError } from 'firost';
3
+ import compressPng from './png.js';
4
+ import compressDummy from './dummy.js';
5
+
6
+ export default {
7
+ types: {
8
+ png: compressPng,
9
+ dummy: compressDummy,
10
+ },
11
+ /**
12
+ * Wrapper to compress all supported formats
13
+ * @param {object} cliArgs CLI Argument object, as created by minimist
14
+ * @returns {boolean} True on success
15
+ */
16
+ async run(cliArgs) {
17
+ const allTypesKeys = _.keys(this.types);
18
+ const userTypes = _.intersection(_.keys(cliArgs), allTypesKeys);
19
+ const typesToCompress = _.isEmpty(userTypes) ? allTypesKeys : userTypes;
20
+
21
+ let hasErrors = false;
22
+ await pMap(typesToCompress, async (type) => {
23
+ try {
24
+ const userPatterns = _.get(cliArgs, '_');
25
+ const compresser = this.types[type];
26
+
27
+ await compresser.run(userPatterns);
28
+ } catch (error) {
29
+ this.__consoleError(error.message);
30
+ hasErrors = true;
31
+ }
32
+ });
33
+
34
+ if (hasErrors) {
35
+ throw firostError('ERROR_COMPRESS', 'Error while compressing files');
36
+ }
37
+
38
+ return true;
39
+ },
40
+ __consoleError: consoleError,
41
+ };
package/lib/png.js ADDED
@@ -0,0 +1,48 @@
1
+ import { _ } from 'golgoth';
2
+ import { firostError, run, which } from 'firost';
3
+ import helper from 'aberlaas-helper';
4
+
5
+ export default {
6
+ /**
7
+ * Find the png files to compress
8
+ * @param {Array} userPatterns Patterns to narrow the search down
9
+ * @returns {Array} Array of files
10
+ */
11
+ async getInputFiles(userPatterns) {
12
+ const filePatterns = _.isEmpty(userPatterns)
13
+ ? ['./**/*.png']
14
+ : userPatterns;
15
+ return await helper.findHostFiles(filePatterns, ['.png']);
16
+ },
17
+
18
+ /**
19
+ * Returns path to the binary to execute
20
+ * @returns {string|boolean} Path to the binary, or false if not found
21
+ */
22
+ async getBinaryPath() {
23
+ return await this.__which('pngmin');
24
+ },
25
+ /**
26
+ * Compress files
27
+ * @param {Array} userPatterns Patterns to narrow the search down
28
+ * @returns {boolean} True on success
29
+ */
30
+ async run(userPatterns) {
31
+ // Stop early if no bin
32
+ const binaryPath = await this.getBinaryPath();
33
+ if (!binaryPath) {
34
+ return true;
35
+ }
36
+
37
+ try {
38
+ const files = await this.getInputFiles(userPatterns);
39
+ const command = `${binaryPath} ${files.join(' ')}`;
40
+ await run(command, { stdout: false });
41
+ } catch (error) {
42
+ throw firostError('PngCompressError', error.message);
43
+ }
44
+
45
+ return true;
46
+ },
47
+ __which: which,
48
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "aberlaas-compress",
3
+ "type": "module",
4
+ "description": "aberlaas compress command: Reduce image filesize",
5
+ "version": "2.10.0",
6
+ "repository": "pixelastic/aberlaas",
7
+ "homepage": "https://projects.pixelastic.com/aberlaas/",
8
+ "author": "Tim Carry (@pixelastic)",
9
+ "license": "MIT",
10
+ "files": [
11
+ "lib/*.js"
12
+ ],
13
+ "exports": {
14
+ ".": "./lib/main.js"
15
+ },
16
+ "main": "./lib/main.js",
17
+ "engines": {
18
+ "node": ">=18.18.0"
19
+ },
20
+ "scripts": {
21
+ "build": "../../scripts/local/build",
22
+ "build:prod": "../../scripts/local/build-prod",
23
+ "cms": "../../scripts/local/cms",
24
+ "serve": "../../scripts/local/serve",
25
+ "ci": "../../scripts/local/ci",
26
+ "release": "../../scripts/local/release",
27
+ "update": "node ../../scripts/meta/update.js",
28
+ "test:meta": "../../scripts/local/test-meta",
29
+ "test": "../../scripts/local/test",
30
+ "test:watch": "../../scripts/local/test-watch",
31
+ "compress": "../../scripts/local/compress",
32
+ "lint": "../../scripts/local/lint",
33
+ "lint:fix": "../../scripts/local/lint-fix"
34
+ },
35
+ "dependencies": {
36
+ "aberlaas-helper": "^2.10.0",
37
+ "firost": "4.3.0",
38
+ "golgoth": "2.4.0"
39
+ },
40
+ "gitHead": "bcdaf87c198a588e02b5539c222f611e356d3079"
41
+ }