Skip to content

Commit

Permalink
Move tests to Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Dec 18, 2018
1 parent 14222f2 commit f813627
Show file tree
Hide file tree
Showing 9 changed files with 6,557 additions and 8,384 deletions.
209 changes: 107 additions & 102 deletions cli.js
Expand Up @@ -24,135 +24,140 @@ Options:
`;

const cli = meow(help, {
autoHelp: true,
autoVersion: true,
flags: {
css: {
type: 'string',
alias: 'c'
},
html: {
type: 'string',
alias: 'h'
},
ignore: {
type: 'string',
alias: 'i'
},
minify: {
type: 'boolean',
alias: 'm'
},
extract: {
type: 'boolean',
alias: 'e'
},
base: {
type: 'string',
alias: 'b'
},
selector: {
type: 'string',
alias: 's'
}
}
autoHelp: true,
autoVersion: true,
flags: {
css: {
type: 'string',
alias: 'c',
},
html: {
type: 'string',
alias: 'h',
},
ignore: {
type: 'string',
alias: 'i',
},
minify: {
type: 'boolean',
alias: 'm',
},
extract: {
type: 'boolean',
alias: 'e',
},
base: {
type: 'string',
alias: 'b',
},
selector: {
type: 'string',
alias: 's',
},
},
});

// Cleanup cli flags
cli.flags = _.reduce(cli.flags, (res, val, key) => {
cli.flags = _.reduce(
cli.flags,
(res, val, key) => {
if (key.length <= 1) {
return res;
return res;
}

switch (key) {
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (error) {
}
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
res.ignore = _.map(val || [], ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);

if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
return ignore;
});
break;
default:
res[key] = val;
break;
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (error) {}
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
res.ignore = _.map(val || [], ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);

if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
return ignore;
});
break;
default:
res[key] = val;
break;
}

return res;
}, {});
},
{}
);

function processError(err) {
process.stderr.write(indentString('Error: ' + (err.message || err), 4));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help, 4));
process.exit(1);
process.stderr.write(indentString('Error: ' + (err.message || err), 4));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help, 4));
// Process.exit(1);
}

function read(file) {
try {
return fs.readFileSync(file, 'utf8');
} catch (error) {
processError(error);
}
try {
return fs.readFileSync(file, 'utf8');
} catch (error) {
processError(error);
}
}

function run(data) {
const opts = _.defaults(cli.flags, {basePath: process.cwd()});
ok = true;
const opts = _.defaults(cli.flags, {basePath: process.cwd()});
ok = true;

if (data) {
// Detect html
try {
css.parse(data);
opts.css = data;
} catch (error) {
opts.html = data;
}
}

_.forEach(cli.input, file => {
const tmp = read(file);
try {
css.parse(tmp);
opts.css = tmp;
} catch (error) {
opts.html = tmp;
}
});

if (!opts.html || !opts.css) {
cli.showHelp();
if (data) {
// Detect html
try {
css.parse(data);
opts.css = data;
} catch (error) {
opts.html = data;
}
}

_.forEach(cli.input, file => {
const tmp = read(file);
try {
const out = inlineCritical(opts.html, opts.css, opts);
process.stdout.write(out.toString(), process.exit);
css.parse(tmp);
opts.css = tmp;
} catch (error) {
processError(error);
opts.html = tmp;
}
});

if (!opts.html || !opts.css) {
cli.showHelp();
}

const {html, css: styles, ...options} = opts;

try {
const out = inlineCritical(html, styles, options);
process.stdout.write(out.toString(), process.exit);
} catch (error) {
processError(error);
}
}

// Get stdin
stdin().then(run);
setTimeout(() => {
if (ok) {
return;
}
run();
if (ok) {
return;
}
run();
}, 100);

0 comments on commit f813627

Please sign in to comment.