sassgraph 2.6 KB

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