forked from victorjonsson/jQuery-Form-Validator
/
Gruntfile.js
179 lines (151 loc) · 5.27 KB
/
Gruntfile.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
const SRC_DIR = './form-validator/src/';
const MODULE_DIR = './form-validator/';
const LANG_DIR = './form-validator/lang/';
const MAIN_PLUGIN_FILE = 'form-validator/jquery.form-validator.min.js';
const JS_EXTENSION = '.js';
const DEV_EXTENSION = '.dev.js';
var fs = require('fs'),
filesToBuild = {
uglify: {},
concat: {
main:{
src:[SRC_DIR+'core-validators.js'],
dest: MAIN_PLUGIN_FILE
}
},
devFiles: []
},
isJavascriptFile = function(fileName) {
return fileName.substr(-3) == JS_EXTENSION;
},
isDevFile = function(fileName) {
return fileName.substr(-1 * DEV_EXTENSION.length) == DEV_EXTENSION;
},
readFile = function (file) {
return fs.readFileSync(file, 'utf-8');
},
replaceInFile = function (path, from, to) {
fs.writeFileSync(path, readFile(path).replace(from, to));
};
module.exports = function (grunt) {
// Gather up all module and language files
[MODULE_DIR, LANG_DIR].forEach(function (path) {
fs.readdirSync(path).forEach(function (fileName) {
if (isDevFile(fileName)) {
var name = fileName.substr(0, fileName.length - DEV_EXTENSION.length),
fullPath = path + name + JS_EXTENSION;
filesToBuild.uglify[fullPath] = [fullPath];
filesToBuild.concat['file' + name] = {
src: [path + fileName],
dest: path + name + JS_EXTENSION
};
filesToBuild.devFiles.push(path + fileName);
}
});
});
// Gather up all source files that will added to minified core library
fs.readdirSync(SRC_DIR).forEach(function (fileName) {
var fullPath = SRC_DIR + fileName;
if (isJavascriptFile(fileName) && filesToBuild.concat.main.src.indexOf(fullPath) == -1) {
filesToBuild.concat.main.src.unshift(fullPath);
}
});
// Add options for concat ang ugligy
filesToBuild.concat.options = {
banner: "<%= meta.banner %>"
};
filesToBuild.uglify.options = {
banner: "<%= meta.banner %>"
};
// Add main script to uglify
filesToBuild.uglify[MAIN_PLUGIN_FILE] = MAIN_PLUGIN_FILE;
grunt.initConfig({
// Import package manifest
pkg: grunt.file.readJSON("package.json"),
// Banner definitions
meta: {
banner: "/**\n" +
" * <%= (pkg.title || pkg.name).toUpperCase() %>\n" +
" *\n" +
" * @version <%= pkg.version %>\n" +
" * @website <%= pkg.homepage %>\n" +
" * @author <%= pkg.author.name %>, <%= pkg.author.url %>\n" +
" * @license <%= pkg.license %>\n" +
" */\n"
},
// Concat definitions.
concat: filesToBuild.concat,
// Lint definitions
jshint: {
files: [MODULE_DIR+"*"+DEV_EXTENSION, SRC_DIR+"*.js"],
options: {
jshintrc: ".jshintrc"
}
},
// Minify definitions
uglify: filesToBuild.uglify,
// watch for changes to source
// Better than calling grunt a million times
// (call 'grunt watch')
watch: {
files: [SRC_DIR+'/*', LANG_DIR+'/*', MODULE_DIR+'/*'],
tasks: ['build'],
options : { nospawn : true }
},
// Unit tests
qunit: {
all: ['test/qunit.html']
},
// Standalone servers
connect: {
server: {
options: {
port: 8000,
base: '.',
keepalive: true
}
}
}
});
/*
* Change to new version or the next version number in all files
* containing the version definition
*/
grunt.registerTask('version', 'Bump up the version number, or change version name by adding --new-version=3.1.0', function () {
var pkg = grunt.config.get('pkg'),
currentVersion = pkg.version,
newVersion = grunt.option('new-version');
if (!newVersion) {
var versionParts = currentVersion.split('.'),
newSubVersion = parseInt(versionParts.splice(versionParts.length - 1, 1)[0]) + 1;
newSubVersion = newSubVersion < 10 && newSubVersion > 0 ? '0' + newSubVersion : newSubVersion.toString();
newVersion = versionParts.join('.') + '.' + newSubVersion;
}
grunt.log.writeln('* Moving from version ' + currentVersion + ' to ' + newVersion);
var fromVersion = '@version ' + currentVersion,
toVersion = '@version ' + newVersion;
// replace version in config files and dev-files
fs.readdirSync(SRC_DIR).forEach(function(file) {
if (isJavascriptFile(file)) {
replaceInFile(SRC_DIR+file, fromVersion, toVersion);
}
});
filesToBuild.devFiles.forEach(function (filePath) {
replaceInFile(filePath, fromVersion, toVersion);
});
replaceInFile('package.json', '"version": "' + currentVersion + '"', '"version": "' + newVersion + '"');
replaceInFile('formvalidator.jquery.json', '"version": "' + currentVersion + '"', '"version": "' + newVersion + '"');
// Set new version globally (can later on be used by concat/uglify)
pkg.version = newVersion;
grunt.config.set('pkg', pkg);
});
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.registerTask("build", ["version", "concat", "uglify"]);
grunt.registerTask('test', ['concat', 'jshint', 'qunit']);
grunt.registerTask("default", ["test", "build"]);
};