sassgraph
2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
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', 'sass'],
describe: 'File extensions to include in the graph',
type: 'array',
})
.option('f', {
alias: 'follow',
default: false,
describe: 'Follow symbolic links',
type: 'bool',
})
.option('j', {
alias: 'json',
default: false,
describe: 'Output the index in json',
type: 'bool',
})
.version()
.alias('v', 'version')
.help('h')
.alias('h', 'help');
var argv = yargs.argv;
if (argv._.length === 0) {
yargs.showHelp();
process.exit(1);
}
if (['ancestors', 'descendents'].indexOf(argv._[0]) !== -1) {
command = argv._.shift();
}
if (argv._.length && path.extname(argv._[0]) === '') {
directory = argv._.shift();
}
if (argv._.length && path.extname(argv._[0])) {
file = argv._.shift();
}
try {
if (!directory) {
throw new Error('Missing directory');
}
if (!command && !argv.json) {
throw new Error('Missing command');
}
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,
follow: argv.follow,
});
if(argv.json) {
console.log(JSON.stringify(graph.index, null, 4));
process.exit(0);
}
if (command === 'ancestors') {
graph.visitAncestors(path.resolve(file), function(f) {
console.log(f);
});
}
if (command === 'descendents') {
graph.visitDescendents(path.resolve(file), function(f) {
console.log(f);
});
}
} catch(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);
}