style-xlsx-m 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/CHANGELOG.md +264 -0
  2. package/LICENSE +201 -0
  3. package/README.md +145 -0
  4. package/bin/xlsx.njs +310 -0
  5. package/bower.json +22 -0
  6. package/dist/LICENSE +201 -0
  7. package/dist/cpexcel.full.mjs +1502 -0
  8. package/dist/cpexcel.js +1506 -0
  9. package/dist/shim.min.js +2 -0
  10. package/dist/xlsx.core.min.js +15 -0
  11. package/dist/xlsx.core.min.map +1 -0
  12. package/dist/xlsx.extendscript.js +24642 -0
  13. package/dist/xlsx.full.min.js +22 -0
  14. package/dist/xlsx.full.min.map +1 -0
  15. package/dist/xlsx.mini.min.js +9 -0
  16. package/dist/xlsx.mini.min.map +1 -0
  17. package/dist/xlsx.zahl.js +4 -0
  18. package/dist/xlsx.zahl.mjs +2 -0
  19. package/package.json +101 -0
  20. package/types/index.d.ts +900 -0
  21. package/types/tsconfig.json +15 -0
  22. package/xlsx-js-style/LICENSE +201 -0
  23. package/xlsx-js-style/bin/xlsx.njs +284 -0
  24. package/xlsx-js-style/dist/LICENSE +201 -0
  25. package/xlsx-js-style/dist/cpexcel.full.mjs +1502 -0
  26. package/xlsx-js-style/dist/cpexcel.js +1506 -0
  27. package/xlsx-js-style/dist/shim.min.js +2 -0
  28. package/xlsx-js-style/dist/xlsx.bundle.js +3 -0
  29. package/xlsx-js-style/dist/xlsx.bundle.js.map +1 -0
  30. package/xlsx-js-style/dist/xlsx.min.js +3 -0
  31. package/xlsx-js-style/dist/xlsx.min.js.map +1 -0
  32. package/xlsx-js-style/node_modules/.bin/adler32 +12 -0
  33. package/xlsx-js-style/node_modules/.bin/adler32.cmd +17 -0
  34. package/xlsx-js-style/node_modules/.bin/adler32.ps1 +28 -0
  35. package/xlsx-js-style/node_modules/.bin/codepage +12 -0
  36. package/xlsx-js-style/node_modules/.bin/codepage.cmd +17 -0
  37. package/xlsx-js-style/node_modules/.bin/codepage.ps1 +28 -0
  38. package/xlsx-js-style/node_modules/adler-32/LICENSE +14 -0
  39. package/xlsx-js-style/node_modules/adler-32/README.md +139 -0
  40. package/xlsx-js-style/node_modules/adler-32/adler32.js +92 -0
  41. package/xlsx-js-style/node_modules/adler-32/bin/adler32.njs +87 -0
  42. package/xlsx-js-style/node_modules/adler-32/package.json +42 -0
  43. package/xlsx-js-style/node_modules/adler-32/types/index.d.ts +14 -0
  44. package/xlsx-js-style/node_modules/adler-32/types/tsconfig.json +14 -0
  45. package/xlsx-js-style/node_modules/adler-32/types/tslint.json +11 -0
  46. package/xlsx-js-style/node_modules/codepage/LICENSE +14 -0
  47. package/xlsx-js-style/node_modules/codepage/README.md +347 -0
  48. package/xlsx-js-style/node_modules/codepage/bin/codepage.njs +98 -0
  49. package/xlsx-js-style/node_modules/codepage/bits/10000.js +2 -0
  50. package/xlsx-js-style/node_modules/codepage/bits/10001.js +115 -0
  51. package/xlsx-js-style/node_modules/codepage/bits/10002.js +253 -0
  52. package/xlsx-js-style/node_modules/codepage/bits/10003.js +183 -0
  53. package/xlsx-js-style/node_modules/codepage/bits/10004.js +2 -0
  54. package/xlsx-js-style/node_modules/codepage/bits/10005.js +2 -0
  55. package/xlsx-js-style/node_modules/codepage/bits/10006.js +2 -0
  56. package/xlsx-js-style/node_modules/codepage/bits/10007.js +2 -0
  57. package/xlsx-js-style/node_modules/codepage/bits/10008.js +167 -0
  58. package/xlsx-js-style/node_modules/codepage/bits/10010.js +2 -0
  59. package/xlsx-js-style/node_modules/codepage/bits/10017.js +2 -0
  60. package/xlsx-js-style/node_modules/codepage/bits/10021.js +2 -0
  61. package/xlsx-js-style/node_modules/codepage/bits/10029.js +2 -0
  62. package/xlsx-js-style/node_modules/codepage/bits/10079.js +2 -0
  63. package/xlsx-js-style/node_modules/codepage/bits/10081.js +2 -0
  64. package/xlsx-js-style/node_modules/codepage/bits/10082.js +2 -0
  65. package/xlsx-js-style/node_modules/codepage/bits/1010.js +2 -0
  66. package/xlsx-js-style/node_modules/codepage/bits/1026.js +2 -0
  67. package/xlsx-js-style/node_modules/codepage/bits/1047.js +2 -0
  68. package/xlsx-js-style/node_modules/codepage/bits/1132.js +2 -0
  69. package/xlsx-js-style/node_modules/codepage/bits/1140.js +2 -0
  70. package/xlsx-js-style/node_modules/codepage/bits/1141.js +2 -0
  71. package/xlsx-js-style/node_modules/codepage/bits/1142.js +2 -0
  72. package/xlsx-js-style/node_modules/codepage/bits/1143.js +2 -0
  73. package/xlsx-js-style/node_modules/codepage/bits/1144.js +2 -0
  74. package/xlsx-js-style/node_modules/codepage/bits/1145.js +2 -0
  75. package/xlsx-js-style/node_modules/codepage/bits/1146.js +2 -0
  76. package/xlsx-js-style/node_modules/codepage/bits/1147.js +2 -0
  77. package/xlsx-js-style/node_modules/codepage/bits/1148.js +2 -0
  78. package/xlsx-js-style/node_modules/codepage/bits/1149.js +2 -0
  79. package/xlsx-js-style/node_modules/codepage/bits/1250.js +2 -0
  80. package/xlsx-js-style/node_modules/codepage/bits/1251.js +2 -0
  81. package/xlsx-js-style/node_modules/codepage/bits/1252.js +2 -0
  82. package/xlsx-js-style/node_modules/codepage/bits/1253.js +2 -0
  83. package/xlsx-js-style/node_modules/codepage/bits/1254.js +2 -0
  84. package/xlsx-js-style/node_modules/codepage/bits/1255.js +2 -0
  85. package/xlsx-js-style/node_modules/codepage/bits/1256.js +2 -0
  86. package/xlsx-js-style/node_modules/codepage/bits/1257.js +2 -0
  87. package/xlsx-js-style/node_modules/codepage/bits/1258.js +2 -0
  88. package/xlsx-js-style/node_modules/codepage/bits/1361.js +231 -0
  89. package/xlsx-js-style/node_modules/codepage/bits/20000.js +193 -0
  90. package/xlsx-js-style/node_modules/codepage/bits/20001.js +217 -0
  91. package/xlsx-js-style/node_modules/codepage/bits/20002.js +167 -0
  92. package/xlsx-js-style/node_modules/codepage/bits/20003.js +213 -0
  93. package/xlsx-js-style/node_modules/codepage/bits/20004.js +193 -0
  94. package/xlsx-js-style/node_modules/codepage/bits/20005.js +223 -0
  95. package/xlsx-js-style/node_modules/codepage/bits/20105.js +2 -0
  96. package/xlsx-js-style/node_modules/codepage/bits/20106.js +2 -0
  97. package/xlsx-js-style/node_modules/codepage/bits/20107.js +2 -0
  98. package/xlsx-js-style/node_modules/codepage/bits/20108.js +2 -0
  99. package/xlsx-js-style/node_modules/codepage/bits/20261.js +31 -0
  100. package/xlsx-js-style/node_modules/codepage/bits/20269.js +2 -0
  101. package/xlsx-js-style/node_modules/codepage/bits/20273.js +2 -0
  102. package/xlsx-js-style/node_modules/codepage/bits/20277.js +2 -0
  103. package/xlsx-js-style/node_modules/codepage/bits/20278.js +2 -0
  104. package/xlsx-js-style/node_modules/codepage/bits/20280.js +2 -0
  105. package/xlsx-js-style/node_modules/codepage/bits/20284.js +2 -0
  106. package/xlsx-js-style/node_modules/codepage/bits/20285.js +2 -0
  107. package/xlsx-js-style/node_modules/codepage/bits/20290.js +2 -0
  108. package/xlsx-js-style/node_modules/codepage/bits/20297.js +2 -0
  109. package/xlsx-js-style/node_modules/codepage/bits/20420.js +2 -0
  110. package/xlsx-js-style/node_modules/codepage/bits/20423.js +2 -0
  111. package/xlsx-js-style/node_modules/codepage/bits/20424.js +2 -0
  112. package/xlsx-js-style/node_modules/codepage/bits/20833.js +2 -0
  113. package/xlsx-js-style/node_modules/codepage/bits/20838.js +2 -0
  114. package/xlsx-js-style/node_modules/codepage/bits/20866.js +2 -0
  115. package/xlsx-js-style/node_modules/codepage/bits/20871.js +2 -0
  116. package/xlsx-js-style/node_modules/codepage/bits/20880.js +2 -0
  117. package/xlsx-js-style/node_modules/codepage/bits/20905.js +2 -0
  118. package/xlsx-js-style/node_modules/codepage/bits/20924.js +2 -0
  119. package/xlsx-js-style/node_modules/codepage/bits/20932.js +189 -0
  120. package/xlsx-js-style/node_modules/codepage/bits/20936.js +167 -0
  121. package/xlsx-js-style/node_modules/codepage/bits/20949.js +183 -0
  122. package/xlsx-js-style/node_modules/codepage/bits/21025.js +2 -0
  123. package/xlsx-js-style/node_modules/codepage/bits/21027.js +2 -0
  124. package/xlsx-js-style/node_modules/codepage/bits/21866.js +2 -0
  125. package/xlsx-js-style/node_modules/codepage/bits/28591.js +2 -0
  126. package/xlsx-js-style/node_modules/codepage/bits/28592.js +2 -0
  127. package/xlsx-js-style/node_modules/codepage/bits/28593.js +2 -0
  128. package/xlsx-js-style/node_modules/codepage/bits/28594.js +2 -0
  129. package/xlsx-js-style/node_modules/codepage/bits/28595.js +2 -0
  130. package/xlsx-js-style/node_modules/codepage/bits/28596.js +2 -0
  131. package/xlsx-js-style/node_modules/codepage/bits/28597.js +2 -0
  132. package/xlsx-js-style/node_modules/codepage/bits/28598.js +2 -0
  133. package/xlsx-js-style/node_modules/codepage/bits/28599.js +2 -0
  134. package/xlsx-js-style/node_modules/codepage/bits/28600.js +2 -0
  135. package/xlsx-js-style/node_modules/codepage/bits/28601.js +2 -0
  136. package/xlsx-js-style/node_modules/codepage/bits/28603.js +2 -0
  137. package/xlsx-js-style/node_modules/codepage/bits/28604.js +2 -0
  138. package/xlsx-js-style/node_modules/codepage/bits/28605.js +2 -0
  139. package/xlsx-js-style/node_modules/codepage/bits/28606.js +2 -0
  140. package/xlsx-js-style/node_modules/codepage/bits/29001.js +2 -0
  141. package/xlsx-js-style/node_modules/codepage/bits/37.js +2 -0
  142. package/xlsx-js-style/node_modules/codepage/bits/38598.js +2 -0
  143. package/xlsx-js-style/node_modules/codepage/bits/437.js +2 -0
  144. package/xlsx-js-style/node_modules/codepage/bits/47451.js +2 -0
  145. package/xlsx-js-style/node_modules/codepage/bits/500.js +2 -0
  146. package/xlsx-js-style/node_modules/codepage/bits/50220.js +395 -0
  147. package/xlsx-js-style/node_modules/codepage/bits/50221.js +395 -0
  148. package/xlsx-js-style/node_modules/codepage/bits/50222.js +395 -0
  149. package/xlsx-js-style/node_modules/codepage/bits/50225.js +259 -0
  150. package/xlsx-js-style/node_modules/codepage/bits/50227.js +257 -0
  151. package/xlsx-js-style/node_modules/codepage/bits/51932.js +171 -0
  152. package/xlsx-js-style/node_modules/codepage/bits/51936.js +257 -0
  153. package/xlsx-js-style/node_modules/codepage/bits/51949.js +183 -0
  154. package/xlsx-js-style/node_modules/codepage/bits/52936.js +7 -0
  155. package/xlsx-js-style/node_modules/codepage/bits/54936.js +257 -0
  156. package/xlsx-js-style/node_modules/codepage/bits/57002.js +39 -0
  157. package/xlsx-js-style/node_modules/codepage/bits/57003.js +23 -0
  158. package/xlsx-js-style/node_modules/codepage/bits/57004.js +7 -0
  159. package/xlsx-js-style/node_modules/codepage/bits/57005.js +15 -0
  160. package/xlsx-js-style/node_modules/codepage/bits/57006.js +23 -0
  161. package/xlsx-js-style/node_modules/codepage/bits/57007.js +21 -0
  162. package/xlsx-js-style/node_modules/codepage/bits/57008.js +17 -0
  163. package/xlsx-js-style/node_modules/codepage/bits/57009.js +13 -0
  164. package/xlsx-js-style/node_modules/codepage/bits/57010.js +15 -0
  165. package/xlsx-js-style/node_modules/codepage/bits/57011.js +17 -0
  166. package/xlsx-js-style/node_modules/codepage/bits/620.js +2 -0
  167. package/xlsx-js-style/node_modules/codepage/bits/708.js +2 -0
  168. package/xlsx-js-style/node_modules/codepage/bits/720.js +2 -0
  169. package/xlsx-js-style/node_modules/codepage/bits/737.js +2 -0
  170. package/xlsx-js-style/node_modules/codepage/bits/775.js +2 -0
  171. package/xlsx-js-style/node_modules/codepage/bits/808.js +2 -0
  172. package/xlsx-js-style/node_modules/codepage/bits/850.js +2 -0
  173. package/xlsx-js-style/node_modules/codepage/bits/852.js +2 -0
  174. package/xlsx-js-style/node_modules/codepage/bits/855.js +2 -0
  175. package/xlsx-js-style/node_modules/codepage/bits/857.js +2 -0
  176. package/xlsx-js-style/node_modules/codepage/bits/858.js +2 -0
  177. package/xlsx-js-style/node_modules/codepage/bits/860.js +2 -0
  178. package/xlsx-js-style/node_modules/codepage/bits/861.js +2 -0
  179. package/xlsx-js-style/node_modules/codepage/bits/862.js +2 -0
  180. package/xlsx-js-style/node_modules/codepage/bits/863.js +2 -0
  181. package/xlsx-js-style/node_modules/codepage/bits/864.js +2 -0
  182. package/xlsx-js-style/node_modules/codepage/bits/865.js +2 -0
  183. package/xlsx-js-style/node_modules/codepage/bits/866.js +2 -0
  184. package/xlsx-js-style/node_modules/codepage/bits/869.js +2 -0
  185. package/xlsx-js-style/node_modules/codepage/bits/870.js +2 -0
  186. package/xlsx-js-style/node_modules/codepage/bits/872.js +2 -0
  187. package/xlsx-js-style/node_modules/codepage/bits/874.js +2 -0
  188. package/xlsx-js-style/node_modules/codepage/bits/875.js +2 -0
  189. package/xlsx-js-style/node_modules/codepage/bits/895.js +2 -0
  190. package/xlsx-js-style/node_modules/codepage/bits/932.js +95 -0
  191. package/xlsx-js-style/node_modules/codepage/bits/936.js +257 -0
  192. package/xlsx-js-style/node_modules/codepage/bits/949.js +253 -0
  193. package/xlsx-js-style/node_modules/codepage/bits/950.js +179 -0
  194. package/xlsx-js-style/node_modules/codepage/cptable.js +6347 -0
  195. package/xlsx-js-style/node_modules/codepage/cputils.js +528 -0
  196. package/xlsx-js-style/node_modules/codepage/dist/LICENSE +14 -0
  197. package/xlsx-js-style/node_modules/codepage/dist/cpexcel.full.js +1505 -0
  198. package/xlsx-js-style/node_modules/codepage/dist/cputils.js +528 -0
  199. package/xlsx-js-style/node_modules/codepage/dist/sbcs.full.js +639 -0
  200. package/xlsx-js-style/node_modules/codepage/node_modules/commander/CHANGELOG.md +350 -0
  201. package/xlsx-js-style/node_modules/codepage/node_modules/commander/LICENSE +22 -0
  202. package/xlsx-js-style/node_modules/codepage/node_modules/commander/Readme.md +408 -0
  203. package/xlsx-js-style/node_modules/codepage/node_modules/commander/index.js +1157 -0
  204. package/xlsx-js-style/node_modules/codepage/node_modules/commander/package.json +37 -0
  205. package/xlsx-js-style/node_modules/codepage/node_modules/commander/typings/index.d.ts +309 -0
  206. package/xlsx-js-style/node_modules/codepage/package.json +67 -0
  207. package/xlsx-js-style/node_modules/codepage/types/index.d.ts +39 -0
  208. package/xlsx-js-style/node_modules/codepage/types/tsconfig.json +14 -0
  209. package/xlsx-js-style/node_modules/codepage/types/tslint.json +11 -0
  210. package/xlsx-js-style/package.json +90 -0
  211. package/xlsx-js-style/types/index.d.ts +1065 -0
  212. package/xlsx-js-style/types/tsconfig.json +15 -0
  213. package/xlsx.js +24711 -0
  214. package/xlsx.mjs +24597 -0
  215. package/xlsxworker.js +14 -0
