This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGruntfile.js
126 lines (117 loc) · 3.68 KB
/
Gruntfile.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
module.exports = function(grunt) {
function countLamps(jsonObj) {
var i = 0;
for(var prop in jsonObj) {
i++;
}
return i;
}
// Project config
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
separator: ';'
},
dist: {
src: ['src/**/*.js'],
dest: 'dist/<%= pkg.name %>.concat.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %>.js <%= grunt.template.today("yyyy-mm-dd hh:mm:ss") %>, Copyright 2013 Bryan Johnson, Licensed MIT */'
},
dist: {
files: {
'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
}
}
},
qunit: {
files: ['test/**/*.html']
},
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
ignores: ['**/*.preprocessed.js'],
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'qunit']
},
preprocess: {
js: {
src: 'test/hue-test.preprocessed.js',
dest: 'test/hue-test.js',
options: {
context: {
IPAddress: grunt.file.isFile('.discovered-ip') ? grunt.file.readJSON('.discovered-ip').internalipaddress : "",
APIKey: grunt.file.isFile('.api-key') ? grunt.file.readJSON('.api-key').apiKey : "",
connectedLampCount: grunt.file.isFile('.lights') ? countLamps(grunt.file.readJSON('.lights')) : 3
}
}
}
}
});
grunt.loadNpmTasks('grunt-preprocess');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
// test-only task
grunt.registerTask('test', ['preprocess', 'jshint', 'qunit']);
// test, concat and uglify for distribution
grunt.registerTask('default', ['preprocess', 'jshint', 'qunit', 'concat', 'uglify']);
grunt.registerTask('setAPIKey', 'Set API key for test run', function(apiKey) {
grunt.log.writeln("Saved api key: " + apiKey);
grunt.file.write('.api-key', JSON.stringify({ "apiKey": apiKey }));
});
grunt.registerTask('getBridgeIP', 'Get connected Hue bridge IP via upnp', function() {
var done = this.async();
var needle = require('needle');
needle.get('http://www.meethue.com/api/nupnp', function(error, response, body) {
var responseJson = JSON.parse(body);
var ip = responseJson[0].internalipaddress;
grunt.log.writeln("Found IP: " + ip);
grunt.config.set('config.bridgeIP', ip);
grunt.file.write('.discovered-ip', JSON.stringify(responseJson[0]));
done();
});
});
grunt.registerTask('discoverLampCount', 'Set number of lamps from a query of the Hue API', function() {
var ip = grunt.file.isFile('.discovered-ip') ? grunt.file.readJSON('.discovered-ip').internalipaddress : "",
apiKey = grunt.file.isFile('.api-key') ? grunt.file.readJSON('.api-key').apiKey : "",
url = 'http://' + ip + '/api/' + apiKey + '/lights',
done = this.async();
grunt.log.writeln('URL: ' + url);
var needle = require('needle');
var jsonObj = null;
needle.get(url, function(error, response, body) {
jsonObj = JSON.parse(body);
grunt.log.writeln("Found " + countLamps(jsonObj) + " connected lamps");
grunt.file.write('.lights', JSON.stringify(jsonObj));
done();
});
});
// init library
grunt.registerTask('init', 'Initialize library with api key for tests', function(apiKey) {
grunt.log.writeln('Initializing with api key: ' + apiKey);
grunt.config.set('config.apiKey', apiKey);
var queuedTasks = [
'setAPIKey:' + apiKey,
'getBridgeIP',
'discoverLampCount'
];
grunt.log.writeln("Queued tasks: " + queuedTasks);
grunt.task.run(queuedTasks);
});
};