UNPKG

6.41 kBJavaScriptView Raw
1/*
2
3 This file is a part of node-on-train project.
4
5 Copyright (C) Thanh D. Dang <thanhdd.it@gmail.com>
6
7 node-on-train is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 node-on-train is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22
23var fs = require('fs');
24var path = require('path');
25var colors = require('colors');
26var diff = require('diff');
27var listFiles = require('../lib/helpers/list_files.js');
28var readline = require('readline');
29var child_process = require('child_process');
30var stringHelper = require('../lib/helpers/string_helper.js');
31
32var app_url, root_app, params, path_templ, lines, file_type, src_content, outStr;
33var count = 0;
34var order = 0;
35var question = false;
36var overwrite_all = false;
37
38
39/**
40* Read all files in template folder.
41* if file exists, user can choose one option from the list of options
42*/
43
44function create_file (src, des, file_name, message) {
45 if (file_type == 'image') {
46 var inStr = fs.createReadStream(src);
47 var outStr = fs.createWriteStream(des);
48 inStr.pipe(outStr);
49 console.log(message + file_name);
50// console.log(outStr._writableState);
51 } else {
52 var write_result = fs.writeFileSync(des, src_content);
53 if (typeof write_result == 'undefined')
54 console.log(message + file_name);
55 else
56 console.log(write_result);
57 }
58}
59function npm_install () {
60 rl.close();
61 console.log(' run '.bold.green + 'npm install');
62 child_process.exec('cd '+ app_url +' && npm install',
63 function (error, stdout, stderr) {
64 console.log('' + stdout);
65 console.log('' + stderr);
66 if (error !== null) {
67 console.log('' + error);
68 }
69 }
70 );
71}
72function create_app () {
73 for (var i = order; i < lines.length; i++) {
74 count++;
75 var line = lines[i].split(" " + path_templ + "/");
76 var des = root_app + "/" + line[1];
77 var src = path_templ + "/" + line[1];
78
79 if (line[0] == "d") {
80 if (fs.existsSync(des)) console.log(' exist '.bold.blue + line[1]);
81 else {
82 fs.mkdirSync(des);
83 console.log(' create '.bold.green + line[1]);
84 }
85 } else {
86 if (line[1] == 'public/assets/images/trainjs.png' ||
87 line[1] == 'public/assets/images/favicon.ico') {
88 file_type = "image";
89 var src_buff = fs.readFileSync(src);
90 src_content = JSON.stringify(src_buff);
91 } else {
92 file_type = "text";
93 src_content = fs.readFileSync(src).toString();
94 for (var k in params) {
95 var reg = new RegExp("%%" + k + "%%", "g");
96 src_content = src_content.replace(reg, params[k]);
97 }
98 }
99
100 if (fs.existsSync(des)) {
101 question = false;
102 if (file_type == "text")
103 var des_content = fs.readFileSync(des).toString();
104 else {
105 var des_buff = fs.readFileSync(des);
106 var des_content = JSON.stringify(des_buff);
107 }
108
109 if (src_content == des_content) {
110 console.log(' identical '.bold.blue + line[1]);
111 } else if (src_content != des_content && overwrite_all == true) {
112 console.log(' conflict '.bold.red + line[1])
113 var message = ' force '.bold.yellow;
114 create_file(src, des, line[1], message);
115 } else if (src_content != des_content && overwrite_all == false) {
116 order = i + 1;
117 question = true;
118 console.log(' conflict '.bold.red + line[1])
119 console.log('Overwrite '+ des +'? (enter "h" for help) [Ynaqdh]');
120 break;
121 }
122 } else {
123 var message = ' create '.bold.green;
124 create_file(src, des, line[1], message);
125 }
126 }
127 }
128 if (count == lines.length && question == false)
129 npm_install();
130}
131
132var rl = readline.createInterface({
133 input: process.stdin,
134 output: process.stdout,
135 terminal: false
136});
137rl.on('line', function (key) {
138 if (question) {
139 var line = lines[order - 1].split(" " + path_templ + "/");
140 var file_name = line[1];
141 var des = root_app + "/" + line[1];
142 var src = path_templ + "/" + line[1];
143 if (key == "h") {
144 console.log('Y - yes, overwrite');
145 console.log('n - no, do not overwrite');
146 console.log('a - all, overwrite this and all others');
147 console.log('q - quit, abort');
148 console.log('d - diff, show the differences between the old and the new');
149 console.log('h - help, show this help');
150 } else if (key == "y" || key == "Y") {
151 var message = ' force '.bold.yellow;
152 create_file(src, des, file_name, message);
153 create_app();
154 if (count == lines.length)
155 npm_install();
156 } else if (key == "n") {
157 var message = ' skip '.bold.yellow + file_name;
158 console.log(message);
159 create_app();
160 if (count == lines.length)
161 npm_install();
162 } else if (key == "a") {
163 overwrite_all = true;
164 var message = ' force '.bold.yellow;
165 create_file(src, des, file_name, message);
166 create_app();
167 if (count == lines.length)
168 npm_install();
169 else
170 rl.close();
171 } else if (key == "q") {
172 console.log('Aborting...');
173 rl.close();
174 } else if (key == "d") {
175 if (file_type == "text") {
176 var des_content = fs.readFileSync(des).toString();
177 var diff_result = diff.createPatch(des, des_content, src_content);
178 console.log(diff_result);
179 } else {
180 console.log(des + ' is binary file');
181 }
182 console.log('Overwrite '+ des +'? (enter "h" for help) [Ynaqdh]');
183 }
184 }
185});
186
187module.exports = function (info_param) {
188 params = info_param;
189 app_url = process.argv[3];
190 var url_arr = app_url.split('/');
191 params.app_name = url_arr[url_arr.length - 1];
192 params.angular_app_name = stringHelper.Underscore2CamelCase( params.app_name );
193 params.title = stringHelper.Underscore2CamelCase( stringHelper.toTitleCase( params.app_name ) );
194 var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
195 path_templ = lib + 'template/new_app';
196
197 if (app_url.substring(0,1) == "/")
198 root_app = app_url;
199 else
200 root_app = './' + app_url;
201
202 if (fs.existsSync(root_app)) console.log(' exist '.bold.blue);
203 else {
204 fs.mkdirSync(root_app);
205 console.log(' create '.bold.green);
206 }
207
208 lines = listFiles(path_templ);
209 create_app();
210}