1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var readline = require('readline');
|
15 | var os = require('os');
|
16 | var fs = require('fs');
|
17 |
|
18 | var copyFilesInDirectory = function(oldPath, newPath) {
|
19 | console.log("Déplacer les fichiers dans " + oldPath + " vers " + newPath);
|
20 | var files = fs.readdirSync(oldPath);
|
21 |
|
22 | var file;
|
23 | var stats;
|
24 | var oldFilePath;
|
25 | var newFilePath;
|
26 | var error = false;
|
27 | for (var i = 0; i < files.length; i++) {
|
28 | file = files[i];
|
29 |
|
30 | stats = fs.lstatSync(oldPath + '/' + file);
|
31 | if (stats.isFile()) {
|
32 | oldFilePath = oldPath + '/' + file;
|
33 | newFilePath = newPath + '/' + file;
|
34 | try {
|
35 | fs.renameSync(oldFilePath, newFilePath);
|
36 | }
|
37 | catch(err) {
|
38 | error = true;
|
39 | console.log('Impossible de déplacer ' + oldPath + ' - vérifier les droits');
|
40 | console.log('Erreur : ' + err);
|
41 | }
|
42 | }
|
43 | }
|
44 | return error
|
45 | };
|
46 |
|
47 |
|
48 | var moveDirectory = function(oldPath, newPath) {
|
49 | console.log("Déplacer le répertoire " + oldPath + " vers " + newPath);
|
50 | var error = false;
|
51 | if (fs.existsSync(oldPath)) {
|
52 | if (!fs.existsSync(newPath)) {
|
53 | fs.mkdirSync(newPath);
|
54 | }
|
55 |
|
56 |
|
57 | var files = fs.readdirSync(oldPath);
|
58 | var file;
|
59 | var stats;
|
60 | if (files) for (var i = 0; i < files.length; i++) {
|
61 | file = files[i];
|
62 | stats = fs.lstatSync(oldPath + '/' + file);
|
63 | if (stats.isDirectory()) {
|
64 | oldSubDirectoryPath = oldPath + '/' + file;
|
65 | newSubDirectoryPath = newPath + '/' + file;
|
66 | error = moveDirectory(oldSubDirectoryPath, newSubDirectoryPath);
|
67 | if (error) break;
|
68 | }
|
69 | }
|
70 |
|
71 | if (error) return error;
|
72 |
|
73 | error = copyFilesInDirectory(oldPath, newPath);
|
74 | if (error) {
|
75 | return error;
|
76 | }
|
77 | else {
|
78 | fs.rmdirSync(oldPath);
|
79 | }
|
80 | }
|
81 | else {
|
82 | console.log('Mise en garde : ' + oldPath + ' le module Yexpert-JS n\'existe plus dans le répertoire');
|
83 | }
|
84 | return error;
|
85 |
|
86 | };
|
87 |
|
88 | var deleteDirectory = function(path) {
|
89 | var files = [];
|
90 | if( fs.existsSync(path) ) {
|
91 | files = fs.readdirSync(path);
|
92 | files.forEach(function(file,index){
|
93 | var curPath = path + "/" + file;
|
94 | if(fs.lstatSync(curPath).isDirectory()) {
|
95 | deleteDirectory(curPath);
|
96 | }
|
97 | else {
|
98 | fs.unlinkSync(curPath);
|
99 | }
|
100 | });
|
101 | fs.rmdirSync(path);
|
102 | }
|
103 | };
|
104 |
|
105 | var tidyUp = function(path) {
|
106 | var postinstallPath = path + '/node_modules/yexpert-js/postinstall';
|
107 | fs.unlinkSync(postinstallPath + '/install.js');
|
108 | process.chdir(path);
|
109 | fs.rmdirSync(postinstallPath);
|
110 | };
|
111 |
|
112 | var installEWD = function(path) {
|
113 | var installErrors = false;
|
114 | console.log('installer Yexpert-JS vers ' + path);
|
115 |
|
116 |
|
117 | var postinstallPath = path + '/node_modules/yexpert-js/postinstall';
|
118 |
|
119 |
|
120 |
|
121 | var oldPath = postinstallPath + '/www';
|
122 | var newPath = path + '/www';
|
123 | installErrors = moveDirectory(oldPath, newPath);
|
124 | if (installErrors) {
|
125 | console.log('Installation interrompue');
|
126 | return;
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 | newPath = path + '/ssl';
|
132 | if (!fs.existsSync(newPath)) fs.mkdirSync(newPath);
|
133 |
|
134 | oldPath = postinstallPath + '/node_modules';
|
135 | newPath = path + '/node_modules';
|
136 | installErrors = moveDirectory(oldPath, newPath);
|
137 |
|
138 |
|
139 |
|
140 | if (installErrors) {
|
141 | console.log('Installation interrompue');
|
142 | return;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 | console.log('Yexpert-JS a été installé et configuré avec succès');
|
148 |
|
149 | };
|
150 |
|
151 | var installExtras = function(path) {
|
152 | var installErrors = false;
|
153 | console.log('Installer des ressources supplémentaires à Yexpert-JS ' + path);
|
154 |
|
155 | var extrasPath = path + '/node_modules/yexpert-js/extras';
|
156 |
|
157 | var oldPath = extrasPath + '/node_modules';
|
158 | var newPath = path + '/node_modules';
|
159 | var installErrors = copyFilesInDirectory(oldPath, newPath);
|
160 |
|
161 | if (installErrors) {
|
162 | console.log('Installation interrompue');
|
163 | return;
|
164 | }
|
165 | fs.rmdirSync(oldPath);
|
166 |
|
167 | oldPath = extrasPath + '/ssl';
|
168 | newPath = path + '/ssl';
|
169 | installErrors = copyFilesInDirectory(oldPath, newPath);
|
170 |
|
171 | if (installErrors) {
|
172 | console.log('Installation interrompue');
|
173 | return;
|
174 | }
|
175 | fs.rmdirSync(oldPath);
|
176 |
|
177 | oldPath = extrasPath + '/startupExamples';
|
178 | newPath = path ;
|
179 | installErrors = copyFilesInDirectory(oldPath, newPath);
|
180 |
|
181 | if (installErrors) {
|
182 | console.log('Installation interrompue');
|
183 | return;
|
184 | }
|
185 | fs.rmdirSync(oldPath);
|
186 |
|
187 | oldPath = extrasPath + '/www';
|
188 | newPath = path + '/www';
|
189 | installErrors = moveDirectory(oldPath, newPath);
|
190 |
|
191 | if (installErrors) {
|
192 | console.log('Installation interrompue');
|
193 | return;
|
194 | }
|
195 | deleteDirectory(oldPath);
|
196 |
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | var installPath;
|
217 | var params = {
|
218 | silent: false
|
219 | };
|
220 | var paramsFile = '../../yexpert-jsSilent.js';
|
221 | if( fs.existsSync(paramsFile) ) {
|
222 | try {
|
223 | params = JSON.parse(fs.readFileSync(paramsFile, 'utf8'));
|
224 | fs.unlinkSync(paramsFile);
|
225 | }
|
226 | catch(err) {
|
227 |
|
228 | }
|
229 | }
|
230 |
|
231 | if (params.silent) {
|
232 | if (!params.installPath) {
|
233 | process.chdir('../..');
|
234 | installPath = process.cwd();
|
235 | }
|
236 | else {
|
237 | installPath = params.installPath;
|
238 | }
|
239 | installEWD(installPath);
|
240 | if (params.extras) {
|
241 | installExtras(installPath);
|
242 | }
|
243 | else {
|
244 | var extrasPath = installPath + '/node_modules/yexpert-js/extras';
|
245 | deleteDirectory(extrasPath);
|
246 | }
|
247 | tidyUp(installPath);
|
248 | process.chdir(installPath);
|
249 | return;
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 | var interface = readline.createInterface({
|
255 | input: process.stdin,
|
256 | output: process.stdout
|
257 | });
|
258 |
|
259 | process.chdir('../..');
|
260 | console.log(' ');
|
261 |
|
262 | interface.question('Installer Yexpert-JS vers le répertoire (' + process.cwd() + '): ', function(installPath) {
|
263 | if (installPath === '' || installPath === 'Y' || installPath === 'y') installPath = process.cwd();
|
264 | if (installPath.slice(-1) === '/') installPath = installPath.slice(0,-1);
|
265 | installEWD(installPath);
|
266 | console.log(' ');
|
267 | console.log('Voulez-vous installer des ressources supplémentaires à partir du répertoire /extras ?');
|
268 | console.log("Si vous êtes nouveau sur Yexpert-JS ou voulez créer un environnement de test, entrer Y");
|
269 | console.log("Si vous êtes un utilisateur expérimenté ou ceci est un environnement de production, entrer N");
|
270 | interface.question("Entrer Y/N: ", function(answer) {
|
271 | if (answer === 'Y' || answer === 'y') {
|
272 | installExtras(installPath);
|
273 | }
|
274 | else {
|
275 | var extrasPath = installPath + '/node_modules/yexpert-js/extras';
|
276 | deleteDirectory(extrasPath);
|
277 | }
|
278 | tidyUp(installPath);
|
279 | interface.close();
|
280 | process.chdir(installPath);
|
281 | });
|
282 | });
|
283 |
|
284 |
|