forked from sitepoint-editors/node-phantom-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
153 lines (130 loc) · 3.96 KB
/
run.js
File metadata and controls
153 lines (130 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
/**
* Package dependencies
*/
var Horseman = require('node-horseman')
, validUrl = require('valid-url')
, fs = require('fs')
, prompt = require('prompt')
, program = require('commander');
program
.version('1.0.0')
.option('-x --action-to-perform [string]', 'The type of action to perform.')
.option('-u --url [string]', 'Optional url used by certain actions')
.parse(process.argv);
/**
* Path to the PhantomJS binary
*/
var PATH_TO_PHANTOM = '/usr/local/bin/phantomjs';
/**
* Stores an array of actions support by this utility framework.
* Populated on script load based on files present in the 'actions' directory
*/
var supportedActions = [];
/**
* Loads a Horseman instance to facilitate interaction with PhantomJS
*/
var loadPhantomInstance = function () {
var options = {
phantomPath: PATH_TO_PHANTOM,
loadImages: true,
injectJquery: true,
webSecurity: true,
ignoreSSLErrors: true
};
var phantomInstance = new Horseman(options);
phantomInstance.on('consoleMessage', function (msg) {
// console.log('Phantom page log: ', msg);
});
phantomInstance.on('error', function (msg) {
// console.log('Phantom page error: ', msg);
});
return phantomInstance;
};
/**
* Triggers execution of the appropriate action
*/
var main = function () {
if (!program.actionToPerform) {
throw 'An action must be specified. Supported actions include: ', supportedActions.join(', ');
} else if (supportedActions.indexOf(program.actionToPerform) < 0) {
throw 'Invalid action specified. Supported actions include: ', supportedActions.join(', ');
} else {
console.log('Performing action: ', program.actionToPerform);
}
var performAction = require('./actions/' + program.actionToPerform)
, phantomInstance = loadPhantomInstance();
prompt.start();
prompt.override = program;
switch (program.actionToPerform) {
case 'create_repo':
prompt.get([{
name: 'repository',
description: 'Enter repository name',
required: true
}, {
name: 'username',
description: 'Enter GitHub username',
required: true
}, {
name: 'password',
description: 'Enter GitHub password',
hidden: true,
required: true
}], function (err, result) {
performAction(phantomInstance, result.username, result.password, result.repository);
});
break;
case 'take_screenshot':
prompt.get([{
name: 'url',
description: 'Enter URL to take screenshot of',
required: true,
conform: function (value) {
return validUrl.isWebUri(value);
}
}], function (err, result) {
performAction(phantomInstance, result.url);
});
break;
case 'get_links':
prompt.get([{
name: 'url',
description: 'Enter URL to gather links from',
required: true,
conform: function (value) {
return validUrl.isWebUri(value);
}
}], function (err, result) {
performAction(phantomInstance, result.url);
});
break;
case 'hello_world':
prompt.get([{
name: 'url',
description: 'Enter a URL',
required: true,
conform: function (value) {
return validUrl.isWebUri(value);
}
}], function (err, result) {
performAction(phantomInstance, result.url);
});
break;
default:
phantomInstance.close();
throw 'Invalid action specified. Supported actions include: ', supportedActions.join(', ');
}
};
/**
* Run immediately on script load to determine available actions and attempt to run the specified action
*/
(function () {
// Generate an array of supported actions based on the files present in the 'actions' directory
fs.readdir('./actions', function (err, files) {
files.forEach(function (filename) {
supportedActions.push(filename.split('.')[0]);
});
main();
});
})();