UNPKG

6.73 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var http = require('http'),
4 localtunnel = require('localtunnel'),
5 parseArgs = require('minimist'),
6 shell = require('shelljs'),
7 fs = require('fs'),
8 path = require('path');
9
10var tunneledUrl = "";
11var PORT = 8008;
12var USAGE = "Error missing args. \n Usage: $cordova-paramedic --platform CORDOVA-PLATFORM --plugin PLUGIN-PATH";
13var TEMP_PROJECT_PATH = "tmp";
14var storedCWD = process.cwd();
15var TIMEOUT = 10 * 60 * 1000; // 10 minutes in msec - this will become a param
16
17var plugin,platformId;
18
19run();
20
21// main program here
22function run() {
23 init();
24 createTempProject();
25 installPlugins();
26 startServer();
27}
28
29function init() {
30 var argv = parseArgs(process.argv.slice(2),{
31 plugin:".",
32 platform:""
33 });
34
35 if(!argv.platform || !argv.plugin) {
36 console.log(USAGE);
37 process.exit(1);
38 }
39
40 platformId = argv.platform;
41 plugin = argv.plugin;
42
43 var cordovaResult = shell.exec('cordova --version', {silent:true});
44 if(cordovaResult.code) {
45 console.error(cordovaResult.output);
46 process.exit(cordovaResult.code);
47 }
48}
49
50function createTempProject() {
51 console.log("cordova-paramedic :: creating temp project");
52 shell.exec('cordova create ' + TEMP_PROJECT_PATH);
53 shell.cd(TEMP_PROJECT_PATH);
54}
55
56function installPlugins() {
57 //console.log("cordova-paramedic :: installing " + plugin);
58
59 var installExitCode = shell.exec('cordova plugin add ' + plugin).code;
60 if(installExitCode != 0) {
61 console.error('Failed to install plugin : ' + plugin);
62 cleanUpAndExitWithCode(1);
63 return;
64 }
65
66 //console.log("cordova-paramedic :: installing " + path.join(plugin,'tests'));
67 installExitCode = shell.exec('cordova plugin add ' + path.join(plugin,'tests')).code;
68 if(installExitCode != 0) {
69 console.error('Failed to find /tests/ for plugin : ' + plugin);
70 cleanUpAndExitWithCode(1);
71 return;
72 }
73
74 //console.log("cordova-paramedic :: installing plugin-test-framework");
75 installExitCode = shell.exec('cordova plugin add https://github.com/apache/cordova-plugin-test-framework').code;
76 if(installExitCode != 0) {
77 console.error('cordova-plugin-test-framework');
78 cleanUpAndExitWithCode(1);
79 return;
80 }
81
82}
83
84function addAndRunPlatform() {
85 setConfigStartPage();
86 //console.log("cordova-paramedic :: adding platform and running");
87 shell.exec('cordova platform add ' + platformId);
88 shell.exec('cordova prepare');
89 // limit runtime to 5 minutes
90 setTimeout(function(){
91 console.error("This test seems to be block :: 5 minute timeout. Exiting ...");
92 cleanUpAndExitWithCode(1);
93 },(TIMEOUT));
94
95 shell.exec('cordova emulate ' + platformId.split("@")[0] + " --phone",
96 {async:true},
97 function(code,output){
98 if(code != 0) {
99 console.error("Error: cordova emulate return error code " + code);
100 console.log("output: " + output);
101 cleanUpAndExitWithCode(1);
102 }
103 }
104 );
105}
106
107function cleanUpAndExitWithCode(exitCode) {
108 shell.cd(storedCWD);
109 process.exit(exitCode);
110}
111
112function writeMedicLogUrl(url) {
113 console.log("cordova-paramedic :: writing medic log url to project");
114 var obj = {logurl:url};
115 fs.writeFileSync(path.join("www","medic.json"),JSON.stringify(obj));
116}
117
118
119function setConfigStartPage() {
120
121 console.log("cordova-paramedic :: setting app start page to test page");
122
123 var fileName = 'config.xml';
124 var configStr = fs.readFileSync(fileName).toString();
125 if(configStr) {
126 configStr = configStr.replace("src=\"index.html\"","src=\"cdvtests/index.html\"");
127 fs.writeFileSync(fileName, configStr);
128 }
129 else {
130 console.error("Oops, could not find config.xml");
131 }
132}
133
134function startServer() {
135 console.log("cordova-paramedic :: starting local medic server " + platformId);
136 var server = http.createServer(requestListener);
137 server.listen(PORT, '127.0.0.1',function onServerConnect() {
138
139 if(platformId == "ios") {
140 console.log('platform is ios');
141 writeMedicLogUrl("http://127.0.0.1:" + PORT);
142 addAndRunPlatform();
143 }
144 else if(platformId == "android") {
145 console.log('platform is android');
146 writeMedicLogUrl("http://127.0.0.1:" + PORT);
147 addAndRunPlatform();
148 }
149 else {
150 //localtunnel(PORT, tunnelCallback); // TODO
151 console.log("Only ios is currently supported");
152 cleanUpAndExitWithCode(1);
153 }
154 });
155}
156
157function requestListener(request, response) {
158 if (request.method == 'PUT' || request.method == 'POST') {
159 var body = '';
160 request.on('data', function (data) {
161 body += data;
162 // Too much POST data, kill the connection!
163 if (body.length > 1e6) {
164 req.connection.destroy();
165 }
166 });
167 request.on('end', function (res) {
168 if(body.indexOf("mobilespec") == 2){ // {\"mobilespec\":{...}}
169 try {
170 console.log("body = " + body);
171 var results = JSON.parse(body);
172 console.log("Results:: ran " +
173 results.mobilespec.specs +
174 " specs with " +
175 results.mobilespec.failures +
176 " failures");
177 if(results.mobilespec.failures > 0) {
178 cleanUpAndExitWithCode(1);
179 }
180 else {
181 cleanUpAndExitWithCode(0);
182 }
183
184 }
185 catch(err) {
186 console.log("parse error :: " + err);
187 cleanUpAndExitWithCode(1);
188 }
189 }
190 else {
191 console.log("console-log:" + body);
192 }
193 });
194 }
195 else {
196 console.log(request.method);
197 response.writeHead(200, { 'Content-Type': 'text/plain'});
198 response.write("Hello"); // sanity check to make sure server is running
199 response.end();
200 }
201}
202
203function tunnelCallback(err, tunnel) {
204 if (err){
205 console.log("failed to create tunnel url, check your internet connectivity.")
206 cleanUpAndExitWithCode(1);
207 }
208 else {
209 // the assigned public url for your tunnel
210 // i.e. https://abcdefgjhij.localtunnel.me
211 tunneledUrl = tunnel.url;
212 console.log("cordova-paramedic :: tunneledURL = " + tunneledUrl);
213 writeMedicLogUrl(tunneledUrl);
214 addAndRunPlatform();
215 }
216}
217