-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGruntfile.js
151 lines (120 loc) · 4.46 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
const md5 = require('md5');
module.exports = function(grunt) {
'use strict';
//------------------------------------------------------------------------------
// Cheatsheet Publish
//------------------------------------------------------------------------------
function preBuildSanityCheck() {
if (! grunt.file.exists('public/js/admin.min.js')) {
grunt.fail.warn('Could not find public/js/admin.min.js! Aborting...');
}
if (! grunt.file.exists('public/js/client.min.js')) {
grunt.fail.warn('Could not find public/js/client.min.js! Aborting...');
}
grunt.log.writeln('Everything looks ok for a build.');
}
// TODO: re-write this using a data structure
function hashAssets() {
const adminCssFile = grunt.file.read('00-publish/css/admin.min.css');
const adminCssHash = md5(adminCssFile).substr(0, 8);
const clientCssFile = grunt.file.read('00-publish/css/client.min.css');
const clientCssHash = md5(clientCssFile).substr(0, 8);
const adminJsFile = grunt.file.read('00-publish/js/admin.min.js');
const adminJsHash = md5(adminJsFile).substr(0, 8);
const clientJsFile = grunt.file.read('00-publish/js/client.min.js');
const clientJsHash = md5(clientJsFile).substr(0, 8);
const adminHtmlFile = '00-publish/admin/index.html';
const adminHtmlFileContents = grunt.file.read(adminHtmlFile);
const clientHtmlFile = '00-publish/index.html';
const clientHtmlFileContents = grunt.file.read(clientHtmlFile);
// write the new files
grunt.file.write('00-publish/css/admin.min.' + adminCssHash + '.css', adminCssFile);
grunt.file.write('00-publish/css/client.min.' + clientCssHash + '.css', clientCssFile);
grunt.file.write('00-publish/js/admin.min.' + adminJsHash + '.js', adminJsFile);
grunt.file.write('00-publish/js/client.min.' + clientJsHash + '.js', clientJsFile);
// delete the old files
grunt.file.delete('00-publish/css/admin.min.css');
grunt.file.delete('00-publish/css/client.min.css');
grunt.file.delete('00-publish/js/admin.min.js');
grunt.file.delete('00-publish/js/client.min.js');
// update the HTML files
grunt.file.write(adminHtmlFile,
adminHtmlFileContents.replace('admin.min.css', 'admin.min.' + adminCssHash + '.css')
.replace('admin.min.js', 'admin.min.' + adminJsHash + '.js'));
grunt.file.write(clientHtmlFile,
clientHtmlFileContents.replace('client.min.css', 'client.min.' + clientCssHash + '.css')
.replace('client.min.js', 'client.min.' + clientJsHash + '.js'));
// show some output
grunt.log.writeln('00-publish/css/admin.min.css → ' +
'00-publish/css/admin.min.' + adminCssHash + '.css');
grunt.log.writeln('00-publish/css/client.min.css → ' +
'00-publish/css/client.min.' + clientCssHash + '.css');
grunt.log.writeln('00-publish/js/admin.min.js → ' +
'00-publish/js/admin.min.' + adminJsHash + '.js');
grunt.log.writeln('00-publish/js/client.min.js → ' +
'00-publish/js/client.min.' + clientJsHash + '.js');
}
//------------------------------------------------------------------------------
// Grunt Config
//------------------------------------------------------------------------------
grunt.initConfig({
clean: {
options: {
force: true
},
// remove all the files in the 00-publish folder
pre: ['00-publish'],
// remove some dev files
post: [
'00-publish/js/admin-dev.js',
'00-publish/js/client-dev.js',
'00-publish/tournament.json'
]
},
copy: {
publish: {
files: [
{expand: true, cwd: 'public/', src: ['**'], dest: '00-publish/'}
]
}
},
less: {
options: {
compress: true
},
watch: {
files: {
'public/css/admin.min.css': 'less/admin.less',
'public/css/client.min.css': 'less/client.less'
}
}
},
watch: {
options: {
atBegin: true
},
less: {
files: "less/*.less",
tasks: "less:watch"
}
}
});
// load tasks from npm
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
// custom tasks
grunt.registerTask('pre-build-sanity-check', preBuildSanityCheck);
grunt.registerTask('hash-assets', hashAssets);
grunt.registerTask('build', [
'pre-build-sanity-check',
'clean:pre',
'less',
'copy:publish',
'hash-assets',
'clean:post'
]);
grunt.registerTask('default', 'watch');
// end module.exports
};