forked from xiaohulu/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
142 lines (108 loc) · 3.13 KB
/
update.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
/**
* @fileoverview update all modules to the latest version and
* rewrite registry.js
*
* @usage node update_all.js
* @author lifesinger@gmail.com <Frank Wang>
*/
var fs = require('fs');
var path = require('path');
var uglifyjs = require('uglify-js');
var jsp = uglifyjs.parser;
var pro = uglifyjs.uglify;
var zlib = require('zlib');
var argv2 = process.argv[2];
var force = process.argv[3] === '-f' || process.argv[3] === '--force';
var Transport = require('../spm/lib/actions/transport.js');
// init registry
const REGISTRY_FILE = path.join(__dirname, 'registry.js');
var registry = {};
if (path.existsSync(REGISTRY_FILE)) {
var code = fs.readFileSync(REGISTRY_FILE, 'utf8');
registry = JSON.parse(code.replace('define({', '{').replace('});', '}'));
}
// get module names
var items = argv2 ? [argv2] : getModuleNames(__dirname);
// run
next();
function next() {
processItem(items.shift());
}
function processItem(item) {
if (item) {
var filepath = path.join(__dirname, item, 'transport.js');
Transport.transport(filepath, function(data) {
var meta = data.meta;
var name = meta.name.toLowerCase();
// already exists
if (data.errCode && registry[name]) {
next();
return;
}
// add to registry
registry[name] = meta;
// add dirpath
meta.dirpath = item;
// delete unnecessary properties
delete meta.transportFile;
if (meta.root.indexOf(':') !== -1) { // not local path
if (meta.extra) {
meta.extra = meta.extra.map(function(p) {
return p.replace(meta.root, '');
});
}
cutRoot(meta, 'src');
cutRoot(meta, 'min');
}
else {
delete meta.root;
}
// get file size info
getFileSize(meta, next);
}, { force: force });
}
else {
updateRegistry();
}
}
function getModuleNames(dirpath) {
return fs.readdirSync(dirpath).filter(function(item) {
return item.charAt(0) !== '.' &&
fs.statSync(path.join(dirpath, item)).isDirectory();
});
}
function cutRoot(meta, name) {
if (meta[name]) {
meta[name] = meta[name].replace(meta.root, '');
}
}
function getFileSize(meta, callback) {
var filename = meta['filename'];
var minFile = path.join(__dirname, meta.dirpath, meta.version, filename + '.js');
var srcFile = minFile.replace(/\.js$/, '-debug.js');
console.log(' ... Reading %s', minFile);
zlib.gzip(fs.readFileSync(minFile, 'utf8'), function(err, data) {
if (err) throw err;
meta['gzipped'] = formatSize(data.length);
if (path.existsSync(srcFile)) {
console.log(' ... Reading %s', srcFile);
meta['raw'] = formatSize(fs.statSync(srcFile).size);
}
callback();
});
}
function formatSize(size) {
return Math.round(size / 1024) + 'KB';
}
function updateRegistry() {
var ast = jsp.parse('define(' + JSON.stringify(registry) + ')');
var code = pro.gen_code(ast, {
'beautify': true,
'indent_level': 2,
'quote_keys': true
});
//code = code.replace('define({', '{').replace('});', '}');
fs.writeFile(REGISTRY_FILE, code, 'utf8', function() {
console.log(' Done!');
});
}