@@ -0,0 +1,1157 @@
1
+ /**
2
+ * Module dependencies.
3
+ */
4
+
5
+ var EventEmitter = require('events').EventEmitter;
6
+ var spawn = require('child_process').spawn;
7
+ var path = require('path');
8
+ var dirname = path.dirname;
9
+ var basename = path.basename;
10
+ var fs = require('fs');
11
+
12
+ /**
13
+ * Inherit `Command` from `EventEmitter.prototype`.
14
+ */
15
+
16
+ require('util').inherits(Command, EventEmitter);
17
+
18
+ /**
19
+ * Expose the root command.
20
+ */
21
+
22
+ exports = module.exports = new Command();
23
+
24
+ /**
25
+ * Expose `Command`.
26
+ */
27
+
28
+ exports.Command = Command;
29
+
30
+ /**
31
+ * Expose `Option`.
32
+ */
33
+
34
+ exports.Option = Option;
35
+
36
+ /**
37
+ * Initialize a new `Option` with the given `flags` and `description`.
38
+ *
39
+ * @param {String} flags
40
+ * @param {String} description
41
+ * @api public
42
+ */
43
+
44
+ function Option(flags, description) {
45
+ this.flags = flags;
46
+ this.required = ~flags.indexOf('<');
47
+ this.optional = ~flags.indexOf('[');
48
+ this.bool = !~flags.indexOf('-no-');
49
+ flags = flags.split(/[ ,|]+/);
50
+ if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
51
+ this.long = flags.shift();
52
+ this.description = description || '';
53
+ }
54
+
55
+ /**
56
+ * Return option name.
57
+ *
58
+ * @return {String}
59
+ * @api private
60
+ */
61
+
62
+ Option.prototype.name = function() {
63
+ return this.long
64
+ .replace('--', '')
65
+ .replace('no-', '');
66
+ };
67
+
68
+ /**
69
+ * Return option name, in a camelcase format that can be used
70
+ * as a object attribute key.
71
+ *
72
+ * @return {String}
73
+ * @api private
74
+ */
75
+
76
+ Option.prototype.attributeName = function() {
77
+ return camelcase(this.name());
78
+ };
79
+
80
+ /**
81
+ * Check if `arg` matches the short or long flag.
82
+ *
83
+ * @param {String} arg
84
+ * @return {Boolean}
85
+ * @api private
86
+ */
87
+
88
+ Option.prototype.is = function(arg) {
89
+ return this.short === arg || this.long === arg;
90
+ };
91
+
92
+ /**
93
+ * Initialize a new `Command`.
94
+ *
95
+ * @param {String} name
96
+ * @api public
97
+ */
98
+
99
+ function Command(name) {
100
+ this.commands = [];
101
+ this.options = [];
102
+ this._execs = {};
103
+ this._allowUnknownOption = false;
104
+ this._args = [];
105
+ this._name = name || '';
106
+ }
107
+
108
+ /**
109
+ * Add command `name`.
110
+ *
111
+ * The `.action()` callback is invoked when the
112
+ * command `name` is specified via __ARGV__,
113
+ * and the remaining arguments are applied to the
114
+ * function for access.
115
+ *
116
+ * When the `name` is "*" an un-matched command
117
+ * will be passed as the first arg, followed by
118
+ * the rest of __ARGV__ remaining.
119
+ *
120
+ * Examples:
121
+ *
122
+ * program
123
+ * .version('0.0.1')
124
+ * .option('-C, --chdir <path>', 'change the working directory')
125
+ * .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
126
+ * .option('-T, --no-tests', 'ignore test hook')
127
+ *
128
+ * program
129
+ * .command('setup')
130
+ * .description('run remote setup commands')
131
+ * .action(function() {
132
+ * console.log('setup');
133
+ * });
134
+ *
135
+ * program
136
+ * .command('exec <cmd>')
137
+ * .description('run the given remote command')
138
+ * .action(function(cmd) {
139
+ * console.log('exec "%s"', cmd);
140
+ * });
141
+ *
142
+ * program
143
+ * .command('teardown <dir> [otherDirs...]')
144
+ * .description('run teardown commands')
145
+ * .action(function(dir, otherDirs) {
146
+ * console.log('dir "%s"', dir);
147
+ * if (otherDirs) {
148
+ * otherDirs.forEach(function (oDir) {
149
+ * console.log('dir "%s"', oDir);
150
+ * });
151
+ * }
152
+ * });
153
+ *
154
+ * program
155
+ * .command('*')
156
+ * .description('deploy the given env')
157
+ * .action(function(env) {
158
+ * console.log('deploying "%s"', env);
159
+ * });
160
+ *
161
+ * program.parse(process.argv);
162
+ *
163
+ * @param {String} name
164
+ * @param {String} [desc] for git-style sub-commands
165
+ * @return {Command} the new command
166
+ * @api public
167
+ */
168
+
169
+ Command.prototype.command = function(name, desc, opts) {
170
+ if (typeof desc === 'object' && desc !== null) {
171
+ opts = desc;
172
+ desc = null;
173
+ }
174
+ opts = opts || {};
175
+ var args = name.split(/ +/);
176
+ var cmd = new Command(args.shift());
177
+
178
+ if (desc) {
179
+ cmd.description(desc);
180
+ this.executables = true;
181
+ this._execs[cmd._name] = true;
182
+ if (opts.isDefault) this.defaultExecutable = cmd._name;
183
+ }
184
+ cmd._noHelp = !!opts.noHelp;
185
+ this.commands.push(cmd);
186
+ cmd.parseExpectedArgs(args);
187
+ cmd.parent = this;
188
+
189
+ if (desc) return this;
190
+ return cmd;
191
+ };
192
+
193
+ /**
194
+ * Define argument syntax for the top-level command.
195
+ *
196
+ * @api public
197
+ */
198
+
199
+ Command.prototype.arguments = function(desc) {
200
+ return this.parseExpectedArgs(desc.split(/ +/));
201
+ };
202
+
203
+ /**
204
+ * Add an implicit `help [cmd]` subcommand
205
+ * which invokes `--help` for the given command.
206
+ *
207
+ * @api private
208
+ */
209
+
210
+ Command.prototype.addImplicitHelpCommand = function() {
211
+ this.command('help [cmd]', 'display help for [cmd]');
212
+ };
213
+
214
+ /**
215
+ * Parse expected `args`.
216
+ *
217
+ * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
218
+ *
219
+ * @param {Array} args
220
+ * @return {Command} for chaining
221
+ * @api public
222
+ */
223
+
224
+ Command.prototype.parseExpectedArgs = function(args) {
225
+ if (!args.length) return;
226
+ var self = this;
227
+ args.forEach(function(arg) {
228
+ var argDetails = {
229
+ required: false,
230
+ name: '',
231
+ variadic: false
232
+ };
233
+
234
+ switch (arg[0]) {
235
+ case '<':
236
+ argDetails.required = true;
237
+ argDetails.name = arg.slice(1, -1);
238
+ break;
239
+ case '[':
240
+ argDetails.name = arg.slice(1, -1);
241
+ break;
242
+ }
243
+
244
+ if (argDetails.name.length > 3 && argDetails.name.slice(-3) === '...') {
245
+ argDetails.variadic = true;
246
+ argDetails.name = argDetails.name.slice(0, -3);
247
+ }
248
+ if (argDetails.name) {
249
+ self._args.push(argDetails);
250
+ }
251
+ });
252
+ return this;
253
+ };
254
+
255
+ /**
256
+ * Register callback `fn` for the command.
257
+ *
258
+ * Examples:
259
+ *
260
+ * program
261
+ * .command('help')
262
+ * .description('display verbose help')
263
+ * .action(function() {
264
+ * // output help here
265
+ * });
266
+ *
267
+ * @param {Function} fn
268
+ * @return {Command} for chaining
269
+ * @api public
270
+ */
271
+
272
+ Command.prototype.action = function(fn) {
273
+ var self = this;
274
+ var listener = function(args, unknown) {
275
+ // Parse any so-far unknown options
276
+ args = args || [];
277
+ unknown = unknown || [];
278
+
279
+ var parsed = self.parseOptions(unknown);
280
+
281
+ // Output help if necessary
282
+ outputHelpIfNecessary(self, parsed.unknown);
283
+
284
+ // If there are still any unknown options, then we simply
285
+ // die, unless someone asked for help, in which case we give it
286
+ // to them, and then we die.
287
+ if (parsed.unknown.length > 0) {
288
+ self.unknownOption(parsed.unknown[0]);
289
+ }
290
+
291
+ // Leftover arguments need to be pushed back. Fixes issue #56
292
+ if (parsed.args.length) args = parsed.args.concat(args);
293
+
294
+ self._args.forEach(function(arg, i) {
295
+ if (arg.required && args[i] == null) {
296
+ self.missingArgument(arg.name);
297
+ } else if (arg.variadic) {
298
+ if (i !== self._args.length - 1) {
299
+ self.variadicArgNotLast(arg.name);
300
+ }
301
+
302
+ args[i] = args.splice(i);
303
+ }
304
+ });
305
+
306
+ // Always append ourselves to the end of the arguments,
307
+ // to make sure we match the number of arguments the user
308
+ // expects
309
+ if (self._args.length) {
310
+ args[self._args.length] = self;
311
+ } else {
312
+ args.push(self);
313
+ }
314
+
315
+ fn.apply(self, args);
316
+ };
317
+ var parent = this.parent || this;
318
+ var name = parent === this ? '*' : this._name;
319
+ parent.on('command:' + name, listener);
320
+ if (this._alias) parent.on('command:' + this._alias, listener);
321
+ return this;
322
+ };
323
+
324
+ /**
325
+ * Define option with `flags`, `description` and optional
326
+ * coercion `fn`.
327
+ *
328
+ * The `flags` string should contain both the short and long flags,
329
+ * separated by comma, a pipe or space. The following are all valid
330
+ * all will output this way when `--help` is used.
331
+ *
332
+ * "-p, --pepper"
333
+ * "-p|--pepper"
334
+ * "-p --pepper"
335
+ *
336
+ * Examples:
337
+ *
338
+ * // simple boolean defaulting to false
339
+ * program.option('-p, --pepper', 'add pepper');
340
+ *
341
+ * --pepper
342
+ * program.pepper
343
+ * // => Boolean
344
+ *
345
+ * // simple boolean defaulting to true
346
+ * program.option('-C, --no-cheese', 'remove cheese');
347
+ *
348
+ * program.cheese
349
+ * // => true
350
+ *
351
+ * --no-cheese
352
+ * program.cheese
353
+ * // => false
354
+ *
355
+ * // required argument
356
+ * program.option('-C, --chdir <path>', 'change the working directory');
357
+ *
358
+ * --chdir /tmp
359
+ * program.chdir
360
+ * // => "/tmp"
361
+ *
362
+ * // optional argument
363
+ * program.option('-c, --cheese [type]', 'add cheese [marble]');
364
+ *
365
+ * @param {String} flags
366
+ * @param {String} description
367
+ * @param {Function|*} [fn] or default
368
+ * @param {*} [defaultValue]
369
+ * @return {Command} for chaining
370
+ * @api public
371
+ */
372
+
373
+ Command.prototype.option = function(flags, description, fn, defaultValue) {
374
+ var self = this,
375
+ option = new Option(flags, description),
376
+ oname = option.name(),
377
+ name = option.attributeName();
378
+
379
+ // default as 3rd arg
380
+ if (typeof fn !== 'function') {
381
+ if (fn instanceof RegExp) {
382
+ var regex = fn;
383
+ fn = function(val, def) {
384
+ var m = regex.exec(val);
385
+ return m ? m[0] : def;
386
+ };
387
+ } else {
388
+ defaultValue = fn;
389
+ fn = null;
390
+ }
391
+ }
392
+
393
+ // preassign default value only for --no-*, [optional], or <required>
394
+ if (!option.bool || option.optional || option.required) {
395
+ // when --no-* we make sure default is true
396
+ if (!option.bool) defaultValue = true;
397
+ // preassign only if we have a default
398
+ if (defaultValue !== undefined) {
399
+ self[name] = defaultValue;
400
+ option.defaultValue = defaultValue;
401
+ }
402
+ }
403
+
404
+ // register the option
405
+ this.options.push(option);
406
+
407
+ // when it's passed assign the value
408
+ // and conditionally invoke the callback
409
+ this.on('option:' + oname, function(val) {
410
+ // coercion
411
+ if (val !== null && fn) {
412
+ val = fn(val, self[name] === undefined ? defaultValue : self[name]);
413
+ }
414
+
415
+ // unassigned or bool
416
+ if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') {
417
+ // if no value, bool true, and we have a default, then use it!
418
+ if (val == null) {
419
+ self[name] = option.bool
420
+ ? defaultValue || true
421
+ : false;
422
+ } else {
423
+ self[name] = val;
424
+ }
425
+ } else if (val !== null) {
426
+ // reassign
427
+ self[name] = val;
428
+ }
429
+ });
430
+
431
+ return this;
432
+ };
433
+
434
+ /**
435
+ * Allow unknown options on the command line.
436
+ *
437
+ * @param {Boolean} arg if `true` or omitted, no error will be thrown
438
+ * for unknown options.
439
+ * @api public
440
+ */
441
+ Command.prototype.allowUnknownOption = function(arg) {
442
+ this._allowUnknownOption = arguments.length === 0 || arg;
443
+ return this;
444
+ };
445
+
446
+ /**
447
+ * Parse `argv`, settings options and invoking commands when defined.
448
+ *
449
+ * @param {Array} argv
450
+ * @return {Command} for chaining
451
+ * @api public
452
+ */
453
+
454
+ Command.prototype.parse = function(argv) {
455
+ // implicit help
456
+ if (this.executables) this.addImplicitHelpCommand();
457
+
458
+ // store raw args
459
+ this.rawArgs = argv;
460
+
461
+ // guess name
462
+ this._name = this._name || basename(argv[1], '.js');
463
+
464
+ // github-style sub-commands with no sub-command
465
+ if (this.executables && argv.length < 3 && !this.defaultExecutable) {
466
+ // this user needs help
467
+ argv.push('--help');
468
+ }
469
+
470
+ // process argv
471
+ var parsed = this.parseOptions(this.normalize(argv.slice(2)));
472
+ var args = this.args = parsed.args;
473
+
474
+ var result = this.parseArgs(this.args, parsed.unknown);
475
+
476
+ // executable sub-commands
477
+ var name = result.args[0];
478
+
479
+ var aliasCommand = null;
480
+ // check alias of sub commands
481
+ if (name) {
482
+ aliasCommand = this.commands.filter(function(command) {
483
+ return command.alias() === name;
484
+ })[0];
485
+ }
486
+
487
+ if (this._execs[name] && typeof this._execs[name] !== 'function') {
488
+ return this.executeSubCommand(argv, args, parsed.unknown);
489
+ } else if (aliasCommand) {
490
+ // is alias of a subCommand
491
+ args[0] = aliasCommand._name;
492
+ return this.executeSubCommand(argv, args, parsed.unknown);
493
+ } else if (this.defaultExecutable) {
494
+ // use the default subcommand
495
+ args.unshift(this.defaultExecutable);
496
+ return this.executeSubCommand(argv, args, parsed.unknown);
497
+ }
498
+
499
+ return result;
500
+ };
501
+
502
+ /**
503
+ * Execute a sub-command executable.
504
+ *
505
+ * @param {Array} argv
506
+ * @param {Array} args
507
+ * @param {Array} unknown
508
+ * @api private
509
+ */
510
+
511
+ Command.prototype.executeSubCommand = function(argv, args, unknown) {
512
+ args = args.concat(unknown);
513
+
514
+ if (!args.length) this.help();
515
+ if (args[0] === 'help' && args.length === 1) this.help();
516
+
517
+ // <cmd> --help
518
+ if (args[0] === 'help') {
519
+ args[0] = args[1];
520
+ args[1] = '--help';
521
+ }
522
+
523
+ // executable
524
+ var f = argv[1];
525
+ // name of the subcommand, link `pm-install`
526
+ var bin = basename(f, '.js') + '-' + args[0];
527
+
528
+ // In case of globally installed, get the base dir where executable
529
+ // subcommand file should be located at
530
+ var baseDir,
531
+ link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f;
532
+
533
+ // when symbolink is relative path
534
+ if (link !== f && link.charAt(0) !== '/') {
535
+ link = path.join(dirname(f), link);
536
+ }
537
+ baseDir = dirname(link);
538
+
539
+ // prefer local `./<bin>` to bin in the $PATH
540
+ var localBin = path.join(baseDir, bin);
541
+
542
+ // whether bin file is a js script with explicit `.js` extension
543
+ var isExplicitJS = false;
544
+ if (exists(localBin + '.js')) {
545
+ bin = localBin + '.js';
546
+ isExplicitJS = true;
547
+ } else if (exists(localBin)) {
548
+ bin = localBin;
549
+ }
550
+
551
+ args = args.slice(1);
552
+
553
+ var proc;
554
+ if (process.platform !== 'win32') {
555
+ if (isExplicitJS) {
556
+ args.unshift(bin);
557
+ // add executable arguments to spawn
558
+ args = (process.execArgv || []).concat(args);
559
+
560
+ proc = spawn(process.argv[0], args, { stdio: 'inherit', customFds: [0, 1, 2] });
561
+ } else {
562
+ proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
563
+ }
564
+ } else {
565
+ args.unshift(bin);
566
+ proc = spawn(process.execPath, args, { stdio: 'inherit' });
567
+ }
568
+
569
+ var signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
570
+ signals.forEach(function(signal) {
571
+ process.on(signal, function() {
572
+ if (proc.killed === false && proc.exitCode === null) {
573
+ proc.kill(signal);
574
+ }
575
+ });
576
+ });
577
+ proc.on('close', process.exit.bind(process));
578
+ proc.on('error', function(err) {
579
+ if (err.code === 'ENOENT') {
580
+ console.error('\n %s(1) does not exist, try --help\n', bin);
581
+ } else if (err.code === 'EACCES') {
582
+ console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
583
+ }
584
+ process.exit(1);
585
+ });
586
+
587
+ // Store the reference to the child process
588
+ this.runningCommand = proc;
589
+ };
590
+
591
+ /**
592
+ * Normalize `args`, splitting joined short flags. For example
593
+ * the arg "-abc" is equivalent to "-a -b -c".
594
+ * This also normalizes equal sign and splits "--abc=def" into "--abc def".
595
+ *
596
+ * @param {Array} args
597
+ * @return {Array}
598
+ * @api private
599
+ */
600
+
601
+ Command.prototype.normalize = function(args) {
602
+ var ret = [],
603
+ arg,
604
+ lastOpt,
605
+ index;
606
+
607
+ for (var i = 0, len = args.length; i < len; ++i) {
608
+ arg = args[i];
609
+ if (i > 0) {
610
+ lastOpt = this.optionFor(args[i - 1]);
611
+ }
612
+
613
+ if (arg === '--') {
614
+ // Honor option terminator
615
+ ret = ret.concat(args.slice(i));
616
+ break;
617
+ } else if (lastOpt && lastOpt.required) {
618
+ ret.push(arg);
619
+ } else if (arg.length > 1 && arg[0] === '-' && arg[1] !== '-') {
620
+ arg.slice(1).split('').forEach(function(c) {
621
+ ret.push('-' + c);
622
+ });
623
+ } else if (/^--/.test(arg) && ~(index = arg.indexOf('='))) {
624
+ ret.push(arg.slice(0, index), arg.slice(index + 1));
625
+ } else {
626
+ ret.push(arg);
627
+ }
628
+ }
629
+
630
+ return ret;
631
+ };
632
+
633
+ /**
634
+ * Parse command `args`.
635
+ *
636
+ * When listener(s) are available those
637
+ * callbacks are invoked, otherwise the "*"
638
+ * event is emitted and those actions are invoked.
639
+ *
640
+ * @param {Array} args
641
+ * @return {Command} for chaining
642
+ * @api private
643
+ */
644
+
645
+ Command.prototype.parseArgs = function(args, unknown) {
646
+ var name;
647
+
648
+ if (args.length) {
649
+ name = args[0];
650
+ if (this.listeners('command:' + name).length) {
651
+ this.emit('command:' + args.shift(), args, unknown);
652
+ } else {
653
+ this.emit('command:*', args);
654
+ }
655
+ } else {
656
+ outputHelpIfNecessary(this, unknown);
657
+
658
+ // If there were no args and we have unknown options,
659
+ // then they are extraneous and we need to error.
660
+ if (unknown.length > 0) {
661
+ this.unknownOption(unknown[0]);
662
+ }
663
+ }
664
+
665
+ return this;
666
+ };
667
+
668
+ /**
669
+ * Return an option matching `arg` if any.
670
+ *
671
+ * @param {String} arg
672
+ * @return {Option}
673
+ * @api private
674
+ */
675
+
676
+ Command.prototype.optionFor = function(arg) {
677
+ for (var i = 0, len = this.options.length; i < len; ++i) {
678
+ if (this.options[i].is(arg)) {
679
+ return this.options[i];
680
+ }
681
+ }
682
+ };
683
+
684
+ /**
685
+ * Parse options from `argv` returning `argv`
686
+ * void of these options.
687
+ *
688
+ * @param {Array} argv
689
+ * @return {Array}
690
+ * @api public
691
+ */
692
+
693
+ Command.prototype.parseOptions = function(argv) {
694
+ var args = [],
695
+ len = argv.length,
696
+ literal,
697
+ option,
698
+ arg;
699
+
700
+ var unknownOptions = [];
701
+
702
+ // parse options
703
+ for (var i = 0; i < len; ++i) {
704
+ arg = argv[i];
705
+
706
+ // literal args after --
707
+ if (literal) {
708
+ args.push(arg);
709
+ continue;
710
+ }
711
+
712
+ if (arg === '--') {
713
+ literal = true;
714
+ continue;
715
+ }
716
+
717
+ // find matching Option
718
+ option = this.optionFor(arg);
719
+
720
+ // option is defined
721
+ if (option) {
722
+ // requires arg
723
+ if (option.required) {
724
+ arg = argv[++i];
725
+ if (arg == null) return this.optionMissingArgument(option);
726
+ this.emit('option:' + option.name(), arg);
727
+ // optional arg
728
+ } else if (option.optional) {
729
+ arg = argv[i + 1];
730
+ if (arg == null || (arg[0] === '-' && arg !== '-')) {
731
+ arg = null;
732
+ } else {
733
+ ++i;
734
+ }
735
+ this.emit('option:' + option.name(), arg);
736
+ // bool
737
+ } else {
738
+ this.emit('option:' + option.name());
739
+ }
740
+ continue;
741
+ }
742
+
743
+ // looks like an option
744
+ if (arg.length > 1 && arg[0] === '-') {
745
+ unknownOptions.push(arg);
746
+
747
+ // If the next argument looks like it might be
748
+ // an argument for this option, we pass it on.
749
+ // If it isn't, then it'll simply be ignored
750
+ if ((i + 1) < argv.length && argv[i + 1][0] !== '-') {
751
+ unknownOptions.push(argv[++i]);
752
+ }
753
+ continue;
754
+ }
755
+
756
+ // arg
757
+ args.push(arg);
758
+ }
759
+
760
+ return { args: args, unknown: unknownOptions };
761
+ };
762
+
763
+ /**
764
+ * Return an object containing options as key-value pairs
765
+ *
766
+ * @return {Object}
767
+ * @api public
768
+ */
769
+ Command.prototype.opts = function() {
770
+ var result = {},
771
+ len = this.options.length;
772
+
773
+ for (var i = 0; i < len; i++) {
774
+ var key = this.options[i].attributeName();
775
+ result[key] = key === this._versionOptionName ? this._version : this[key];
776
+ }
777
+ return result;
778
+ };
779
+
780
+ /**
781
+ * Argument `name` is missing.
782
+ *
783
+ * @param {String} name
784
+ * @api private
785
+ */
786
+
787
+ Command.prototype.missingArgument = function(name) {
788
+ console.error();
789
+ console.error(" error: missing required argument `%s'", name);
790
+ console.error();
791
+ process.exit(1);
792
+ };
793
+
794
+ /**
795
+ * `Option` is missing an argument, but received `flag` or nothing.
796
+ *
797
+ * @param {String} option
798
+ * @param {String} flag
799
+ * @api private
800
+ */
801
+
802
+ Command.prototype.optionMissingArgument = function(option, flag) {
803
+ console.error();
804
+ if (flag) {
805
+ console.error(" error: option `%s' argument missing, got `%s'", option.flags, flag);
806
+ } else {
807
+ console.error(" error: option `%s' argument missing", option.flags);
808
+ }
809
+ console.error();
810
+ process.exit(1);
811
+ };
812
+
813
+ /**
814
+ * Unknown option `flag`.
815
+ *
816
+ * @param {String} flag
817
+ * @api private
818
+ */
819
+
820
+ Command.prototype.unknownOption = function(flag) {
821
+ if (this._allowUnknownOption) return;
822
+ console.error();
823
+ console.error(" error: unknown option `%s'", flag);
824
+ console.error();
825
+ process.exit(1);
826
+ };
827
+
828
+ /**
829
+ * Variadic argument with `name` is not the last argument as required.
830
+ *
831
+ * @param {String} name
832
+ * @api private
833
+ */
834
+
835
+ Command.prototype.variadicArgNotLast = function(name) {
836
+ console.error();
837
+ console.error(" error: variadic arguments must be last `%s'", name);
838
+ console.error();
839
+ process.exit(1);
840
+ };
841
+
842
+ /**
843
+ * Set the program version to `str`.
844
+ *
845
+ * This method auto-registers the "-V, --version" flag
846
+ * which will print the version number when passed.
847
+ *
848
+ * @param {String} str
849
+ * @param {String} [flags]
850
+ * @return {Command} for chaining
851
+ * @api public
852
+ */
853
+
854
+ Command.prototype.version = function(str, flags) {
855
+ if (arguments.length === 0) return this._version;
856
+ this._version = str;
857
+ flags = flags || '-V, --version';
858
+ var versionOption = new Option(flags, 'output the version number');
859
+ this._versionOptionName = versionOption.long.substr(2) || 'version';
860
+ this.options.push(versionOption);
861
+ this.on('option:' + this._versionOptionName, function() {
862
+ process.stdout.write(str + '\n');
863
+ process.exit(0);
864
+ });
865
+ return this;
866
+ };
867
+
868
+ /**
869
+ * Set the description to `str`.
870
+ *
871
+ * @param {String} str
872
+ * @return {String|Command}
873
+ * @api public
874
+ */
875
+
876
+ Command.prototype.description = function(str) {
877
+ if (arguments.length === 0) return this._description;
878
+ this._description = str;
879
+ return this;
880
+ };
881
+
882
+ /**
883
+ * Set an alias for the command
884
+ *
885
+ * @param {String} alias
886
+ * @return {String|Command}
887
+ * @api public
888
+ */
889
+
890
+ Command.prototype.alias = function(alias) {
891
+ var command = this;
892
+ if (this.commands.length !== 0) {
893
+ command = this.commands[this.commands.length - 1];
894
+ }
895
+
896
+ if (arguments.length === 0) return command._alias;
897
+
898
+ if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
899
+
900
+ command._alias = alias;
901
+ return this;
902
+ };
903
+
904
+ /**
905
+ * Set / get the command usage `str`.
906
+ *
907
+ * @param {String} str
908
+ * @return {String|Command}
909
+ * @api public
910
+ */
911
+
912
+ Command.prototype.usage = function(str) {
913
+ var args = this._args.map(function(arg) {
914
+ return humanReadableArgName(arg);
915
+ });
916
+
917
+ var usage = '[options]' +
918
+ (this.commands.length ? ' [command]' : '') +
919
+ (this._args.length ? ' ' + args.join(' ') : '');
920
+
921
+ if (arguments.length === 0) return this._usage || usage;
922
+ this._usage = str;
923
+
924
+ return this;
925
+ };
926
+
927
+ /**
928
+ * Get or set the name of the command
929
+ *
930
+ * @param {String} str
931
+ * @return {String|Command}
932
+ * @api public
933
+ */
934
+
935
+ Command.prototype.name = function(str) {
936
+ if (arguments.length === 0) return this._name;
937
+ this._name = str;
938
+ return this;
939
+ };
940
+
941
+ /**
942
+ * Return the largest option length.
943
+ *
944
+ * @return {Number}
945
+ * @api private
946
+ */
947
+
948
+ Command.prototype.largestOptionLength = function() {
949
+ return this.options.reduce(function(max, option) {
950
+ return Math.max(max, option.flags.length);
951
+ }, 0);
952
+ };
953
+
954
+ /**
955
+ * Return help for options.
956
+ *
957
+ * @return {String}
958
+ * @api private
959
+ */
960
+
961
+ Command.prototype.optionHelp = function() {
962
+ var width = this.largestOptionLength();
963
+
964
+ // Append the help information
965
+ return this.options.map(function(option) {
966
+ return pad(option.flags, width) + ' ' + option.description +
967
+ ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : '');
968
+ }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
969
+ .join('\n');
970
+ };
971
+
972
+ /**
973
+ * Return command help documentation.
974
+ *
975
+ * @return {String}
976
+ * @api private
977
+ */
978
+
979
+ Command.prototype.commandHelp = function() {
980
+ if (!this.commands.length) return '';
981
+
982
+ var commands = this.commands.filter(function(cmd) {
983
+ return !cmd._noHelp;
984
+ }).map(function(cmd) {
985
+ var args = cmd._args.map(function(arg) {
986
+ return humanReadableArgName(arg);
987
+ }).join(' ');
988
+
989
+ return [
990
+ cmd._name +
991
+ (cmd._alias ? '|' + cmd._alias : '') +
992
+ (cmd.options.length ? ' [options]' : '') +
993
+ (args ? ' ' + args : ''),
994
+ cmd._description
995
+ ];
996
+ });
997
+
998
+ var width = commands.reduce(function(max, command) {
999
+ return Math.max(max, command[0].length);
1000
+ }, 0);
1001
+
1002
+ return [
1003
+ '',
1004
+ ' Commands:',
1005
+ '',
1006
+ commands.map(function(cmd) {
1007
+ var desc = cmd[1] ? ' ' + cmd[1] : '';
1008
+ return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
1009
+ }).join('\n').replace(/^/gm, ' '),
1010
+ ''
1011
+ ].join('\n');
1012
+ };
1013
+
1014
+ /**
1015
+ * Return program help documentation.
1016
+ *
1017
+ * @return {String}
1018
+ * @api private
1019
+ */
1020
+
1021
+ Command.prototype.helpInformation = function() {
1022
+ var desc = [];
1023
+ if (this._description) {
1024
+ desc = [
1025
+ ' ' + this._description,
1026
+ ''
1027
+ ];
1028
+ }
1029
+
1030
+ var cmdName = this._name;
1031
+ if (this._alias) {
1032
+ cmdName = cmdName + '|' + this._alias;
1033
+ }
1034
+ var usage = [
1035
+ '',
1036
+ ' Usage: ' + cmdName + ' ' + this.usage(),
1037
+ ''
1038
+ ];
1039
+
1040
+ var cmds = [];
1041
+ var commandHelp = this.commandHelp();
1042
+ if (commandHelp) cmds = [commandHelp];
1043
+
1044
+ var options = [
1045
+ '',
1046
+ ' Options:',
1047
+ '',
1048
+ '' + this.optionHelp().replace(/^/gm, ' '),
1049
+ ''
1050
+ ];
1051
+
1052
+ return usage
1053
+ .concat(desc)
1054
+ .concat(options)
1055
+ .concat(cmds)
1056
+ .join('\n');
1057
+ };
1058
+
1059
+ /**
1060
+ * Output help information for this command
1061
+ *
1062
+ * @api public
1063
+ */
1064
+
1065
+ Command.prototype.outputHelp = function(cb) {
1066
+ if (!cb) {
1067
+ cb = function(passthru) {
1068
+ return passthru;
1069
+ };
1070
+ }
1071
+ process.stdout.write(cb(this.helpInformation()));
1072
+ this.emit('--help');
1073
+ };
1074
+
1075
+ /**
1076
+ * Output help information and exit.
1077
+ *
1078
+ * @api public
1079
+ */
1080
+
1081
+ Command.prototype.help = function(cb) {
1082
+ this.outputHelp(cb);
1083
+ process.exit();
1084
+ };
1085
+
1086
+ /**
1087
+ * Camel-case the given `flag`
1088
+ *
1089
+ * @param {String} flag
1090
+ * @return {String}
1091
+ * @api private
1092
+ */
1093
+
1094
+ function camelcase(flag) {
1095
+ return flag.split('-').reduce(function(str, word) {
1096
+ return str + word[0].toUpperCase() + word.slice(1);
1097
+ });
1098
+ }
1099
+
1100
+ /**
1101
+ * Pad `str` to `width`.
1102
+ *
1103
+ * @param {String} str
1104
+ * @param {Number} width
1105
+ * @return {String}
1106
+ * @api private
1107
+ */
1108
+
1109
+ function pad(str, width) {
1110
+ var len = Math.max(0, width - str.length);
1111
+ return str + Array(len + 1).join(' ');
1112
+ }
1113
+
1114
+ /**
1115
+ * Output help information if necessary
1116
+ *
1117
+ * @param {Command} command to output help for
1118
+ * @param {Array} array of options to search for -h or --help
1119
+ * @api private
1120
+ */
1121
+
1122
+ function outputHelpIfNecessary(cmd, options) {
1123
+ options = options || [];
1124
+ for (var i = 0; i < options.length; i++) {
1125
+ if (options[i] === '--help' || options[i] === '-h') {
1126
+ cmd.outputHelp();
1127
+ process.exit(0);
1128
+ }
1129
+ }
1130
+ }
1131
+
1132
+ /**
1133
+ * Takes an argument an returns its human readable equivalent for help usage.
1134
+ *
1135
+ * @param {Object} arg
1136
+ * @return {String}
1137
+ * @api private
1138
+ */
1139
+
1140
+ function humanReadableArgName(arg) {
1141
+ var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
1142
+
1143
+ return arg.required
1144
+ ? '<' + nameOutput + '>'
1145
+ : '[' + nameOutput + ']';
1146
+ }
1147
+
1148
+ // for versions before node v0.8 when there weren't `fs.existsSync`
1149
+ function exists(file) {
1150
+ try {
1151
+ if (fs.statSync(file).isFile()) {
1152
+ return true;
1153
+ }
1154
+ } catch (e) {
1155
+ return false;
1156
+ }
1157
+ }