Initial commit
[yaffs-website] / node_modules / sass-graph / bin / sassgraph
1 #!/usr/bin/env node
2 var fs = require('fs');
3 var path = require('path');
4
5 var command, directory, file;
6
7 var yargs = require('yargs')
8     .usage('Usage: $0 <command> [options] <dir> [file]')
9     // .demand(1)
10
11     .command('ancestors', 'Output the ancestors')
12     .command('descendents', 'Output the descendents')
13
14     .example('$0 ancestors -I src src/ src/_footer.scss', 'outputs the ancestors of src/_footer.scss')
15
16     .option('I', {
17       alias: 'load-path',
18       default: [process.cwd()],
19       describe: 'Add directories to the sass load path',
20       type: 'array',
21     })
22
23     .option('e', {
24       alias: 'extensions',
25       default: ['scss', 'css'],
26       describe: 'File extensions to include in the graph',
27       type: 'array',
28     })
29
30     .option('j', {
31       alias: 'json',
32       default: false,
33       describe: 'Output the index in json',
34       type: 'bool',
35     })
36
37     .version(function() {
38       return require('../package').version;
39     })
40     .alias('v', 'version')
41
42     .help('h')
43     .alias('h', 'help');
44
45 var argv = yargs.argv;
46
47 if (argv._.length === 0) {
48   yargs.showHelp();
49   process.exit(1);
50 }
51
52 if (['ancestors', 'descendents'].indexOf(argv._[0]) !== -1) {
53   command = argv._.shift();
54 }
55
56 if (argv._ && path.extname(argv._[0]) === '') {
57   directory = argv._.shift();
58 }
59
60 if (argv._ && path.extname(argv._[0])) {
61   file = argv._.shift();
62 }
63
64
65 try {
66   if (!directory) {
67     throw new Error('Missing directory');
68   }
69
70   if (!command && !argv.json) {
71     throw new Error('Missing command');
72   }
73
74   if (!file && (command === 'ancestors' || command === 'descendents')) {
75     throw new Error(command + ' command requires a file');
76   }
77
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);
82     }));
83   }
84
85   var graph = require('../').parseDir(directory, {
86     extensions: argv.extensions,
87     loadPaths: loadPaths,
88   });
89
90   if(argv.json) {
91     console.log(JSON.stringify(graph.index, null, 4));
92     process.exit(0);
93   }
94
95   if (command === 'ancestors') {
96     graph.visitAncestors(path.resolve(file), function(f) {
97       console.log(f);
98     });
99   }
100
101   if (command === 'descendents') {
102     graph.visitDescendents(path.resolve(file), function(f) {
103       console.log(f);
104     });
105   }
106 } catch(e) {
107   if (e.code === 'ENOENT') {
108     console.error('Error: no such file or directory "' + e.path + '"');
109   }
110   else {
111     console.log('Error: ' + e.message);
112   }
113
114   // console.log(e.stack);
115   process.exit(1);
116 }