scratch – Blame information for rev 73

Subversion Repositories:
Rev:
Rev Author Line No. Line
73 office 1 /*
2 * Copyright (c) 2014 gskinner.com, inc.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25  
26 module.exports = function (grunt) {
27 var net = require('net');
28 var _callback;
29 var _ports;
30 var _opts;
31 var _done;
32  
33 grunt.registerMultiTask('findopenport', 'Prints a list of active ips.', function() {
34 _opts = this.options();
35  
36 _done = this.async();
37 _ports = _opts['ports'] || [80, 8888, 9000, 9999, 9001];
38 checkNext();
39 });
40  
41 function checkNext() {
42 if (!_ports.length) {
43 grunt.option(_portName, -1);
44 _done();
45 return;
46 }
47  
48 check(_ports.shift(), function(success, port) {
49 if (!success) {
50 checkNext();
51 } else {
52 //grunt.option(_portName, port);
53 var configNames = Array.isArray(_opts.configName)?_opts.configName:[_opts.configName];
54  
55 configNames.forEach(function(item) {
56 grunt.config.set(item, port);
57 });
58 _done();
59 }
60 });
61 }
62  
63 function check(port, callback) {
64 var server = net.createServer();
65 server.on('error', function(e) {
66 callback(false, port);
67 });
68  
69 server.listen(port, function() {
70 callback(true, port);
71 server.close();
72 });
73 }
74 };