Skip to content

Commit

Permalink
Parse CLI args passed as JSON (#596) (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb authored Aug 17, 2024
1 parent ef92de3 commit 0b01821
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 97 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {getOptions} from './src/config.js';
*/
export async function generate(params, cb) {
try {
const options = getOptions(params);
const options = await getOptions(params);
const {target = {}, base = process.cwd()} = options;
const result = await create(options);
// Store generated css
Expand Down
130 changes: 72 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,32 @@
"node": ">=18.18"
},
"dependencies": {
"@adobe/css-tools": "^4.3.3",
"@adobe/css-tools": "^4.4.0",
"async-traverse-tree": "^1.1.0",
"clean-css": "^5.3.3",
"common-tags": "^1.8.2",
"css-url-parser": "^1.1.3",
"data-uri-to-buffer": "^6.0.1",
"debug": "^4.3.4",
"css-url-parser": "^1.1.4",
"data-uri-to-buffer": "^6.0.2",
"debug": "^4.3.6",
"find-up": "^7.0.0",
"get-stdin": "^9.0.0",
"globby": "^14.0.1",
"globby": "^14.0.2",
"got": "^13.0.0",
"group-args": "^0.1.0",
"indent-string": "^5.0.0",
"inline-critical": "^12.0.1",
"is-glob": "^4.0.3",
"joi": "^17.12.2",
"joi": "^17.13.3",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"make-dir": "^4.0.0",
"meow": "^13.2.0",
"oust": "^2.0.4",
"p-all": "^5.0.0",
"penthouse": "^2.3.3",
"picocolors": "^1.0.0",
"picocolors": "^1.0.1",
"plugin-error": "^2.0.1",
"postcss": "^8.4.38",
"postcss": "^8.4.41",
"postcss-discard": "^2.0.0",
"postcss-image-inliner": "^7.0.1",
"postcss-url": "^10.1.3",
Expand Down
20 changes: 18 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import process from 'node:process';
import Joi from 'joi';
import debugBase from 'debug';
import {traverse, STOP} from 'async-traverse-tree';
import {ConfigError} from './errors.js';

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

export function getOptions(options = {}) {
const {error, value} = schema.validate(options);
export async function getOptions(options = {}) {
const parsedOptions = await traverse(options, (key, value) => {
if (['css', 'html', 'src'].includes(key)) {
return STOP;
}

if (typeof value === 'string') {
try {
return JSON.parse(value);
} catch {}
}

return value;
});

const {error, value} = schema.validate(parsedOptions);

const {inline, dimensions, penthouse = {}, target, ignore} = value || {};

if (error) {
Expand Down
Loading

0 comments on commit 0b01821

Please sign in to comment.