added a proper cli module
Showing
4 changed files
with
119 additions
and
73 deletions
... | @@ -47,7 +47,8 @@ phantom.casperVersion = { | ... | @@ -47,7 +47,8 @@ phantom.casperVersion = { |
47 | } | 47 | } |
48 | }; | 48 | }; |
49 | 49 | ||
50 | // patching fs | 50 | // Patching fs |
51 | // TODO: watch for these methods being implemented in official fs module | ||
51 | var fs = (function(fs) { | 52 | var fs = (function(fs) { |
52 | if (!fs.hasOwnProperty('basename')) { | 53 | if (!fs.hasOwnProperty('basename')) { |
53 | fs.basename = function(path) { | 54 | fs.basename = function(path) { |
... | @@ -70,64 +71,9 @@ var fs = (function(fs) { | ... | @@ -70,64 +71,9 @@ var fs = (function(fs) { |
70 | // casper root path | 71 | // casper root path |
71 | phantom.casperPath = fs.absolute(phantom.libraryScript); | 72 | phantom.casperPath = fs.absolute(phantom.libraryScript); |
72 | 73 | ||
73 | // casper cli args | ||
74 | phantom.casperArgs = (function(cliArgs) { | ||
75 | var extract = { | ||
76 | args: [], | ||
77 | options: {}, | ||
78 | get: function(what) { | ||
79 | if (typeof what === "number") { | ||
80 | return this.args[what]; | ||
81 | } else if (typeof what === "string") { | ||
82 | return this.options[what]; | ||
83 | } else { | ||
84 | throw new Error("Unsupported cli arg getter " + typeof what); | ||
85 | } | ||
86 | } | ||
87 | }; | ||
88 | cliArgs.forEach(function(arg) { | ||
89 | if (arg.indexOf('--') === 0) { | ||
90 | // named option | ||
91 | var optionMatch = arg.match(/^--(.*)=(.*)/i); | ||
92 | if (optionMatch) { | ||
93 | extract.options[optionMatch[1]] = optionMatch[2]; | ||
94 | } else { | ||
95 | // flag | ||
96 | var flagMatch = arg.match(/^--(.*)/); | ||
97 | if (flagMatch) { | ||
98 | extract.options[flagMatch[1]] = true; | ||
99 | } | ||
100 | } | ||
101 | } else { | ||
102 | // positional arg | ||
103 | extract.args.push(arg); | ||
104 | } | ||
105 | }); | ||
106 | return extract; | ||
107 | })(phantom.args); | ||
108 | |||
109 | var sourceIds = {}; | 74 | var sourceIds = {}; |
110 | 75 | ||
111 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | 76 | // Patching require() |
112 | // TODO: remoive when phantomjs has js engine upgrade | ||
113 | if (!new Error().hasOwnProperty('stack')) { | ||
114 | Object.defineProperty(Error.prototype, 'stack', { | ||
115 | set: function(string) { | ||
116 | this._stack = string; | ||
117 | }, | ||
118 | get: function() { | ||
119 | if (this._stack) { | ||
120 | return this._stack; | ||
121 | } else if (this.fileName || this.sourceId) { | ||
122 | return this.toString() + '\nat ' + getErrorMessage(this); | ||
123 | } | ||
124 | return this.toString() + '\nat unknown'; | ||
125 | }, | ||
126 | configurable: true, | ||
127 | enumerable: true | ||
128 | }); | ||
129 | } | ||
130 | |||
131 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | 77 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ |
132 | // TODO: remove when PhantomJS has full module support | 78 | // TODO: remove when PhantomJS has full module support |
133 | require = (function(require, requireDir) { | 79 | require = (function(require, requireDir) { |
... | @@ -175,7 +121,7 @@ require = (function(require, requireDir) { | ... | @@ -175,7 +121,7 @@ require = (function(require, requireDir) { |
175 | } | 121 | } |
176 | } | 122 | } |
177 | if (!file) { | 123 | if (!file) { |
178 | throw new Error("Can't find module " + path); | 124 | throw new Error("CasperJS couldn't find module " + path); |
179 | } | 125 | } |
180 | if (file in requireCache) { | 126 | if (file in requireCache) { |
181 | return requireCache[file].exports; | 127 | return requireCache[file].exports; |
... | @@ -215,6 +161,31 @@ require = (function(require, requireDir) { | ... | @@ -215,6 +161,31 @@ require = (function(require, requireDir) { |
215 | }; | 161 | }; |
216 | })(require, phantom.casperPath); | 162 | })(require, phantom.casperPath); |
217 | 163 | ||
164 | // Adding stack traces to Error | ||
165 | // Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/ | ||
166 | // TODO: remove when phantomjs has js engine upgrade | ||
167 | if (!new Error().hasOwnProperty('stack')) { | ||
168 | Object.defineProperty(Error.prototype, 'stack', { | ||
169 | set: function(string) { | ||
170 | this._stack = string; | ||
171 | }, | ||
172 | get: function() { | ||
173 | if (this._stack) { | ||
174 | return this._stack; | ||
175 | } else if (this.fileName || this.sourceId) { | ||
176 | return this.toString() + '\nat ' + getErrorMessage(this); | ||
177 | } | ||
178 | return this.toString() + '\nat unknown'; | ||
179 | }, | ||
180 | configurable: true, | ||
181 | enumerable: true | ||
182 | }); | ||
183 | } | ||
184 | |||
185 | // casper cli args | ||
186 | phantom.casperArgs = require('cli').parse(phantom.args); | ||
187 | |||
188 | // loaded status | ||
218 | phantom.casperLoaded = true; | 189 | phantom.casperLoaded = true; |
219 | 190 | ||
220 | if (!!phantom.casperArgs.options.version) { | 191 | if (!!phantom.casperArgs.options.version) { |
... | @@ -227,7 +198,7 @@ if (!!phantom.casperArgs.options.version) { | ... | @@ -227,7 +198,7 @@ if (!!phantom.casperArgs.options.version) { |
227 | phantom.exit(0); | 198 | phantom.exit(0); |
228 | } | 199 | } |
229 | 200 | ||
230 | phantom.casperScript = phantom.casperArgs.args[0]; | 201 | phantom.casperScript = phantom.casperArgs.get(0); |
231 | 202 | ||
232 | if (!fs.isFile(phantom.casperScript)) { | 203 | if (!fs.isFile(phantom.casperScript)) { |
233 | console.log('Unable to open file: ' + phantom.casperScript); | 204 | console.log('Unable to open file: ' + phantom.casperScript); |
... | @@ -238,4 +209,7 @@ if (!fs.isFile(phantom.casperScript)) { | ... | @@ -238,4 +209,7 @@ if (!fs.isFile(phantom.casperScript)) { |
238 | phantom.casperArgs.args = phantom.casperArgs.args.filter(function(arg) { | 209 | phantom.casperArgs.args = phantom.casperArgs.args.filter(function(arg) { |
239 | return arg !== phantom.casperScript; | 210 | return arg !== phantom.casperScript; |
240 | }); | 211 | }); |
212 | |||
213 | // passed script execution | ||
214 | // TODO: proper syntax validation? | ||
241 | phantom.injectJs(phantom.casperScript); | 215 | phantom.injectJs(phantom.casperScript); | ... | ... |
modules/cli.js
0 → 100644
1 | /*! | ||
2 | * Casper is a navigation utility for PhantomJS. | ||
3 | * | ||
4 | * Documentation: http://n1k0.github.com/casperjs/ | ||
5 | * Repository: http://github.com/n1k0/casperjs | ||
6 | * | ||
7 | * Copyright (c) 2011 Nicolas Perriault | ||
8 | * | ||
9 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
10 | * copy of this software and associated documentation files (the "Software"), | ||
11 | * to deal in the Software without restriction, including without limitation | ||
12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
13 | * and/or sell copies of the Software, and to permit persons to whom the | ||
14 | * Software is furnished to do so, subject to the following conditions: | ||
15 | * | ||
16 | * The above copyright notice and this permission notice shall be included | ||
17 | * in all copies or substantial portions of the Software. | ||
18 | * | ||
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
25 | * DEALINGS IN THE SOFTWARE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | var utils = require('utils'); | ||
30 | |||
31 | /** | ||
32 | * Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated | ||
33 | * Object. | ||
34 | * | ||
35 | * @param array phantomArgs phantom.args value | ||
36 | * @return Object | ||
37 | */ | ||
38 | exports.parse = function(phantomArgs) { | ||
39 | if (!utils.isType(phantomArgs, "runtimearray")) { | ||
40 | throw new Error('parse() can only process a phantomjs arguments array'); | ||
41 | } | ||
42 | var extract = { | ||
43 | args: [], | ||
44 | options: {}, | ||
45 | get: function(what) { | ||
46 | if (typeof what === "number") { | ||
47 | return this.args[what]; | ||
48 | } else if (typeof what === "string") { | ||
49 | return this.options[what]; | ||
50 | } else { | ||
51 | throw new Error("Unsupported cli arg getter " + typeof what); | ||
52 | } | ||
53 | } | ||
54 | }; | ||
55 | phantomArgs.forEach(function(arg) { | ||
56 | if (arg.indexOf('--') === 0) { | ||
57 | // named option | ||
58 | var optionMatch = arg.match(/^--(.*)=(.*)/i); | ||
59 | if (optionMatch) { | ||
60 | extract.options[optionMatch[1]] = optionMatch[2]; | ||
61 | } else { | ||
62 | // flag | ||
63 | var flagMatch = arg.match(/^--(.*)/); | ||
64 | if (flagMatch) { | ||
65 | extract.options[flagMatch[1]] = true; | ||
66 | } | ||
67 | } | ||
68 | } else { | ||
69 | // positional arg | ||
70 | extract.args.push(arg); | ||
71 | } | ||
72 | }); | ||
73 | return extract; | ||
74 | }; |
... | @@ -22,16 +22,14 @@ var casper = require('casper').create({ | ... | @@ -22,16 +22,14 @@ var casper = require('casper').create({ |
22 | verbose: true | 22 | verbose: true |
23 | }); | 23 | }); |
24 | 24 | ||
25 | (function(casper) { | 25 | var tests = []; |
26 | var tests = []; | 26 | if (casper.cli.args.length) { |
27 | if (casper.cli.args.length) { | 27 | tests = casper.cli.args.filter(function(path) { |
28 | tests = casper.cli.args.filter(function(path) { | 28 | return fs.isFile(path) || fs.isDirectory(path); |
29 | return fs.isFile(path) || fs.isDirectory(path); | 29 | }); |
30 | }); | 30 | } |
31 | } | 31 | if (!tests.length) { |
32 | if (!tests.length) { | 32 | // default test suite is casperjs' one |
33 | // default test suite is casperjs' one | 33 | tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))]; |
34 | tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))]; | 34 | } |
35 | } | 35 | casper.test.runSuites.apply(casper.test, tests); |
36 | casper.test.runSuites.apply(casper.test, tests); | ||
37 | })(casper); | ... | ... |
-
Please register or sign in to post a comment