uuid 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env node
  2. var assert = require('assert');
  3. function usage() {
  4. console.log('Usage:');
  5. console.log(' uuid');
  6. console.log(' uuid v1');
  7. console.log(' uuid v3 <name> <namespace uuid>');
  8. console.log(' uuid v4');
  9. console.log(' uuid v5 <name> <namespace uuid>');
  10. console.log(' uuid --help');
  11. console.log('\nNote: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122');
  12. }
  13. var args = process.argv.slice(2);
  14. if (args.indexOf('--help') >= 0) {
  15. usage();
  16. process.exit(0);
  17. }
  18. var version = args.shift() || 'v4';
  19. switch (version) {
  20. case 'v1':
  21. var uuidV1 = require('../v1');
  22. console.log(uuidV1());
  23. break;
  24. case 'v3':
  25. var uuidV3 = require('../v3');
  26. var name = args.shift();
  27. var namespace = args.shift();
  28. assert(name != null, 'v3 name not specified');
  29. assert(namespace != null, 'v3 namespace not specified');
  30. if (namespace == 'URL') namespace = uuidV3.URL;
  31. if (namespace == 'DNS') namespace = uuidV3.DNS;
  32. console.log(uuidV3(name, namespace));
  33. break;
  34. case 'v4':
  35. var uuidV4 = require('../v4');
  36. console.log(uuidV4());
  37. break;
  38. case 'v5':
  39. var uuidV5 = require('../v5');
  40. var name = args.shift();
  41. var namespace = args.shift();
  42. assert(name != null, 'v5 name not specified');
  43. assert(namespace != null, 'v5 namespace not specified');
  44. if (namespace == 'URL') namespace = uuidV5.URL;
  45. if (namespace == 'DNS') namespace = uuidV5.DNS;
  46. console.log(uuidV5(name, namespace));
  47. break;
  48. default:
  49. usage();
  50. process.exit(1);
  51. }