-
Notifications
You must be signed in to change notification settings - Fork 474
/
Gruntfile.js
208 lines (186 loc) · 5.72 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//TODO: During next major version bump change to /dist. Leaving at ./form-validator for backwards
//compatibility
const DIST_DIR = './form-validator';
const MAIN_PLUGIN_FILE = 'jquery.form-validator';
const SRC_DIR = './src';
const MAIN_DIR = '/main/';
const MODULE_DIR = '/modules';
const LANG_DIR = '/lang';
const CSS_FILE = 'theme-default.css';
const CORE_VALIDATORS = 'core-validators.js'; //must come at end of concatted file
const coreValidatorsPath = SRC_DIR + MAIN_DIR + CORE_VALIDATORS;
var fs = require('fs'),
readFile = function (file) {
return fs.readFileSync(file, 'utf-8');
},
replaceInFile = function (path, from, to) {
fs.writeFileSync(path, readFile(path).replace(from, to));
};
function initializeGruntConfig(grunt) {
grunt.initConfig({
// Import package manifest
pkg: grunt.file.readJSON("package.json"),
// Banner definitions
meta: {
banner: "/** File generated by Grunt -- do not modify\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: {
main:{
files: [
//This concatenates the core validators file after the other files
//Per: http://gruntjs.com/configuring-tasks
{
src: [SRC_DIR + MAIN_DIR+'*.js', '!' + coreValidatorsPath, coreValidatorsPath],
dest: DIST_DIR + '/' + MAIN_PLUGIN_FILE + '.js'
},
{
src: [SRC_DIR + MAIN_DIR+'*.js', '!' + coreValidatorsPath, coreValidatorsPath],
dest: DIST_DIR + '/' + MAIN_PLUGIN_FILE + '.min.js'
}]
},
options: {
banner: "<%= meta.banner %>"
}
},
cssmin: {
target: {
files: [
{
dest: DIST_DIR,
src: CSS_FILE,
cwd: SRC_DIR,
expand: true,
ext: '.min.css'
}
]
}
},
// Lint definitions
jshint: {
files: [SRC_DIR + '/*'],
options: {
jshintrc: ".jshintrc",
ignores: [SRC_DIR + '/' + CSS_FILE]
}
},
// Minify definitions
uglify: {
options: {
banner: "<%= meta.banner %>"
},
main: {
files: [
{
expand: true,
cwd: DIST_DIR + '/',
src: ['**/*.js', '!' + MAIN_PLUGIN_FILE +'.js'],
dest: DIST_DIR + '/'
}
]
}
},
watch: {
files: [SRC_DIR + '/**'],
tasks: ['test'],
options: {
nospawn: true,
livereload: true
}
},
// Unit tests
qunit: {
all: ['test/qunit.html']
},
// Standalone servers
connect: {
server: {
options: {
port: 8000,
base: '.'
}
}
},
copy: {
main: {
files: [
{
src: SRC_DIR + '/' + CSS_FILE,
dest: DIST_DIR + '/' + CSS_FILE
},
{
cwd: SRC_DIR + '/' + MODULE_DIR,
src: '**',
dest: DIST_DIR + '/',
expand: true
},
{
cwd: SRC_DIR + '/' + LANG_DIR,
src: '**',
dest: DIST_DIR + LANG_DIR +'/',
expand: true
}]
}
},
clean: [DIST_DIR + '/'],
umd: {
main: {
options: {
src: DIST_DIR + '/**/*.js',
dest: './',
deps: {
default: ['jQuery'],
amd: [{'jquery': 'jQuery'}],
cjs: [{'jquery': 'jQuery'}]
}
}
}
}
});
}
module.exports = function (grunt) {
initializeGruntConfig(grunt);
/*
* Change to new version or the next version number. The project must be built again after this task
* in order for the version change to take effect.
*/
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);
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-clean');
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks('grunt-contrib-copy');
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.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-umd');
grunt.registerTask("build-production", ["version", "test", "uglify"]);
grunt.registerTask('build', ['concat', 'copy', 'umd', 'cssmin']);
grunt.registerTask('test', ['build','jshint', 'qunit']);
grunt.registerTask("default", ["test", "connect", "watch"]);
grunt.registerTask("prepublish", ["test", "uglify"]);
};