2 var fs = require('fs');
3 var path = require('path');
5 var command, directory, file;
7 var yargs = require('yargs')
8 .usage('Usage: $0 <command> [options] <dir> [file]')
11 .command('ancestors', 'Output the ancestors')
12 .command('descendents', 'Output the descendents')
14 .example('$0 ancestors -I src src/ src/_footer.scss', 'outputs the ancestors of src/_footer.scss')
18 default: [process.cwd()],
19 describe: 'Add directories to the sass load path',
25 default: ['scss', 'css'],
26 describe: 'File extensions to include in the graph',
33 describe: 'Output the index in json',
38 return require('../package').version;
40 .alias('v', 'version')
45 var argv = yargs.argv;
47 if (argv._.length === 0) {
52 if (['ancestors', 'descendents'].indexOf(argv._[0]) !== -1) {
53 command = argv._.shift();
56 if (argv._ && path.extname(argv._[0]) === '') {
57 directory = argv._.shift();
60 if (argv._ && path.extname(argv._[0])) {
61 file = argv._.shift();
67 throw new Error('Missing directory');
70 if (!command && !argv.json) {
71 throw new Error('Missing command');
74 if (!file && (command === 'ancestors' || command === 'descendents')) {
75 throw new Error(command + ' command requires a file');
78 var loadPaths = argv.loadPath;
79 if(process.env.SASS_PATH) {
80 loadPaths = loadPaths.concat(process.env.SASS_PATH.split(/:/).map(function(f) {
81 return path.resolve(f);
85 var graph = require('../').parseDir(directory, {
86 extensions: argv.extensions,
91 console.log(JSON.stringify(graph.index, null, 4));
95 if (command === 'ancestors') {
96 graph.visitAncestors(path.resolve(file), function(f) {
101 if (command === 'descendents') {
102 graph.visitDescendents(path.resolve(file), function(f) {
107 if (e.code === 'ENOENT') {
108 console.error('Error: no such file or directory "' + e.path + '"');
111 console.log('Error: ' + e.message);
114 // console.log(e.stack);