cli.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env node
  2. /**
  3. * dateformat <https://github.com/felixge/node-dateformat>
  4. *
  5. * Copyright (c) 2014 Charlike Mike Reagent (cli), contributors.
  6. * Released under the MIT license.
  7. */
  8. 'use strict';
  9. /**
  10. * Module dependencies.
  11. */
  12. var dateFormat = require('../lib/dateformat');
  13. var meow = require('meow');
  14. var stdin = require('get-stdin');
  15. var cli = meow({
  16. pkg: '../package.json',
  17. help: [
  18. 'Options',
  19. ' --help Show this help',
  20. ' --version Current version of package',
  21. ' -d | --date Date that want to format (Date object as Number or String)',
  22. ' -m | --mask Mask that will use to format the date',
  23. ' -u | --utc Convert local time to UTC time or use `UTC:` prefix in mask',
  24. ' -g | --gmt You can use `GMT:` prefix in mask',
  25. '',
  26. 'Usage',
  27. ' dateformat [date] [mask]',
  28. ' dateformat "Nov 26 2014" "fullDate"',
  29. ' dateformat 1416985417095 "dddd, mmmm dS, yyyy, h:MM:ss TT"',
  30. ' dateformat 1315361943159 "W"',
  31. ' dateformat "UTC:h:MM:ss TT Z"',
  32. ' dateformat "longTime" true',
  33. ' dateformat "longTime" false true',
  34. ' dateformat "Jun 9 2007" "fullDate" true',
  35. ' date +%s | dateformat',
  36. ''
  37. ].join('\n')
  38. })
  39. var date = cli.input[0] || cli.flags.d || cli.flags.date || Date.now();
  40. var mask = cli.input[1] || cli.flags.m || cli.flags.mask || dateFormat.masks.default;
  41. var utc = cli.input[2] || cli.flags.u || cli.flags.utc || false;
  42. var gmt = cli.input[3] || cli.flags.g || cli.flags.gmt || false;
  43. utc = utc === 'true' ? true : false;
  44. gmt = gmt === 'true' ? true : false;
  45. if (!cli.input.length) {
  46. stdin(function(date) {
  47. console.log(dateFormat(date, dateFormat.masks.default, utc, gmt));
  48. });
  49. return;
  50. }
  51. if (cli.input.length === 1 && date) {
  52. mask = date;
  53. date = Date.now();
  54. console.log(dateFormat(date, mask, utc, gmt));
  55. return;
  56. }
  57. if (cli.input.length >= 2 && date && mask) {
  58. if (mask === 'true' || mask === 'false') {
  59. utc = mask === 'true' ? true : false;
  60. gmt = !utc;
  61. mask = date
  62. date = Date.now();
  63. }
  64. console.log(dateFormat(date, mask, utc, gmt));
  65. return;
  66. }