1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var fs = require('fs');
|
24 | var colors = require('colors');
|
25 | var diff = require('diff');
|
26 | var listFiles = require('../lib/helpers/list_files.js');
|
27 | var readline = require('readline');
|
28 | var root_app = process.cwd();
|
29 |
|
30 | var params, path_templ, lines, src_content, outStr;
|
31 | var count = 0;
|
32 | var file_order = 0;
|
33 | var template_order = 0;
|
34 | var question = false;
|
35 | var overwrite_all = false;
|
36 |
|
37 | function create_file (src, des, file_path, message) {
|
38 | var write_result = fs.writeFileSync(des, src_content);
|
39 | if (typeof write_result == 'undefined')
|
40 | console.log(message + file_path);
|
41 | else
|
42 | console.log(write_result);
|
43 | }
|
44 |
|
45 | function generate_scaffold () {
|
46 | for (var i = file_order; i < lines.length; i++) {
|
47 | question = false;
|
48 | count++;
|
49 | var line = lines[i].split(" " + path_templ + "/");
|
50 | var src = path_templ + "/" + line[1];
|
51 |
|
52 | if (line[0] == "d") {
|
53 | if (dir_templates && dir_templates[line[1]]) {
|
54 | for (var t = 0; t < dir_templates[line[1]].length; t++) {
|
55 | var dir_path = dir_templates[line[1]][t]['dir_path'];
|
56 | var des = root_app + "/" + dir_path;
|
57 | if (!fs.existsSync(des)) {
|
58 | fs.mkdirSync(des);
|
59 | console.log(' create '.bold.green + dir_path);
|
60 | }
|
61 | }
|
62 | } else {
|
63 | var des = root_app + "/" + line[1];
|
64 | if (!fs.existsSync(des)) {
|
65 | fs.mkdirSync(des);
|
66 | console.log(' create '.bold.green + line[1]);
|
67 | }
|
68 | }
|
69 | } else {
|
70 | if (file_templates[line[1]]) {
|
71 | for (var t = template_order; t < file_templates[line[1]].length; t++) {
|
72 | var info_render = file_templates[line[1]][t]['info_render'];
|
73 | var file_path = file_templates[line[1]][t]['file_path'];
|
74 | var des = root_app + "/" + file_path;
|
75 | src_content = fs.readFileSync(src).toString();
|
76 | for (var k in info_render) {
|
77 | var reg = new RegExp("%%" + k + "%%", "g");
|
78 | src_content = src_content.replace(reg, info_render[k]);
|
79 | }
|
80 |
|
81 | if (fs.existsSync(des)) {
|
82 | var des_content = fs.readFileSync(des).toString();
|
83 | if (src_content == des_content) {
|
84 | console.log(' identical '.bold.blue + file_path);
|
85 | if (template_order == file_templates[line[1]].length - 1)
|
86 | template_order = 0;
|
87 | } else if (src_content != des_content && overwrite_all == true) {
|
88 | console.log(' conflict '.bold.red + file_path)
|
89 | var message = ' force '.bold.yellow;
|
90 | create_file(src, des, file_path, message);
|
91 | } else if (src_content != des_content && overwrite_all == false) {
|
92 | file_order = i;
|
93 | template_order = t;
|
94 | question = true;
|
95 | console.log(' conflict '.bold.red + file_path)
|
96 | console.log('Overwrite '+ des +'? (enter "h" for help) [Ynaqdh]');
|
97 | break;
|
98 | }
|
99 | } else {
|
100 | var message = ' create '.bold.green;
|
101 | create_file(src, des, file_path, message);
|
102 | if (template_order == file_templates[line[1]].length - 1)
|
103 | template_order = 0;
|
104 | }
|
105 | }
|
106 |
|
107 | if (question) break;
|
108 | } else {
|
109 | var file_path = line[1];
|
110 | var des = root_app + "/" + file_path;
|
111 | src_content = fs.readFileSync(src).toString();
|
112 |
|
113 | if (fs.existsSync(des)) {
|
114 | var des_content = fs.readFileSync(des).toString();
|
115 | if (src_content == des_content) {
|
116 | console.log(' identical '.bold.blue + file_path);
|
117 | } else if (src_content != des_content && overwrite_all == true) {
|
118 | console.log(' conflict '.bold.red + file_path)
|
119 | var message = ' force '.bold.yellow;
|
120 | create_file(src, des, file_path, message);
|
121 | } else if (src_content != des_content && overwrite_all == false) {
|
122 | file_order = i;
|
123 | question = true;
|
124 | console.log(' conflict '.bold.red + file_path)
|
125 | console.log('Overwrite '+ des +'? (enter "h" for help) [Ynaqdh]');
|
126 | break;
|
127 | }
|
128 | } else {
|
129 | var message = ' create '.bold.green;
|
130 | create_file(src, des, file_path, message);
|
131 | }
|
132 | }
|
133 | }
|
134 | }
|
135 |
|
136 | if (count >= lines.length && question == false)
|
137 | rl.close();
|
138 | }
|
139 |
|
140 | function resumeGenerate(obj) {
|
141 | if (obj) {
|
142 | if (obj.length == 1) {
|
143 | template_order = 0;
|
144 | file_order++;
|
145 | } else {
|
146 | if (template_order < obj.length - 1) {
|
147 | template_order++;
|
148 | } else {
|
149 | template_order = 0;
|
150 | file_order++;
|
151 | }
|
152 | }
|
153 | } else {
|
154 | template_order = 0;
|
155 | file_order++;
|
156 | }
|
157 | generate_scaffold();
|
158 | }
|
159 |
|
160 | var rl = readline.createInterface({
|
161 | input: process.stdin,
|
162 | output: process.stdout,
|
163 | terminal: false
|
164 | });
|
165 | rl.on('line', function (key) {
|
166 | if (question) {
|
167 | var line = lines[file_order].split(" " + path_templ + "/");
|
168 | var src = path_templ + "/" + line[1];
|
169 | if (file_templates[line[1]]) {
|
170 | var file_path = file_templates[line[1]][template_order]['file_path'];
|
171 | } else {
|
172 | var file_path = line[1];
|
173 | }
|
174 | var des = root_app + "/" + file_path;
|
175 |
|
176 | if (key == "h") {
|
177 | console.log('Y - yes, overwrite');
|
178 | console.log('n - no, do not overwrite');
|
179 | console.log('a - all, overwrite this and all others');
|
180 | console.log('q - quit, abort');
|
181 | console.log('d - diff, show the differences between the old and the new');
|
182 | console.log('h - help, show this help');
|
183 | } else if (key == "y" || key == "Y") {
|
184 | var message = ' force '.bold.yellow;
|
185 | create_file(src, des, file_path, message);
|
186 | if (count == lines.length)
|
187 | rl.close();
|
188 | else
|
189 | resumeGenerate(file_templates[line[1]]);
|
190 | } else if (key == "n") {
|
191 | var message = ' skip '.bold.yellow + file_path;
|
192 | console.log(message);
|
193 | if (count == lines.length)
|
194 | rl.close();
|
195 | else
|
196 | resumeGenerate(file_templates[line[1]]);
|
197 | } else if (key == "a") {
|
198 | overwrite_all = true;
|
199 | var message = ' force '.bold.yellow;
|
200 | create_file(src, des, file_path, message);
|
201 | resumeGenerate(file_templates[line[1]]);
|
202 | rl.close();
|
203 | } else if (key == "q") {
|
204 | console.log('Aborting...');
|
205 | rl.close();
|
206 | } else if (key == "d") {
|
207 | var des_content = fs.readFileSync(des).toString();
|
208 | var diff_result = diff.createPatch(des, des_content, src_content);
|
209 | console.log(diff_result);
|
210 | console.log('Overwrite '+ des +'? (enter "h" for help) [Ynaqdh]');
|
211 | }
|
212 | }
|
213 | });
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | module.exports = function(path, dir_templs, file_templs) {
|
220 | path_templ = path;
|
221 | dir_templates = dir_templs;
|
222 | file_templates = file_templs;
|
223 | lines = listFiles(path_templ);
|
224 | generate_scaffold();
|
225 | }
|