-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathsync.js
526 lines (451 loc) · 15 KB
/
sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
var ARGV = process.argv;
var ROOT = __dirname;
var path = require('path');
var fs = require('fs');
var async = require('async');
var spawn = require('child_process').spawn;
var util = require('util');
var hash = require('object-hash');
var assign = require('object-assign');
function mixin(a, b) {
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
}
function loadConfig(path) {
console.log('Load config from %s', path);
var list = require(path);
var map = {};
if (Array.isArray(list)) {
list.forEach(function(item) {
map[item.version] = item;
if (item.extend) {
if (!map[item.extend]) {
throw new Error('Extend `'+item.extend+'` is not defined.');
}
var copySelf = mixin({}, item)
mixin(item, map[item.extend]);
mixin(item, copySelf);
delete item.extend;
}
});
} else {
var pkg = list;
list = [];
var tags = pkg.tags;
tags.forEach(function(tag) {
var data = assign({}, pkg, tag);
delete data.tags;
delete data.__hash;
var hashvalue = hash(data);
if (hashvalue === tag.__hash) {
return;
}
if (data.mapping) {
data.mapping = data.mapping.map(function(subitem) {
var clone = assign({}, subitem);
var reg = new RegExp(subitem.reg, "i");
clone.reg = reg;
return clone;
});
}
list.push(data);
});
}
return list;
}
function createRepos(repos, token, from, folder) {
if (!token) {
process.stderr.write('ERROR, must given token of GITHUB!\n');
process.exit(1);
}
var GithubAPI = require('github');
var github = new GithubAPI({
version: '3.0.0'
});
github.authenticate({
type: "token",
token: token
});
github.repos.createFromOrg({
name: repos,
org: 'fis-components',
description: 'Fork from ' + from
}, function(err, data) {
if (err) {
//throw err;
process.exit(1);
}
process.exit(0);
});
delete github;
}
function getLastMessage(cb) {
var git_log = spawn('git', ['log', '-1', '--pretty=%B']);
git_log.stderr.pipe(process.stderr);
var message = '';
git_log.stdout.on('data', function(c) {
message += c.toString();
});
git_log.stdout.on('end', function() {
cb(message);
});
}
function getFilesFromLastMessage(cb) {
getLastMessage(function(message) {
var m = /^(update|forceupdate)\s+(.+)/.exec(message);
var finder = require('./finder.js');
var files;
var force = false;
console.log('Last Message: %s', message);
if (m) {
files = m[2].split(/\s+/);
force = m[1] === "forceupdate";
} else if (~message.indexOf('bos sync')) {
// 更新所有 bos
// files = finder(__dirname, 'modules/**/*.js')
// .map(function(i) {
// return i.relative;
// })
// .filter(function(p) {
// if (p.indexOf('modules') == -1) {
// return false;
// }
// return /\.js$/.test(p);
// });
// var queue = [];
// files.forEach(function(file) {
// var list = loadConfig('./' + file);
// var name = file.replace('modules/', '')
// .replace(/\.js$/, '');
// list.forEach(function(r) {
// queue.push(function(cb) {
// var h = spawn('bash', [
// path.join(ROOT, 'bosSync.sh'),
// name || r.name,
// r.version
// ], {
// cwd: __dirname
// });
// h.on('exit', function(code) {
// if (code) {
// process.exit(1);
// }
// cb();
// });
// h.stdout.pipe(process.stdout);
// h.stderr.pipe(process.stderr);
// });
// });
// });
// console.log('Start BOS Sync...');
// async.series(queue, function() {
// console.log('done');
// });
// return;
}
if (files && files.length) {
files = finder(__dirname, files)
.map(function(i) {
return i.relative;
})
.filter(function(p) {
if (p.indexOf('modules') == -1 && p.indexOf("packages") == -1) {
return false;
}
return /\.js(?:on)?$/.test(p);
});
if (files.length) {
cb(files, force);
}
}
});
}
function lastChangFiles(cb) {
var lastSuccessMessageId = 'HEAD^';
var remote = 'https://raw.githubusercontent.com/fis-components/components/history/commitId.log';
console.log('Fetching ' + remote);
var curl = spawn('curl', [remote]);
var body = '';
curl.stderr.pipe(process.stderr);
// curl.stdout.pipe(process.stdout);
curl.stdout.on('data', function(c) {
body += c.toString();
});
curl.stdout.on('end', function() {
if (!/Not Found/i.test(body)) {
lastSuccessMessageId = body.trim();
}
exec(util.format('git diff --name-status %s..HEAD', lastSuccessMessageId), function(error, data) {
if (error) {
exec('git diff --name-status HEAD^..HEAD', function(error, data) {
if (error) {
console.log(error);
} else {
onDiffData(data);
}
});
} else {
onDiffData(data);
}
});
});
function exec(command, cb) {
console.log(command);
var args = command.split(/\s+/);
var program = args.shift();
var child = spawn(program, args);
var err = '', std = '';
child.stderr.on('data', function(c) {
err += c.toString();
});
child.stdout.on('data', function(c) {
std += c.toString();
});
child.on('close', function(code) {
code ? cb(err || 'Spawn Error') : cb(null, std);
})
}
function onDiffData(o) {
var arr = o.split('\n');
arr = arr
.filter(function(line) {
var parts = line.split(/\s+/);
if (!line || parts[0] === 'D') {
return false;
}
if (parts[1].indexOf('modules') == -1 && parts[1].indexOf("packages") == -1) {
return false;
}
return /\.js(?:on)?$/.test(parts[1]);
})
.map(function(line) {
return line.split(/\s+/)[1];
});
// 如果没有 modules 更新。则读取 commint message 指令。
if (!arr.length) {
console.log('Nothing changed.')
return getFilesFromLastMessage(cb);
}
getLastMessage(function(message) {
//cb(['modules/jquery.js']);
cb(arr, !/softupdate/i.test(message), function(cb) {
// 保存最后一次处理的 git message id.
var child = spawn('bash', [path.join(ROOT, 'storeStatus.sh')], {
cwd: __dirname
});
child.on('exit', function(code) {
if (code) {
process.exit(1);
}
cb();
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
});
})
}
}
function dumpMapping(mapping) {
var string = '[\n';
mapping.forEach(function(map) {
string += ' {\n';
string += ' reg: ' + map.reg + ',\n';
string += ' release:' + map.release + '\n';
string += ' },\n';
});
string += '];';
return string;
}
if (ARGV[2] == 'sync') {
var sync = function(arr, rebuild, callback) {
var queue = [];
arr.forEach(function(name) {
var list = loadConfig(path.join(ROOT, name));
var isFromJson = /\.json$/.test(name);
name = name.replace(/(?:modules|packages)\//, '')
.replace(/\.js(?:on)?$/, '');
var basename = path.basename(name);
list.forEach(function(r) {
queue.push(function(cb) {
var h = spawn('bash', [
path.join(ROOT, 'build.sh'),
basename,
r.repos,
r.build || '',
r.version,
r.build_dest || '',
r.tag || r.version,
rebuild ? 'true' : 'false',
name.substring(0, name.length - basename.length),
isFromJson ? 'true' : 'false'
], {
cwd: __dirname
});
// h.stdout.on('end', function() {
// cb();
// });
h.on('exit', function(code) {
if (code) {
process.exit(1);
}
cb();
});
h.stdout.pipe(process.stdout);
h.stderr.pipe(process.stderr);
});
});
});
queue.push(function(cb) {
callback ? callback(cb) : cb();
});
async.series(queue, function() {
console.log('done');
});
};
// 直接从 argv 里面读取。
if (ARGV.length > 3) {
console.log('Sync with args...')
var files = ARGV.slice(3);
var finder = require('./finder.js');
files = finder(__dirname, files)
.map(function(i) {
return i.relative;
})
.filter(function(p) {
if (p.indexOf('modules') == -1 && p.indexOf("packages") == -1) {
return false;
}
return /\.js(?:on)?$/.test(p);
});
if (files.length) {
sync(files, true);
}
} else {
lastChangFiles(sync);
}
} else if (ARGV[2] == 'create-repos') {
console.log('=sync.js create repos: https://github.com/fis-components/%s', ARGV[3]);
createRepos(ARGV[3], ARGV[4], ARGV[5], ARGV[6]);
} else if (ARGV[2] == 'create-component.json') {
var name = ARGV[3].trim();
var version = ARGV[4].trim();
var isFromJson = (ARGV[5] || '').trim() === "true";
var folder = (ARGV[6] || '').trim();
console.log.apply(console, ARGV);
console.log(name, version, folder, isFromJson);
try {
var list = loadConfig(
isFromJson ?
path.join(ROOT, 'packages', folder + name + '.json'):
path.join(ROOT, 'modules', folder + name + '.js')
);
for (var i = 0; i < list.length; i++) {
var r = list[i];
if (r.version != version) {
continue;
}
r.name = name;
// fs.writeFileSync(
// path.join(ROOT, '_' + name, '__mapping.js'),
// 'module.exports=' + dumpMapping(r.mapping)
// );
//r.mapping = './__mapping.js';
delete r.mapping; //@TODO
delete r.build;
delete r.shim;
// normalize dependencies for better compatibility.
if (r.dependencies && Array.isArray(r.dependencies)) {
var dependencies = {};
r.dependencies.forEach(function(item) {
var parts = item.split('@');
dependencies[parts[0]] = parts[1] || '*';
});
r.dependencies = dependencies;
}
// console.log('Write to %s with data:\n%s', path.join(ROOT, '_' + name, 'component.json'), JSON.stringify(r, null, 2));
fs.writeFileSync(
path.join(ROOT, '_' + name, 'component.json'),
JSON.stringify(r, null, ' ')
);
break;
}
} catch (e) {
throw e;
//process.exit(1);
}
process.exit(0);
} else if (ARGV[2] == 'move') {
var name = ARGV[3].trim();
var version = ARGV[4].trim();
var from = ARGV[5].trim();
var to = ARGV[6].trim();
var folder = (ARGV[7] || '').trim();
var isFromJson = (ARGV[8] || '').trim() === "true";
try {
console.log("load config from %s", folder + name + (isFromJson ? '.js' : '.json') );
// var list = loadConfig(path.join(ROOT, 'modules', folder + name + '.js'));
var list = loadConfig(
isFromJson ?
path.join(ROOT, 'packages', folder + name + '.json'):
path.join(ROOT, 'modules', folder + name + '.js')
);
for (var i = 0; i < list.length; i++) {
var r = list[i];
if (r.version != version) {
continue;
}
var Scaffold = require('fis-scaffold-kernel');
var scaffold = new Scaffold({
log: {
//level: 0
}
});
r.mapping.unshift({
reg: /\.git\/.*/i,
release: false
}, {
reg: /^\/component\.json$/i,
release: '$0'
});
var ok = scaffold.deliver(from, to, r.mapping);
if (ok == 0) {
process.exit(1); //fail
}
}
} catch (e) {
throw e;
process.exit(1);
}
process.exit(0);
} else if (ARGV[2] == 'convert') {
var name = ARGV[3].trim();
var version = ARGV[4].trim();
var dist = ARGV[5].trim();
var folder = (ARGV[6] || '').trim();
var isFromJson = (ARGV[7] || '').trim() === "true";
var convert = require('./convert.js');
var finder = require('./finder.js');
var list = loadConfig(
isFromJson ?
path.join(ROOT, 'packages', folder + name + '.json'):
path.join(ROOT, 'modules', folder + name + '.js'));
for (var i = 0; i < list.length; i++) {
var r = list[i];
if (r.version != version) {
continue;
}
var jses = finder(dist, '**/*.js');
convert({
files: jses,
convertAsync: false,
config: r,
dir: dist
}, function() {
console.log('convert done')
});
}
}