forked from Patternslib/Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·174 lines (149 loc) · 4.94 KB
/
build.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
#!/usr/bin/env node
var program = require('commander'),
crypto = require('crypto'),
fs = require('fs'),
git = require('git-rev'),
path = require('path'),
rimraf = require('rimraf'),
spawn = require('child_process').spawn;
function list(val) {
return val.split(',');
}
program
.option('-n, --no-minify', 'Do no minify package')
.option('--no-symlink', 'Do not symlink new bundle')
.option('-t, --tag [version]', 'Build specific version')
.option('-m, --modules [module1,module2,...]',
'Include only these modules in bundle', list)
.parse(process.argv);
var build = function(tag, cleanup) {
var filename;
if (program.modules) {
// User wants a custom bundle
var patterns = program.modules.sort().join('&'),
shasum = crypto.createHash('sha1');
shasum.update(tag + '?' + patterns);
var hex = shasum.digest('hex').substr(0,8);
filename = 'patterns-' + tag+ '-' + hex + (program.minify?'.min':'') + '.js';
} else {
filename = 'patterns-' + tag + (program.minify?'.min':'') + '.js';
}
var modules = program.modules || ['main'];
modules = modules.sort();
// Generate args that are passed to jam
var fullname = path.join(__dirname, 'bundles', filename);
// The CLI interface is:
// jam compile -i pat1 [-i pat2 [-i pat3]] -o outfile
var args = [
'build',
'--almond',
'--verbose',
'-i', modules.join(','),
'-o', fullname
];
if (!program.minify)
args.push('--no-minify');
var p = spawn(path.join(__dirname, 'node_modules/.bin/bungle'), args);
p.stdout.on('data', function(data){
console.log(data.toString());
});
p.stderr.on('data', function(data){
console.log('stderr ' + data);
});
p.on('exit', function(code) {
if (code === 0) {
var header = "/**\n" +
" * Patterns bundle " + tag + (hex?"-"+hex:"") + "\n" +
" *\n" +
" * See http://patternslib.com/ for more information.\n" +
" *\n";
if (modules.indexOf('main') === -1) {
header += " * Included patterns:\n * - ";
header += program.modules.sort().join("\n * - ") + "\n";
}
header += " */\n\n";
var js = fs.readFileSync(fullname);
var init = '';
var deps = modules
.map(function(e){ return '"'+e+'"'; })
.join(', ');
init = "require(['registry', " + deps +
"], function(r){r.init();});";
var stream = fs.createWriteStream(fullname);
stream.write(header + js + init);
stream.end();
if (program.symlink) {
var link = path.join(__dirname, 'bundles',
'patterns' + (program.minify?'.min':'') + '.js');
if (fs.existsSync(link)) {
fs.unlinkSync(link);
}
fs.symlinkSync(filename, link);
}
console.log('Great success!');
return cleanup();
} else {
console.log('Build failed ...');
return cleanup();
}
});
return;
};
var checkout = function(tmpdir, tag) {
var p = spawn('git', ['checkout', program.tag]);
p.stdout.on('data', function(data){
console.log('stdout ' + data);
});
p.stderr.on('data', function(data){
console.log('stderr ' + data);
});
p.on('exit', function(code, status){
if (code === 0) {
build(tag, function(){
rimraf.sync(tmpdir);
process.exit(0);
});
} else {
console.log('could not checkout tag ' + tag);
process.exit(1);
}
});
};
var clone = function(tmpdir, tag) {
var p = spawn('git', ['clone', '-s', '.', tmpdir]);
p.stdout.on('data', function(data){
console.log('stdout ' + data);
});
p.stderr.on('data', function(data){
console.log('stderr ' + data);
});
p.on('exit', function(code, status){
if (code === 0) {
try {
process.chdir(tmpdir);
} catch(err) {
console.log('chdir: ' + err);
process.exit(1);
}
checkout(tmpdir, tag);
} else {
console.log('could not clone');
process.exit(1);
}
});
};
if (program.tag) {
var randChar = function() {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
return chars[Math.floor(Math.random() * chars.length)];
};
var tmpdir;
do {
tmpdir = '/tmp/patterns-' + Array(8).join('.').split('').map(randChar).join('');
} while(fs.existsSync(tmpdir));
clone(tmpdir, program.tag);
} else {
git.tag(function (tag) {
build(tag, function(){});
});
}