Skip to content

Commit 0b01821

Browse files
authored
Parse CLI args passed as JSON (#596) (#599)
1 parent ef92de3 commit 0b01821

5 files changed

Lines changed: 135 additions & 97 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {getOptions} from './src/config.js';
1616
*/
1717
export async function generate(params, cb) {
1818
try {
19-
const options = getOptions(params);
19+
const options = await getOptions(params);
2020
const {target = {}, base = process.cwd()} = options;
2121
const result = await create(options);
2222
// Store generated css

package-lock.json

Lines changed: 72 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,32 @@
3232
"node": ">=18.18"
3333
},
3434
"dependencies": {
35-
"@adobe/css-tools": "^4.3.3",
35+
"@adobe/css-tools": "^4.4.0",
36+
"async-traverse-tree": "^1.1.0",
3637
"clean-css": "^5.3.3",
3738
"common-tags": "^1.8.2",
38-
"css-url-parser": "^1.1.3",
39-
"data-uri-to-buffer": "^6.0.1",
40-
"debug": "^4.3.4",
39+
"css-url-parser": "^1.1.4",
40+
"data-uri-to-buffer": "^6.0.2",
41+
"debug": "^4.3.6",
4142
"find-up": "^7.0.0",
4243
"get-stdin": "^9.0.0",
43-
"globby": "^14.0.1",
44+
"globby": "^14.0.2",
4445
"got": "^13.0.0",
4546
"group-args": "^0.1.0",
4647
"indent-string": "^5.0.0",
4748
"inline-critical": "^12.0.1",
4849
"is-glob": "^4.0.3",
49-
"joi": "^17.12.2",
50+
"joi": "^17.13.3",
5051
"lodash": "^4.17.21",
5152
"lodash-es": "^4.17.21",
5253
"make-dir": "^4.0.0",
5354
"meow": "^13.2.0",
5455
"oust": "^2.0.4",
5556
"p-all": "^5.0.0",
5657
"penthouse": "^2.3.3",
57-
"picocolors": "^1.0.0",
58+
"picocolors": "^1.0.1",
5859
"plugin-error": "^2.0.1",
59-
"postcss": "^8.4.38",
60+
"postcss": "^8.4.41",
6061
"postcss-discard": "^2.0.0",
6162
"postcss-image-inliner": "^7.0.1",
6263
"postcss-url": "^10.1.3",

src/config.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process';
22
import Joi from 'joi';
33
import debugBase from 'debug';
4+
import {traverse, STOP} from 'async-traverse-tree';
45
import {ConfigError} from './errors.js';
56

67
const debug = debugBase('critical:config');
@@ -75,8 +76,23 @@ const schema = Joi.object()
7576
.label('options')
7677
.xor('html', 'src');
7778

78-
export function getOptions(options = {}) {
79-
const {error, value} = schema.validate(options);
79+
export async function getOptions(options = {}) {
80+
const parsedOptions = await traverse(options, (key, value) => {
81+
if (['css', 'html', 'src'].includes(key)) {
82+
return STOP;
83+
}
84+
85+
if (typeof value === 'string') {
86+
try {
87+
return JSON.parse(value);
88+
} catch {}
89+
}
90+
91+
return value;
92+
});
93+
94+
const {error, value} = schema.validate(parsedOptions);
95+
8096
const {inline, dimensions, penthouse = {}, target, ignore} = value || {};
8197

8298
if (error) {

0 commit comments

Comments
 (0)