wiredep-cli.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var pkg = require('./package.json');
  4. var wiredep = require('./wiredep');
  5. var argv = require('minimist')(process.argv.slice(2));
  6. var chalk = require('chalk');
  7. var fs = require('fs');
  8. var args = [
  9. { short: 'h', full: 'help' },
  10. { short: 'v', full: 'version' },
  11. { short: 'b', full: 'bowerJson' },
  12. { short: 'd', full: 'directory' },
  13. { short: 'e', full: 'exclude' },
  14. { short: 'i', full: 'ignorePath' },
  15. { short: 's', full: 'src' }
  16. ];
  17. function help() {
  18. console.log(pkg.description);
  19. console.log('');
  20. console.log('Usage: ' + chalk.cyan('$') + chalk.bold(' wiredep ') + chalk.yellow('[options]'));
  21. console.log('');
  22. console.log('Options:');
  23. console.log(' -h, --help # Print usage information');
  24. console.log(' -v, --version # Print the version');
  25. console.log(' -b, --bowerJson # Path to `bower.json`');
  26. console.log(' -d, --directory # Your Bower directory');
  27. console.log(' -e, --exclude # A path to be excluded');
  28. console.log(' -i, --ignorePath # A path to be ignored');
  29. console.log(' -s, --src # Path to your source file');
  30. console.log(' --dependencies # Include Bower `dependencies`');
  31. console.log(' --devDependencies # Include Bower `devDependencies`');
  32. console.log(' --includeSelf # Include top-level `main` files');
  33. console.log(' --verbose # Print the results of `wiredep`');
  34. }
  35. if (argv.v || argv.version) {
  36. console.log(pkg.version);
  37. return;
  38. }
  39. if (argv.h || argv.help || Object.keys(argv).length === 1) {
  40. help();
  41. return;
  42. }
  43. if (!argv.s && !argv.src) {
  44. console.log(chalk.bold.red('> Source file not specified.'));
  45. console.log('Please pass a `--src path/to/source.html` to `wiredep`.');
  46. return;
  47. }
  48. if (argv.b || argv.bowerJson) {
  49. try {
  50. argv.bowerJson = JSON.parse(fs.readFileSync(argv.b || argv.bowerJson));
  51. } catch (e) {}
  52. }
  53. try {
  54. if (!argv.bowerJson) {
  55. fs.statSync('./bower.json');
  56. }
  57. } catch (e) {
  58. console.log(chalk.bold.red('> bower.json not found.'));
  59. console.log('Please run `wiredep` from the directory where your `bower.json` file is located.');
  60. console.log('Alternatively, pass a `--bowerJson path/to/bower.json`.');
  61. return;
  62. }
  63. var results = wiredep(Object.keys(argv).reduce(function (acc, arg) {
  64. args.filter(function (argObj) {
  65. if (argObj.short === arg) {
  66. acc[argObj.full] = argv[arg];
  67. delete acc[arg];
  68. }
  69. });
  70. return acc;
  71. }, argv));
  72. if (argv.verbose) {
  73. console.log(results);
  74. }