-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
194 lines (174 loc) · 6.28 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
module.exports = function(grunt) {
var settings;
var themeFolder;
var defaultSettings = {
'THEME_NAME': 'mytheme',
'DISQUS_SHORTNAME': 'example',
'GOOGLE_ANALYTICS_CODE': 'UA-XXXXXXX-XX',
'GOOGLE_ANALYTICS_DOMAIN': 'example.com'
};
if (grunt.file.exists('theme/settings.json')) {
settings = grunt.file.readJSON('theme/settings.json');
if (typeof settings.THEME_FOLDER === 'undefined' || settings.THEME_FOLDER.length === 0) {
settings.THEME_FOLDER = settings.THEME_NAME.replace(/[\W\.]+/g, "");
}
themeFolder = settings.THEME_FOLDER;
settings.hasGoogleAnalytics = typeof settings.GOOGLE_ANALYTICS_CODE !== 'undefined'
&& settings.GOOGLE_ANALYTICS_CODE !== null
&& typeof settings.GOOGLE_ANALYTICS_DOMAIN !== 'undefined'
&& settings.GOOGLE_ANALYTICS_CODE !== defaultSettings.GOOGLE_ANALYTICS_CODE
&& settings.GOOGLE_ANALYTICS_DOMAIN !== defaultSettings.GOOGLE_ANALYTICS_DOMAIN;
settings.hasDisqus = typeof settings.DISQUS_SHORTNAME !== 'undefined'
&& settings.DISQUS_SHORTNAME !== defaultSettings.DISQUS_SHORTNAME;
}
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy: {
create: {
files: [
{
cwd: 'themebase',
expand: true,
src: ['**'],
dest: 'theme/',
filter: function(filepath) {
var path = require('path');
// Setup our target path.
var dest = path.join(
'theme',
// Remove the parent source directory from filepath
filepath.split(path.sep).slice(1).join(path.sep)
);
// Don't copy analytics partial if it is not setup in the settings.
if (filepath.indexOf('partials/gauniversal.hbs') > -1
&& !settings.hasGoogleAnalytics) {
grunt.log.writeln('Skipping anlytics partials: not setup in the settings.json.');
return false;
}
if (filepath.indexOf('partials/disqus.hbs') > -1
&& !settings.hasDisqus) {
grunt.log.writeln('Skipping Disqus partial: not setup in theme/settigns.json.');
return false;
}
return !(grunt.file.exists(dest));
}
}
],
options: {
process: function(content, srcpath) {
if (srcpath.indexOf('partials/gauniversal.hbs') > -1) {
grunt.log.writeln('Processing file: ' + srcpath);
return content
.replace('[GOOGLEANALYTICSCODE]', settings.GOOGLE_ANALYTICS_CODE)
.replace('[GOOGLEANALYTICSDOMAIN]', settings.GOOGLE_ANALYTICS_DOMAIN);
}
if (srcpath.indexOf('partials/disqus.hbs') > -1) {
grunt.log.writeln('Processing file: ' + srcpath);
return content
.replace('[DISQUSSHORTNAME]', settings.DISQUS_SHORTNAME);
}
if (srcpath.indexOf('default.hbs') > -1 && settings.hasGoogleAnalytics) {
return content
.replace('{{!> gauniversal}}', '{{> gauniversal}}');
}
if (srcpath.indexOf('post.hbs') > -1 && settings.hasDisqus) {
return content
.replace('{{!> disqus}}', '{{> disqus}}');
}
return content;
}
}
},
dist: {
files: [
{
expand: true,
src: 'theme/**',
dest: themeFolder
}
]
}
},
sass: {
dist: {
files: {
'theme/assets/css/post.css': 'theme/assets/css/post.scss',
'theme/assets/css/screen.css': 'theme/assets/css/screen.scss'
},
options: {
style: 'compressed'
}
},
dev: {
files: {
'theme/assets/css/post.css': 'theme/assets/css/post.scss',
'theme/assets/css/screen.css': 'theme/assets/css/screen.scss'
},
options: {
style: 'expanded',
lineNumbers: true
}
}
},
watch: {
sass: {
files: ['hover/assets/css/*.scss'],
tasks: ['sass:dev'],
options: {
livereload: true
}
}
}
});
grunt.registerTask('default', function() {
grunt.log.writeln('Main commands:');
grunt.log.writeln(' grunt init Creates a settings file for setting up your theme configuration');
grunt.log.writeln(' grunt create Creates the theme work area');
grunt.log.writeln(' grunt build Creates you theme in a distributable format with compressesd CSS & HTML');
grunt.log.writeln('');
grunt.log.writeln('View the README.md for more info.');
});
grunt.registerTask('build', [
'sass:dist',
'copy:dist'
]);
grunt.registerTask('init', function() {
if (grunt.file.exists('theme') === false) {
grunt.log.writeln('Creating `theme` folder.');
grunt.file.mkdir('theme');
} else {
grunt.log.writeln('Skipping creating `theme` folder (already exists).');
}
if (grunt.file.exists('theme/settings.json') === false) {
grunt.log.writeln('Creating `theme/settings.json`.');
grunt.file.copy('settings.example.json', 'theme/settings.json');
} else {
grunt.log.writeln('Skipping creating `settings.json` file (already exists).');
}
});
grunt.registerTask('checkReadyForCreate', function() {
// Test to make sure that we have the settings we need.
if (typeof settings === 'undefined' || settings === null) {
grunt.fatal('`grunt init` must be run before trying to create the theme.');
}
if (typeof settings.THEME_NAME === 'undefined'
|| settings.THEME_NAME === defaultSettings.THEME_NAME) {
grunt.fatal('You must give your theme a name to create it. Set your theme name in theme/settings.json.');
}
});
grunt.registerTask('create', [
'checkReadyForCreate',
'copy:create'
]);
grunt.registerTask('server', function (target) {
grunt.task.run([
'sass:dev',
'watch'
]);
});
};