|
| 1 | +#!/usr/bin/node |
| 2 | + |
| 3 | +var fs = require('fs'), |
| 4 | + sys = require('sys'), |
| 5 | + exec = require('child_process').exec, |
| 6 | + jsPath = 'js/formvalidator/', |
| 7 | + mainScript = jsPath + 'jquery.formvalidator.js', |
| 8 | + newVersion = -1; |
| 9 | + |
| 10 | +/* |
| 11 | + * Find out new version number |
| 12 | + */ |
| 13 | +var versionParts = fs.readFileSync(mainScript, 'utf-8').split('@version ')[1].split('*/')[0].trim().split('.'); |
| 14 | +if(versionParts.length < 3) { |
| 15 | + // new version number is decided in code |
| 16 | + newVersion = versionParts.join('.'); |
| 17 | +} |
| 18 | +else { |
| 19 | + // Increase the last number by one |
| 20 | + var newSubVersion = parseInt(versionParts.splice(versionParts.length-1, 1)[0]) + 1; |
| 21 | + newVersion = versionParts.join('.') + '.' + newSubVersion.toString(); |
| 22 | +} |
| 23 | + |
| 24 | +console.log('Build version: '+newVersion); |
| 25 | + |
| 26 | +/** |
| 27 | + * Create new minified version of a file and change |
| 28 | + * version number |
| 29 | + * @param {String} path |
| 30 | + * @param {String} newName |
| 31 | + */ |
| 32 | +function buildFile(path, newName) { |
| 33 | + var codeParts = fs.readFileSync(path, 'utf-8').split('@version '); |
| 34 | + var lastCodeParts = codeParts[1].split("\n"); |
| 35 | + var origCode = codeParts[0] + '@version '+newVersion+ "\n" + lastCodeParts.slice(1, lastCodeParts.length).join("\n") + ""; |
| 36 | + fs.writeFileSync(path, origCode); |
| 37 | + fs.writeFileSync(newName, ''); |
| 38 | + exec('uglifyjs '+path+' >> '+newName, function (error, stdout, stderr) { |
| 39 | + if(stdout) |
| 40 | + console.log(stdout); |
| 41 | + if(error) |
| 42 | + console.log(error); |
| 43 | + if(stderr) |
| 44 | + console.log(stderr); |
| 45 | + |
| 46 | + console.log('* '+newName); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +buildFile(mainScript, jsPath + 'jquery.formvalidator.min.js'); |
| 51 | +fs.readdirSync(jsPath).forEach(function(f) { |
| 52 | + if(f.substr(-7) == '.dev.js') { |
| 53 | + var compressedFileName = jsPath + f.substr(0, f.length - 6) + 'js'; |
| 54 | + buildFile(jsPath+f, compressedFileName); |
| 55 | + } |
| 56 | +}); |
0 commit comments