1 | var childProcess,
|
2 | settingConfig,
|
3 | settingFailed = false,
|
4 | connectionFailed = false,
|
5 | os = require("os");
|
6 |
|
7 | function _processIt(command, args, opt, callback) {
|
8 |
|
9 | var childProcess = require("child_process"),
|
10 | process;
|
11 |
|
12 | if (childProcess) {
|
13 | if (os.platform() === "win32") {
|
14 | args.unshift(command);
|
15 | args.unshift("/c");
|
16 | command = "cmd";
|
17 | }
|
18 | process = childProcess.spawn(command, args, opt);
|
19 |
|
20 | process.stderr.on('data', function (err) {
|
21 | console.log('child process exited with errors, ' + err);
|
22 | });
|
23 |
|
24 | process.on('close', function (code) {
|
25 | if (code !== 0) {
|
26 | console.log('child process exited with code ' + code);
|
27 | } else {
|
28 | if (callback) {
|
29 | callback.call(this);
|
30 | }
|
31 |
|
32 | }
|
33 | });
|
34 | }
|
35 | }
|
36 |
|
37 | function _testConnection(host, callback) {
|
38 | require('dns').resolve(host, function (err) {
|
39 | if (err) {
|
40 |
|
41 | connectionFailed = true;
|
42 | console.error("Test connection to: ", host ,"failed with errors. Check your cable and your proxy settings", err);
|
43 | }
|
44 | else {
|
45 |
|
46 |
|
47 | }
|
48 |
|
49 | if (callback) {
|
50 | callback.call(this, err);
|
51 | }
|
52 | });
|
53 | }
|
54 |
|
55 | function _isProxy() {
|
56 | return settingConfig.proxy.enable;
|
57 | }
|
58 |
|
59 | function _getProxyURL() {
|
60 | var host = settingConfig.proxy.base.host,
|
61 | port = settingConfig.proxy.base.port;
|
62 |
|
63 | return (_isProxy() ? ['"',"http://", host, ":", port, '"'].join("") : undefined);
|
64 | }
|
65 |
|
66 | settingConfig = require('fs').readFileSync("settings.json");
|
67 | if (settingConfig) {
|
68 | try {
|
69 | settingConfig = JSON.parse(settingConfig);
|
70 | } catch (e) {
|
71 | console.error("Failed to parse setting.json content. ", e);
|
72 | settingFailed = true;
|
73 | }
|
74 | }
|
75 |
|
76 | function _process() {
|
77 |
|
78 | _testConnection("www.google.com", function() {
|
79 | _testConnection("www.npmjs.org", function() {
|
80 |
|
81 | });
|
82 | });
|
83 |
|
84 |
|
85 | }
|
86 |
|
87 | if (!settingFailed) {
|
88 |
|
89 | if (_isProxy()) {
|
90 | _processIt("npm", ["config", "set", "proxy", _getProxyURL()], undefined, function() {
|
91 | _processIt("npm", ["config", "set", "https-proxy", _getProxyURL()], undefined, function() {
|
92 | _processIt("git", ["config", "http.proxy", _getProxyURL()], undefined, function() {
|
93 | _process();
|
94 | });
|
95 | });
|
96 | });
|
97 |
|
98 | } else {
|
99 | _processIt("npm", ["config", "delete", "proxy"], undefined, function() {
|
100 | _processIt("npm", ["config", "delete", "https-proxy"], undefined, function() {
|
101 | _processIt("git", ["config", "--unset", "http.proxy"], undefined, function() {
|
102 | _process();
|
103 | });
|
104 | });
|
105 | });
|
106 |
|
107 |
|
108 | }
|
109 | }
|