forked from victorjonsson/jQuery-Form-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·77 lines (63 loc) · 2.61 KB
/
build
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
#!/usr/bin/env node
var fs = require('fs'),
sys = require('sys'),
exec = require('child_process').exec,
jsPath = 'form-validator/',
mainScript = jsPath + 'jquery.form-validator.js',
mainMinifiedScript = jsPath + 'jquery.form-validator.min.js',
newVersion = -1;
// Find out new version number
var versionParts = fs.readFileSync(mainScript, 'utf-8').split('@version ')[1].split('*/')[0].trim().split('.');
if(versionParts.length < 3) {
// new version number is decided in code
newVersion = versionParts.join('.');
}
else {
// Increase the last number by one
var newSubVersion = parseInt(versionParts.splice(versionParts.length-1, 1)[0]) + 1;
newVersion = versionParts.join('.') + '.' + newSubVersion.toString();
}
console.log('Build version: '+newVersion);
// Get code docs
var documentation = fs.readFileSync(mainScript, 'utf-8').split('*/')[0]+'*/';
var docParts = documentation.split('@version ');
documentation = docParts[0] +'@version '+newVersion+'\n'+docParts[1].split('\n')[1];
/**
* Create new minified version of a file and change version number
* @param {String} sourceFile
* @param {String} targetFile
* @param {String} [bin]
*/
function buildFile(sourceFile, targetFile, bin) {
var codeParts = fs.readFileSync(sourceFile, 'utf-8').split('@version '),
lastCodeParts = codeParts[1].split("\n"),
origCode = codeParts[0] + '@version '+newVersion+ "\n" + lastCodeParts.slice(1, lastCodeParts.length).join("\n") + "";
fs.writeFileSync(sourceFile, origCode);
fs.writeFileSync(targetFile, '');
if(!bin)
bin = 'uglifyjs %source% >> %target%';
exec(bin.replace('%source%', sourceFile).replace('%target%', targetFile), function (error, stdout, stderr) {
if(stdout)
console.log('stdout: '+stdout);
if(error)
console.log('error: '+error);
if(stderr)
console.log('stderror: '+stderr);
console.log('* '+targetFile);
});
}
// Minify main script
buildFile(mainScript, mainMinifiedScript);
// Put top documentation back into minified main script
fs.writeFileSync(mainMinifiedScript, documentation+"\n"+fs.readFileSync(mainMinifiedScript, 'utf-8'));
// Minify modules and language files
[jsPath, jsPath+'lang/'].forEach(function(path) {
fs.readdirSync(path).forEach(function(fileName) {
if(fileName.substr(-7) == '.dev.js') {
var compressedFileName = path + fileName.substr(0, fileName.length - 6) + 'js';
buildFile(path+fileName, compressedFileName);
}
});
});
// Minify default theme css
buildFile(jsPath+'theme-default.css', jsPath+'theme-default.min.css', 'sqwish %source%');