UNPKG

6.64 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd: Quick and Easy Web Development |
5 | |
6 | Copyright (c) 2017 M/Gateway Developments Ltd, |
7 | Redhill, Surrey UK. |
8 | All rights reserved. |
9 | |
10 | http://www.mgateway.com |
11 | Email: rtweed@mgateway.com |
12 | |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 10 October 2017
28
29*/
30
31var fs = require('fs-extra');
32var os = require('os');
33var https = require('https');
34var readline = require('readline');
35var child_process = require('child_process');
36
37const rl = readline.createInterface({
38 input: process.stdin,
39 output: process.stdout
40});
41
42function download(url, dest, cb) {
43 var file = fs.createWriteStream(dest);
44 var request = https.get(url, function(response) {
45 response.pipe(file);
46 file.on('finish', function() {
47 file.close(cb); // close() is async, call cb after close completes.
48 });
49 }).on('error', function(err) { // Handle errors
50 fs.unlink(dest); // Delete the file async. (But we don't check the result)
51 if (cb) cb(err.message);
52 });
53};
54
55function npmInstall(moduleName) {
56 child_process.execSync('npm install ' + moduleName, {stdio: 'inherit'});
57}
58
59function runBash(scriptName) {
60 child_process.execSync('sh ' + scriptName, {stdio: 'inherit'});
61}
62
63function ok() {
64 console.log('QEWD.js is ready to run');
65 console.log('You should now be able to start QEWD by typing: node qewd');
66}
67
68function startupFile(dir) {
69 rl.question('Which database/server setup are you using (cache | gtm | yottadb | redis | rpi): ', (db) => {
70 if (!db || db === '') {
71 console.log('You must specify a database!');
72 return startupFile(dir);
73 }
74
75 var fromPath;
76 var toPath = dir + '/qewd.js';
77
78 if (db === 'cache') {
79
80 var oPath = {
81 Linux: 'linux',
82 Windows_NT: 'win64',
83 Darwin: 'osx'
84 };
85 var fileNames = {
86 '4': '421',
87 '6': '610',
88 '7': '700',
89 '8': '800'
90 };
91 var nodeVer = process.version;
92 nodeVer = nodeVer.split('v')[1];
93 nodeVer = nodeVer.split('.')[0];
94 var platform = os.type();
95
96 var url = 'https://s3-eu-west-1.amazonaws.com/cache.node/build-140/' + oPath[platform] + '/cache' + fileNames[nodeVer] + '.node';
97
98 var dest = dir + '/node_modules/cache.node';
99
100 console.log('cache.node url: ' + url);
101 console.log('to be saved as ' + dest);
102
103 download(url, dest, function(err) {
104 if (!err) {
105 fromPath = dir + '/node_modules/qewd/example/qewd.js';
106 fs.copySync(fromPath, toPath);
107 console.log('Check the Cache configuration parameters in your startup file: ' + dir + '/qewd.js');
108 console.log('Then you can start up QEWD by typing: node qewd');
109 }
110 rl.close();
111 });
112 return;
113 }
114
115 if (db === 'redis') {
116 var platform = os.type();
117 if (platform === 'Linux') {
118 var prepareScript = dir + '/node_modules/qewd/installers/prepare.sh';
119 runBash(prepareScript);
120 }
121
122 npmInstall('tcp-netx');
123 npmInstall('ewd-redis-globals');
124 fromPath = dir + '/node_modules/qewd/example/qewd-redis.js';
125 fs.copySync(fromPath, toPath);
126 ok();
127 rl.close();
128 return;
129 }
130
131 if (db === 'rpi') {
132 npmInstall('tcp-netx');
133 npmInstall('ewd-redis-globals');
134 fromPath = dir + '/node_modules/qewd/example/qewd-rpi.js';
135 fs.copySync(fromPath, toPath);
136 ok();
137 rl.close();
138 return;
139 }
140
141 if (db === 'gtm' || db === 'yottadb') {
142
143 if (!fs.existsSync(dir + '/node_modules/nodem') && typeof process.env['gtm_dist'] !== 'undefined') {
144 // safe and appropriate to install NodeM
145 runBash(dir + '/node_modules/qewd/installers/install_nodem.sh ' + dir);
146 }
147
148 fromPath = dir + '/node_modules/qewd/example/qewd-gtm.js';
149 fs.copySync(fromPath, toPath);
150 ok();
151 rl.close();
152 return;
153 }
154
155 console.log('Invalid database! Try again');
156 startupFile(dir);
157 });
158}
159
160function run() {
161
162 var cwd = process.cwd();
163 if (cwd.indexOf('node_modules/') !== -1) process.chdir('../..');
164 cwd = process.cwd();
165
166 rl.question('QEWD Installation Directory: (' + process.cwd() + '): ', (dir) => {
167 if (!dir || dir === '') dir = process.cwd();
168
169 console.log('Setting up QEWD in directory ' + dir);
170
171 var wwwPath = dir + '/www';
172 var www_exists = fs.existsSync(wwwPath);
173 console.log('www exists: ' + www_exists);
174
175 if (!www_exists) {
176 fs.mkdirSync(wwwPath);
177 console.log('www directory created');
178 }
179
180 var monitorPath = wwwPath + '/qewd-monitor';
181 var path_exists = fs.existsSync(monitorPath);
182 console.log('www/qewd-monitor exists: ' + path_exists);
183
184 if (!path_exists) {
185 fs.mkdirSync(monitorPath);
186 console.log('www/qewd-monitor directory created');
187 }
188
189 var fromPath = dir + '/node_modules/qewd-monitor/www';
190
191 fs.copySync(fromPath, monitorPath);
192
193 startupFile(dir);
194 });
195}
196
197run();
198