-
Notifications
You must be signed in to change notification settings - Fork 2
/
jakefile.js
254 lines (197 loc) · 7.2 KB
/
jakefile.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env node
/**
* module dependencies.
*/
var pkg = require('./package'),
path = require("path"),
fs = require('fs'),
compressor = require('node-minify'),
kido = require('kido/lib'),
spawn = require('child_process').spawn;
/**
* module varibles.
*/
var async = {async:true},
phantomjs = require('phantomjs'),
phantomjsPath = phantomjs.path,
phantomjsRunner = path.join(__dirname, 'tests', 'runner.js'),
jsDir = path.join(__dirname,"/lib/"),
fileOut = path.join(__dirname, 'dist', pkg.name + '.js'),
fileOutMin = path.join(__dirname, 'dist', pkg.name + '.min.js'),
fileOutForTests = path.join(__dirname, 'tests', 'js', 'kido.js'),
fileOutForSamples = path.join(__dirname, 'samples', 'js', 'kido.js'),
copyright = path.join(__dirname, 'tmp', 'copyright.txt'),
fileOutMinTmp = path.join(__dirname, 'tmp', 'kido.min.js'),
files = [
path.join(jsDir,"kido.js"),
path.join(jsDir,"kido.config.js"),
path.join(jsDir,"kido.email.js"),
path.join(jsDir,"kido.logging.js"),
path.join(jsDir,"kido.notifications.js"),
path.join(jsDir,"kido.pubsub.js"),
path.join(jsDir,"kido.queue.js"),
path.join(jsDir,"kido.security.js"),
path.join(jsDir,"kido.sms.js"),
path.join(jsDir,"kido.storage.js"),
path.join(jsDir,"kido.storage.indexes.js"),
path.join(jsDir,"kido.services.js"),
path.join(jsDir,"kido.datasources.js"),
path.join(jsDir,"kido.localstorage.js"),
path.join(jsDir,"kido.offline.js")
];
desc('clean');
task('clean', function () {
console.log('');
[copyright, fileOut, fileOutMin, fileOutMinTmp, fileOutForTests, fileOutForSamples].forEach(safeDelete);
console.log('');
console.log('Successfully cleaned files.');
});
desc('creates a temporary directory.');
directory('tmp');
desc('prepares the copyright for unify and minify');
task('copyright', ['tmp'], function () {
var header = "// KidoZen Javascript SDK v" + pkg.version + ".\n" +
"// Copyright (c) 2014 Kidozen, Inc. MIT Licensed" + "\n" +
"var KIDO_SDK_VERSION = '" + pkg.version + "';";
fs.writeFileSync(copyright, header, 'utf8');
});
desc('unify the SDK with the copyright label.');
task('unify', ['clean', 'copyright'], async, function () {
console.log('');
console.log('Unifying files to: ' + fileOut);
// Using Google Closure
new compressor.minify({
type: 'no-compress',
fileIn: [copyright].concat(files),
fileOut: fileOut,
callback: function(err){
if ( err ) {
console.error('unable to unify.');
console.error(err);
complete();
}
console.log('Copying file to tests folder.');
fs.createReadStream(fileOut)
.pipe(fs.createWriteStream(fileOutForTests));
console.log('Copying file to samples folder.');
fs.createReadStream(fileOut)
.pipe(fs.createWriteStream(fileOutForSamples));
//give a few seconds for the files to be copied.
setTimeout(complete, 2 * 1000);
}
});
});
desc('applies the gcc to the sdk files and outputs to a tmp folder.');
task('gcc', ['tmp'], async, function () {
console.log('');
console.log('building version ' + pkg.version + ' of ' + pkg.name);
// Using Google Closure
new compressor.minify({
type: 'gcc',
fileIn: files,
fileOut: fileOutMinTmp,
callback: function(err){
if (err) {
console.error('minify');
console.error(err);
}
complete();
}
});
});
desc('minifies the sdk and adds the copyright label.');
task('minify', ['clean', 'copyright', 'gcc'], async, function () {
new compressor.minify({
type: 'no-compress',
fileIn: [copyright, fileOutMinTmp],
fileOut: fileOutMin,
callback: function (err) {
if (err) {
console.error('unable to minify and add copyright.');
console.error(err);
}
complete();
}});
});
desc('cleans, unifies and minifies the sdk');
task('build', ['unify', 'minify']);
desc('runs the unit tests against an actual kidozen environment');
task('test', ['build'], async, function () {
test();
});
desc('runs the unit tests and outputs a report in xunit format');
task('report', async, function ( output ) {
if (!output) {
console.log('No output option defined, using "tests.xml".');
test('tests.xml');
} else {
console.log('Exporting tests report to ' + output);
test(output);
}
});
desc('unifies and minifies the files.');
task('default', ['build']);
/**
* helpers
*/
var safeDelete = function (file) {
if (fs.existsSync(file)) {
console.log('cleaning ' + file);
fs.unlinkSync(file);
}
};
var test = function ( output ) {
//first run the kido emulator.
emulate(function ( err ) {
if (err) {
console.error('An error occurred while trying to start emulator:');
console.error(err);
return complete();
}
console.log('Running tests');
//use spawn to preserve color on console.
var options = { stdio: 'inherit' },
url = 'http://localhost:3000' + (output ? '#xunit' : '');
console.log("Testing url: ", url);
var sp = spawn(phantomjsPath, [phantomjsRunner, url, output || ''], options);
sp.on('close', function ( code ) {
console.log('');
if (code !== 0)
console.error('Unable to run tests with phantomjs');
else
console.info('Tests ran Successfully');
process.exit();
});
});
};
var emulate = function ( cb ) {
var app = process.env.KIDO_JS_APP,
hosting = process.env.KIDO_JS_HOSTING,
user = process.env.KIDO_JS_USER,
pass = process.env.KIDO_JS_PASS;
if (!app || !hosting || !user || !pass) {
console.error('The following environment variables are required:');
console.error('KIDO_JS_APP - kidozen application.');
console.error('KIDO_JS_HOSTING - domain for your kidozen environment.');
console.error('KIDO_JS_USER - kidozen user name.');
console.error('KIDO_JS_PASS - kidozen user password.');
console.error('(ie: demo, mycompany.kidocloud.com, mycompany@kidozen.com, mysecret)');
console.error('Usage: jake test KIDO_JS_APP=demo KID...');
process.exit(1);
}
console.log('');
console.log('Starting emulator...');
var config = { hosting: hosting, user: user, pass: pass },
api = kido(config),
folder = path.join(__dirname, 'tests'),
credentials = {
username: user,
password: pass,
ip: {
name: "Kidozen",
activeEndpoint: "https://identity.kidozen.com/wrapv0.9",
protocol: "wrapv0.9"
}
};
api.emulate(credentials,app, folder, cb);
};