forked from CoNarrative/glujs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
202 lines (179 loc) · 5.9 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
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
var buildDir = 'build';
var path = require('path');
var uglify = require('uglify-js');
var fs = require('fs');
var cli = require('commander');
var wrench = require('wrench');
var glob = require('glob-whatev')
cli
.version('0.1.0')
.option('--pretty', 'Skip uglification and just concatenate the files for debugging.')
cli
.command('build')
.description('concatenate (and uglify unless using --pretty) files and place them in the build directory')
.action(build)
cli
.command('clean')
.description('erase the build directory and its contents')
.action(clean)
function build() {
clean();
console.log('building all...');
gluJS();
gluJSTest();
gluJSExt3();
gluJSExt4();
copyToBuild('MIT.LICENSE');
copyToBuild('readme.txt');
copyExamples();
}
function clean() {
if (path.existsSync(buildDir)) {
wrench.rmdirSyncRecursive('build');
console.log('deleted old build.');
} else {
console.log('no old build to clean.');
}
}
function copyExamples() {
console.log('copying examples');
copyDirs('examples', buildDir + '/examples', function(kind, dir, filename, contents){
if (filename.match(/^\./)) return false;
if (kind==='f' && (filename==='index.html' || filename==='runner.html')) {
return contents.replace(/\.\.\/\.\.\/build\/glu/g,'../../glu');
}
});
wrench.mkdirSyncRecursive(buildDir + '/lib/extjs-4.x');
}
function gluJS() {
console.log('building Core');
var files = new FileList()
.include('src/glu.js')
.include('src/conventions.js')
.include('src/viewmodel.js')
.include('src/list.js')
.include('src/*.js');
concat('build/glu.js', files);
}
function gluJSTest() {
console.log('building test library');
var files = new FileList()
.include('src/test/*.js');
concat('build/glu-test.js', files);
}
function gluJSExt3() {
console.log('building Ext JS 3.x adapter');
var files = new FileList()
.include('src/providers/sencha/setup.js')
.include('src/providers/sencha/*.js')
.include('src/providers/sencha/adapters_extjs/basic.js')
.include('src/providers/sencha/adapters_extjs/*.js')
.include('src/providers/sencha/adapters_extjs3/*.js')
concat('build/glu-extjs-3.js', files);
}
function gluJSExt4() {
console.log('building Ext JS 4.x adapter');
var files = new FileList()
.include('src/providers/sencha/setup.js')
.include('src/providers/sencha/*.js')
.include('src/providers/sencha/adapters_extjs/basic.js')
.include('src/providers/sencha/adapters_extjs/*.js')
.include('src/providers/sencha/adapters_extjs4/*.js')
concat('build/glu-extjs-4.js', files);
}
//MINI-BUILD LIBRARY
var license = "// Copyright (c) 2012 CoNarrative - http://www.conarrative.com/\n" +
"// License: MIT (http://www.opensource.org/licenses/mit-license.php)\n" +
"// GluJS version 1.1.0\n";
function concat(combined, files) {
//make parent directory of combined output file
var lastSlashIdx = combined.lastIndexOf('/');
if (lastSlashIdx == -1) {
lastSlashIdx = combined.lastIndexOf('\\');
}
if (lastSlashIdx > 0) {
wrench.mkdirSyncRecursive(combined.substring(0, lastSlashIdx));
}
if (files.toArray) {
files = files.toArray();
}
var out = files.map(function (filePath) {
var src = fs.readFileSync(filePath, 'utf8');
return cli.isPretty ?
'//BEGIN ' + filePath + "\n" + src + '//END' + filePath + "\n" :
src;
});
var final = cli.pretty ?
license + '\n' + out.join('\n') :
license + uglify(out.join(' '));
fs.writeFileSync(combined, final, 'utf8');
}
function uglify(contents) {
var jsp = uglify.parser,
pro = uglify.uglify;
ast = jsp.parse(contents);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
return pro.gen_code(ast);
}
FileList = function () {
this.files = {};
};
FileList.prototype.include = function (pattern) {
var thisBatch = glob.glob(pattern);
for (var i = 0; i < thisBatch.length; i++) {
this.files[thisBatch[i]] = true;
}
return this;
}
FileList.prototype.toArray = function () {
var files = [];
for (var filename in this.files) {
files.push(filename);
}
return files;
}
copyDirs = function(sourceDir, newDir, filter) {
filter = filter || function(){};
var checkDir = fs.statSync(sourceDir);
try {
fs.mkdirSync(newDir, checkDir.mode);
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}
var files = fs.readdirSync(sourceDir);
for(var i = 0; i < files.length; i++) {
var filename = files[i];
var destPath = newDir + '/' + filename;
var currFile = fs.lstatSync(sourceDir + '/' + filename);
if(currFile.isDirectory()) {
//directory
if (filter('d', sourceDir, filename)!==false) {
copyDirs(sourceDir + '/' + filename, destPath, filter);
}
} else if(currFile.isSymbolicLink()) {
//sym link
var symlink = fs.readlinkSync(sourceDir + '/' + filename);
if (filter('s', sourceDir, filename)!=false) {
fs.symlinkSync(symlink, destPath);
}
} else {
//file
var contents = fs.readFileSync(sourceDir + '/' + filename, 'utf8');
var filtered = filter('f', sourceDir, filename, contents);
if (filtered!=false) {
if (typeof(filtered)==='string') {
contents = filtered;
}
fs.writeFileSync(destPath, contents);
}
}
}
};
function copyToBuild(oldFileName){
oldFile = fs.createReadStream(oldFileName);
newFile = fs.createWriteStream(buildDir + '/' + oldFileName);
require('util').pump(oldFile, newFile);
}
//EXECUTE
cli.parse(process.argv);