Skip to content

Commit

Permalink
Drop handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
t32k committed Dec 29, 2016
1 parent 26b63d2 commit 62f7344
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 429 deletions.
15 changes: 1 addition & 14 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ program
.version(require('../package.json').version)
.usage('[options] <file ...>')
.option('-c, --config <path>', 'set configurations')
.option('-f, --format <format>', 'set the output format <json|html|md|csv>')
.option('-t, --template <path>', 'set the template path for output formant')
.option('-f, --format <format>', 'set the output format <json|csv>')
.option('-n, --number', 'show only numeral metrics')
.option('-m, --mobile', 'set the mobile user agent')
.parse(process.argv);
Expand Down Expand Up @@ -66,20 +65,8 @@ const stats = new StyleStats(program.args, config);
stats.parse()
.then(result => {
const format = new Format(result);
// Custom format
if (fs.existsSync(program.template)) {
format.setTemplate(fs.readFileSync(program.template, {encoding: 'utf8'}));
console.log(format.getFormattedText());
return;
}
// Other formants
switch (program.format) {
case 'md':
console.log(format.toMarkdown());
break;
case 'html':
console.log(format.toHTML());
break;
case 'json':
console.log(format.toJSON());
break;
Expand Down
12 changes: 6 additions & 6 deletions lib/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ class Analyzer {
* @param {Number} cssSize
* @param {Object} options
*/
constructor(rules, selectors, declarations, cssString, cssSize, options) {
constructor(data, options) {
// array of rule
// referenced in analyzeRules
this.rules = rules;
this.rules = data.rules;

// array of css selector
// referenced in analyzeSelectors
this.selectors = selectors;
this.selectors = data.selectors;

// array of css declaration
// referenced in analyzeDeclarations
this.declarations = declarations;
this.declarations = data.declarations;

// all of css string
this.cssString = cssString;
this.cssString = data.cssString;

// size of css
this.cssSize = cssSize;
this.cssSize = data.cssSize;

// result options
this.options = options;
Expand Down
35 changes: 1 addition & 34 deletions lib/format.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
const fs = require('fs');
const path = require('path');
const json2csv = require('json2csv');
const Table = require('cli-table');
const Handlebars = require('handlebars');
const json2csv = require('json2csv');
const prettify = require('../lib/prettify');

Handlebars.registerHelper('removeBreak', text => {
text = Handlebars.Utils.escapeExpression(text);
text = text.replace(/(\r\n|\n|\r)/gm, ' ');
return new Handlebars.SafeString(text);
});

class Format {
constructor(context) {
this.context = context;
this.template = null;
}

setTemplate(source) {
this.template = Handlebars.compile(source || '');
}

getFormattedText() {
if (!this.template) {
throw new Error('Template is not set');
}
return this.template(prettify(this.context));
}

toMarkdown() {
const file = path.join(__dirname, '../assets/markdown.hbs');
this.setTemplate(fs.readFileSync(file, {encoding: 'utf8'}));
return this.getFormattedText();
}

toHTML() {
const file = path.join(__dirname, '../assets/html.hbs');
this.setTemplate(fs.readFileSync(file, {encoding: 'utf8'}));
return this.getFormattedText();
}

toJSON() {
Expand Down
9 changes: 1 addition & 8 deletions lib/stylestats.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,7 @@ class StyleStats {

return new Promise((resolve, reject) => {
this.parser.parse().then(data => {
const analyzedData = new Analyzer(
data.rules,
data.selectors,
data.declarations,
data.cssString,
data.cssSize,
that.options
).analyze();
const analyzedData = new Analyzer(data, that.options).analyze();

const stats = {};
if (that.options.published) {
Expand Down
39 changes: 0 additions & 39 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,6 @@ test.cb('It should return JSON format', t => {
});
});

test.cb('It should return HTML format', t => {
t.plan(1);
cmds.push('--format', 'html');
exec(cmds.join(' '), (err, stdout) => {
if (err) {
t.fail();
}
const fixture = fs.readFileSync('test/fixture/cli/example.html', 'utf-8');
t.is(stdout, fixture);
t.end();
});
});

test.cb('It should return Markdown format', t => {
t.plan(1);
cmds.push('--format', 'md');
exec(cmds.join(' '), (err, stdout) => {
if (err) {
t.fail();
}
const fixture = fs.readFileSync('test/fixture/cli/example.md', 'utf-8');
t.is(stdout, fixture);
t.end();
});
});

test.cb('It should return CSV format', t => {
t.plan(1);
cmds.push('--format', 'csv');
Expand All @@ -70,16 +44,3 @@ test.cb('It should return CSV format', t => {
t.end();
});
});

test.cb('It should return custom template format', t => {
t.plan(1);
cmds.push('--template test/fixture/cli/template.hbs');
exec(cmds.join(' '), (err, stdout) => {
if (err) {
t.fail();
}
const fixture = fs.readFileSync('test/fixture/cli/custom-template.html', 'utf-8');
t.is(stdout, fixture);
t.end();
});
});
141 changes: 0 additions & 141 deletions test/fixture/cli/custom-template.html

This file was deleted.

0 comments on commit 62f7344

Please sign in to comment.