1 | module.exports = function (param, dir, cb) {
|
2 | var FlexHosts = require("./api");
|
3 | var readLine = require("readline");
|
4 | var pathLib = require("path");
|
5 | var fsLib = require("fs");
|
6 | var mkdirp = require("mkdirp");
|
7 |
|
8 | param = param || {};
|
9 |
|
10 | if (typeof dir == "function") {
|
11 | cb = dir;
|
12 | dir = null;
|
13 | }
|
14 | else if (typeof cb != "function") {
|
15 | cb = function (err, host2ip) {
|
16 | if (err) {
|
17 | console.log(err);
|
18 | }
|
19 | else {
|
20 | console.log(host2ip);
|
21 | }
|
22 | };
|
23 | }
|
24 |
|
25 | var confDir, confFile, json = pathLib.basename(__dirname) + ".json";
|
26 | if (dir) {
|
27 | if (dir.indexOf('/') == 0 || /^\w{1}:[\\/].*$/.test(dir)) {
|
28 | if (/\.json$/.test(dir)) {
|
29 | confFile = dir;
|
30 | confDir = pathLib.dirname(confFile);
|
31 | }
|
32 | else {
|
33 | confDir = dir;
|
34 | confFile = pathLib.join(confDir, json);
|
35 | }
|
36 | }
|
37 | else {
|
38 | confDir = pathLib.join(process.cwd(), dir);
|
39 | confFile = pathLib.join(confDir, json);
|
40 | }
|
41 |
|
42 | if (!fsLib.existsSync(confDir)) {
|
43 | mkdirp.sync(confDir);
|
44 | fsLib.chmod(confDir, 0777);
|
45 | }
|
46 | }
|
47 | else {
|
48 | confFile = null;
|
49 | }
|
50 |
|
51 | var flexHosts = new FlexHosts(param, confFile, cb);
|
52 |
|
53 | var rl = readLine.createInterface({
|
54 | input: process.stdin,
|
55 | output: process.stdout
|
56 | });
|
57 | rl.on("SIGINT", function () {
|
58 | process.emit("SIGINT");
|
59 | });
|
60 |
|
61 | process.on("SIGINT", (function (i) {
|
62 | return function() {
|
63 | switch (i) {
|
64 | case 0:
|
65 | console.log("\n\x1b[35m%s\x1b[0m", "Press Control-C again to exit.");
|
66 | i++;
|
67 | break;
|
68 | case 1:
|
69 | console.log("Exiting...");
|
70 | flexHosts.restore();
|
71 | i++;
|
72 | break;
|
73 | default:
|
74 | console.log("Waiting...");
|
75 | }
|
76 | }
|
77 | })(0));
|
78 |
|
79 | return flexHosts;
|
80 | };
|