25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

189 satır
4.0 KiB

  1. // Growl - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
  2. /**
  3. * Module dependencies.
  4. */
  5. var exec = require('child_process').exec
  6. , path = require('path')
  7. , os = require('os')
  8. , cmd;
  9. switch(os.type()) {
  10. case 'Darwin':
  11. cmd = {
  12. type: "Darwin"
  13. , pkg: "growlnotify"
  14. , msg: '-m'
  15. , sticky: '--sticky'
  16. , priority: {
  17. cmd: '--priority'
  18. , range: [
  19. -2
  20. , -1
  21. , 0
  22. , 1
  23. , 2
  24. , "Very Low"
  25. , "Moderate"
  26. , "Normal"
  27. , "High"
  28. , "Emergency"
  29. ]
  30. }
  31. };
  32. break;
  33. case 'Linux':
  34. cmd = {
  35. type: "Linux"
  36. , pkg: "notify-send"
  37. , msg: ''
  38. , sticky: '-t 0'
  39. , icon: '-i'
  40. , priority: {
  41. cmd: '-u'
  42. , range: [
  43. "low"
  44. , "normal"
  45. , "critical"
  46. ]
  47. }
  48. };
  49. break;
  50. case 'Windows_NT':
  51. cmd = {
  52. type: "Windows"
  53. , pkg: "growlnotify"
  54. , msg: ''
  55. , sticky: '/s:true'
  56. , title: '/t:'
  57. , icon: '/i:'
  58. , priority: {
  59. cmd: '/p:'
  60. , range: [
  61. -2
  62. , -1
  63. , 0
  64. , 1
  65. , 2
  66. ]
  67. }
  68. };
  69. break;
  70. }
  71. /**
  72. * Expose `growl`.
  73. */
  74. exports = module.exports = growl;
  75. /**
  76. * Node-growl version.
  77. */
  78. exports.version = '1.4.1'
  79. /**
  80. * Send growl notification _msg_ with _options_.
  81. *
  82. * Options:
  83. *
  84. * - title Notification title
  85. * - sticky Make the notification stick (defaults to false)
  86. * - priority Specify an int or named key (default is 0)
  87. * - name Application name (defaults to growlnotify)
  88. * - image
  89. * - path to an icon sets --iconpath
  90. * - path to an image sets --image
  91. * - capitalized word sets --appIcon
  92. * - filename uses extname as --icon
  93. * - otherwise treated as --icon
  94. *
  95. * Examples:
  96. *
  97. * growl('New email')
  98. * growl('5 new emails', { title: 'Thunderbird' })
  99. * growl('Email sent', function(){
  100. * // ... notification sent
  101. * })
  102. *
  103. * @param {string} msg
  104. * @param {object} options
  105. * @param {function} fn
  106. * @api public
  107. */
  108. function growl(msg, options, fn) {
  109. var image
  110. , args
  111. , options = options || {}
  112. , fn = fn || function(){};
  113. // noop
  114. if (!cmd) return fn(new Error('growl not supported on this platform'));
  115. args = [cmd.pkg];
  116. // image
  117. if (image = options.image) {
  118. switch(cmd.type) {
  119. case 'Darwin':
  120. var flag, ext = path.extname(image).substr(1)
  121. flag = flag || ext == 'icns' && 'iconpath'
  122. flag = flag || /^[A-Z]/.test(image) && 'appIcon'
  123. flag = flag || /^png|gif|jpe?g$/.test(ext) && 'image'
  124. flag = flag || ext && (image = ext) && 'icon'
  125. flag = flag || 'icon'
  126. args.push('--' + flag, image)
  127. break;
  128. case 'Linux':
  129. args.push(cmd.icon + image);
  130. break;
  131. case 'Windows':
  132. args.push(cmd.icon + '"' + image.replace(/\\/g, "\\\\") + '"');
  133. break;
  134. }
  135. }
  136. // sticky
  137. if (options.sticky) args.push(cmd.sticky);
  138. // priority
  139. if (options.priority) {
  140. var priority = options.priority + '';
  141. var checkindexOf = cmd.priority.range.indexOf(priority);
  142. if (~cmd.priority.range.indexOf(priority)) {
  143. args.push(cmd.priority, options.priority);
  144. }
  145. }
  146. // name
  147. if (options.name && cmd.type === "Darwin") {
  148. args.push('--name', options.name);
  149. }
  150. switch(cmd.type) {
  151. case 'Darwin':
  152. args.push(cmd.msg);
  153. args.push('"' + msg + '"');
  154. if (options.title) args.push(options.title);
  155. break;
  156. case 'Linux':
  157. if (options.title) {
  158. args.push("'" + options.title + "'");
  159. args.push(cmd.msg);
  160. args.push("'" + msg + "'");
  161. } else {
  162. args.push("'" + msg + "'");
  163. }
  164. break;
  165. case 'Windows':
  166. args.push('"' + msg + '"');
  167. if (options.title) args.push(cmd.title + '"' + options.title + '"');
  168. break;
  169. }
  170. // execute
  171. exec(args.join(' '), fn);
  172. };