Skip to content

Commit

Permalink
Merge pull request #22 from pocketjoso/fix-node-module
Browse files Browse the repository at this point in the history
fix node module
  • Loading branch information
pocketjoso committed Feb 28, 2015
2 parents 7c3deb6 + 1747a63 commit b4f7ded
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Generate an interactive line graph showing the specificity in your stylesheet. U
## Usage
`specifity-graph` can be used in different ways:
* [CLI](#cli)
* [Node module](#node-module)
* [Via JavaScript](#via-javascript)
* [Online](http://jonassebastianohlsson.com/specificity-graph/)

Expand All @@ -30,12 +31,20 @@ Creates a directory containing
| -b --browser | auto launch browser to view generated chart |
| -h --help | Help |

### Node module
```
var specificityGraph = require('specificity-graph');
specificityGraph(directory, css, function(directory){
console.log('specificity-graph files created in ' + directory);
});
```

### Via JavaScript
First get the `specificityGraph` accessible in your code:

#### Using Browserify (or similar)
Just `require('specificity-graph')`
Just `require('specificity-graph')`.

#### Standalone (no module loader)
Add `<script src="specificity-graph-standalone.js"></script>`,
Expand Down
46 changes: 15 additions & 31 deletions bin/specificity-graph
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env node

/*
* Executable that will be available in the path by executing `npm install specificity-graph -g`.
* If installed as a dependency the binary will be placed in node_modules/.bin/specificity-graph
*/

'use strict';

var fs = require('fs'),
path = require('path'),
colors = require('cli-color'),
commander = require('commander'),
open = require('opn'),
generateCssData = require('../lib/generateCssData'),
generateCustomSpecificityFiles = require('../lib/cli');
specificityGraph = require('../lib/index');

commander
.usage('<cssFile> [options]')
Expand All @@ -32,18 +36,19 @@ if (!input){
console.error(colors.red('Missing arguments'));
console.log('usage: ' + '<cssFile> [options]. See --help for more info');
process.exit(1);
}
if(input && !fs.existsSync(input)) {
console.error(colors.red('Unable to read file'), input);
process.exit(1);
}
} else {
if(!fs.existsSync(input)) {
console.error(colors.red('Unable to read file'), input);
process.exit(1);
}

if(input) {
try {
var css = fs.readFileSync(input);
var specificity = generateCssData(css.toString());

generateCustomSpecificityFiles(output, specificity, function (dest){
specificityGraph(output, css, function (dest, err){
if(err) {
console.log(colors.yellow('[specificity-graph] The directory already exist'));
}
console.log(colors.blue('[specificity-graph]') + ' Write specificity in ' + dest);
if(openInBrowser){
open(path.join(dest, 'index.html'));
Expand All @@ -54,24 +59,3 @@ if(input) {
error(e);
}
}


if(!input) {
process.stdin.resume();
process.stdin.setEncoding('utf8');

var buffer = '';
process.stdin.on('data', function(data) {
buffer += data;
});

process.stdin.on('end', function() {
try {
var specificity = generateCssData(buffer);
console.log(JSON.stringify(specificity,null, 2));

}catch(e) {
error(e);
}
});
}
24 changes: 13 additions & 11 deletions lib/cli.js → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/*
* Node module wrapper for specificity-graph
*/

var fs = require('fs'),
path = require('path');
mkdirp = require('mkdirp');
colors = require('cli-color');
path = require('path'),
mkdirp = require('mkdirp'),
colors = require('cli-color'),
generateCssData = require('./generateCssData');

/**
* Generates CSS specificity data in JSON format,
Expand All @@ -12,17 +17,16 @@ var fs = require('fs'),
* @param {Function} cb Success callback
* @return {void}
*/
function main(directory, data, cb) {
module.exports = function(directory, css, cb) {
var directory = directory || './specificity-graph';
fs.mkdir(directory, function (err) {
if(err) {
console.log(colors.yellow('[specificity-graph] The directory already exist'));
}
cb = cb || function(){};
var json = JSON.stringify(data);

var json = JSON.stringify(generateCssData(css.toString()));

writeSpecificityJson(json, directory);
addExampleFiles(json, directory);
cb(directory);
cb(directory, err);
});
}

Expand Down Expand Up @@ -70,5 +74,3 @@ function copyFileToDir(file, directory) {
.createReadStream(path.join(__dirname, '../' + file))
.pipe(fs.createWriteStream(path.join(directory, file)));
}

module.exports = main;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "specificity-graph",
"description": "Generate an interactive Specificity Graph for your CSS.",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "https://github.com/pocketjoso/specificity-graph",
"author": {
"name": "Jonas Ohlsson",
Expand All @@ -16,7 +16,7 @@
"url": "https://github.com/pocketjoso/specificity-graph/issues"
},
"license": "MIT",
"main": "./bin/specificity-graph",
"main": "./lib/index.js",
"bin": {
"specificity-graph": "./bin/specificity-graph"
},
Expand Down

0 comments on commit b4f7ded

Please sign in to comment.