Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.0.0 #28

Merged
merged 8 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ All notable changes to this project will be documented in this file.

### Tests

## [2.0.0]
### BREAKING CHANGES
- `.sass` files are not included in the graph by default. Use the `-e .sass` flag.

### Features
- Configurable file extensions - [@dannymidnight](https://github.com/dannymidnight), [@xzyfer](https://github.com/xzyfer)

### Fixed
- Prioritize cwd when resolving load paths - [@schnerd](https://github.com/schnerd)

### Tests
- Added test for prioritizing cwd when resolving load paths - [@xzyfer](https://github.com/xzyfer)

## [1.3.0]
### Features
- Add support for indented syntax - [@vegetableman](https://github.com/vegetableman)
Expand Down
140 changes: 88 additions & 52 deletions bin/sassgraph
Original file line number Diff line number Diff line change
@@ -1,80 +1,116 @@
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');

var path = require("path");
var fs = require("fs");
var program = require("commander");

var sassGraph = require("../sass-graph");
var package = require("../package.json");

program.version("Sass Graph Version " + package.version);

program
.usage([
"[OPTIONS] DIR FILE",
program._version,
"DIR is a directory containing scss files. FILE is a scss file to analyze."
].join("\n\n "))
.option("-I <dir>", "Add dir to the sass load path. Multiple dirs can be comma delimited.")
.option("-a", "Print ancestors")
.option("-d", "Print descendents")
.option("--json", "Prints the index in json")
.option("-v, --version", "Display version information")
.parse(process.argv);

if ( ! program.args.length) {
program.help();
var command, directory, file;

var yargs = require('yargs')
.usage('Usage: $0 <command> [options] <dir> [file]')
// .demand(1)

.command('ancestors', 'Output the ancestors')
.command('descendents', 'Output the descendents')

.example('$0 ancestors -I src src/ src/_footer.scss', 'outputs the ancestors of src/_footer.scss')

.option('I', {
alias: 'load-path',
default: [process.cwd()],
describe: 'Add directories to the sass load path',
type: 'array',
})

.option('e', {
alias: 'extensions',
default: ['scss', 'css'],
describe: 'File extensions to include in the graph',
type: 'array',
})

.option('j', {
alias: 'json',
default: false,
describe: 'Output the index in json',
type: 'bool',
})

.version(function() {
return require('../package').version;
})
.alias('v', 'version')

.help('h')
.alias('h', 'help');

var argv = yargs.argv;

if (argv._.length === 0) {
yargs.showHelp();
process.exit(1);
}

var loadPaths = [];
if (['ancestors', 'descendents'].indexOf(argv._[0]) !== -1) {
command = argv._.shift();
}

// load paths are comma delimited
if(program.I) {
loadPaths = program.I.split(/,/).map(function(f){
return path.resolve(f);
});
if (argv._ && path.extname(argv._[0]) === '') {
directory = argv._.shift();
}

// SASS_PATH can contain a colon delimited list of paths
if(process.env.SASS_PATH) {
loadPaths = loadPaths.concat(process.env.SASS_PATH.split(/:/).map(function(f){
return path.resolve(f);
}));
if (argv._ && path.extname(argv._[0])) {
file = argv._.shift();
}

var sassDir = program.args[0];
var sassFile = program.args[1];

try {
if(!fs.lstatSync(sassDir).isDirectory()) {
console.error("%s must be a directory", sassDir);
process.exit(1);
if (!directory) {
throw new Error('Missing directory');
}

var graph = sassGraph.parseDir(sassDir, { loadPaths: loadPaths });
if (!command && !argv.json) {
throw new Error('Missing command');
}

if(program.json) {
if (!file && (command === 'ancestors' || command === 'descendents')) {
throw new Error(command + ' command requires a file');
}

var loadPaths = argv.loadPath;
if(process.env.SASS_PATH) {
loadPaths = loadPaths.concat(process.env.SASS_PATH.split(/:/).map(function(f) {
return path.resolve(f);
}));
}

var graph = require('../').parseDir(directory, {
extensions: argv.extensions,
loadPaths: loadPaths,
});

if(argv.json) {
console.log(JSON.stringify(graph.index, null, 4));
process.exit(0);
}

if (program.A) {
graph.visitAncestors(path.resolve(sassFile), function(f) {
if (command === 'ancestors') {
graph.visitAncestors(path.resolve(file), function(f) {
console.log(f);
});
}
if (program.D) {
graph.visitDescendents(path.resolve(sassFile), function(f) {
console.log(f);
});
}
if (program.B) {
graph.visitDescendents(path.resolve(sassFile), function(f) {

if (command === 'descendents') {
graph.visitDescendents(path.resolve(file), function(f) {
console.log(f);
});
}
} catch(e) {
console.log(e);
if (e.code === 'ENOENT') {
console.error('Error: no such file or directory "' + e.path + '"');
}
else {
console.log('Error: ' + e.message);
}

// console.log(e.stack);
process.exit(1);
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"graph"
],
"dependencies": {
"commander": "^2.6.0",
"glob": "^4.3.4",
"lodash": "^2.4.1"
"glob": "^5.0.5",
"lodash": "^3.8.0",
"yargs": "^3.8.0"
},
"devDependencies": {
"assert": "^1.3.0",
Expand Down
91 changes: 68 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sass Graph

Parses sass and exposes a graph of dependencies
Parses Sass files in a directory and exposes a graph of dependencies

[![Build Status](https://travis-ci.org/xzyfer/sass-graph.svg?branch=master)](https://travis-ci.org/xzyfer/sass-graph)
[![npm version](https://badge.fury.io/js/sass-graph.svg)](http://badge.fury.io/js/sass-graph)
Expand All @@ -20,35 +20,80 @@ npm install --save-dev sass-graph
Usage as a Node library:

```js
$ node
> var sassGraph = require('./sass-graph');
undefined
> sassGraph.parseDir('tests/fixtures');
{ index: {,
'tests/fixtures/a.scss': {
imports: ['b.scss'],
importedBy: [],
},
'tests/fixtures/b.scss': {
imports: ['_c.scss'],
importedBy: ['a.scss'],
},
'tests/fixtures/_c.scss': {
imports: [],
importedBy: ['b/scss'],
},
}}
var sassGraph = require('./sass-graph');
```

Usage as a command line tool:

The command line tool will parse a graph and then either display ancestors, descendents or both.

```
$ ./bin/sassgraph tests/fixtures tests/fixtures/a.scss -d
tests/fixtures/a.scss
tests/fixtures/b.scss
tests/fixtures/_c.scss
$ ./bin/sassgraph --help
Usage: bin/sassgraph <command> [options] <dir> [file]

Commands:
ancestors Output the ancestors
descendents Output the descendents

Options:
-I, --load-path Add directories to the sass load path
-e, --extensions File extensions to include in the graph
-j, --json Output the index in json
-h, --help Show help
-v, --version Show version number

Examples:
./bin/sassgraph descendents test/fixtures test/fixtures/a.scss
/path/to/test/fixtures/b.scss
/path/to/test/fixtures/_c.scss
```

## API

#### parseDir

Parses a directory and builds a dependency graph of all requested file extensions.

#### parseFile

Parses a file and builds its dependency graph.

## Options

#### loadPaths

Type: `Array`
Default: `[process.cwd]`

Directories to use when resolved `@import` directives.

#### extensions

Type: `Array`
Default: `['scss', 'css']`

File types to be parsed.

## Example

```js
var sassGraph = require('./sass-graph');
console.log(sassGraph.parseDir('test/fixtures'));

//{ index: {,
// '/path/to/test/fixtures/a.scss': {
// imports: ['b.scss'],
// importedBy: [],
// },
// '/path/to/test/fixtures/b.scss': {
// imports: ['_c.scss'],
// importedBy: ['a.scss'],
// },
// '/path/to/test/fixtures/_c.scss': {
// imports: [],
// importedBy: ['b/scss'],
// },
//}}
```

## Running Mocha tests
Expand Down
Loading