-
Notifications
You must be signed in to change notification settings - Fork 54
/
Gruntfile.js
191 lines (176 loc) · 5.35 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
const sass = require('node-sass');
var CONTENT_PATH_PREFIX = "content/";
// define the grunt function for cli
module.exports = function(grunt) {
grunt.initConfig({
concurrent: {
options: {
logConcurrentOutput: true,
},
target: ['hugo-server', 'watch'],
},
env : {
options : {
add : {
HUGO_VERSION : '0.101.0'
}
},
dev : {}
},
pkg: grunt.file.readJSON('package.json'),
postcss: {
develop: {
options: {
map: true,
processors: [
require('autoprefixer')(),
require('cssnano')(),
],
},
src: ['static/stylesheets/sensu.css', 'static/stylesheets/offline.css'],
},
dist: {
options: {
map: false,
processors: [
require('autoprefixer')(),
require('cssnano')(),
],
},
src: 'static/stylesheets/sensu.css',
},
},
sass: {
develop: {
files: {
'static/stylesheets/sensu.css': 'static/stylesheets/scss/main.scss',
'static/stylesheets/offline.css': 'static/stylesheets/scss/offline.scss',
},
options: {
implementation: sass,
sourceMap: true,
}
},
dist: {
files: {
'static/stylesheets/sensu.css': 'static/stylesheets/scss/main.scss',
},
options: {
implementation: sass,
sourceMap: false,
},
},
},
watch: {
css: {
files: ['static/stylesheets/**/*.scss'],
tasks: ['sass:develop', 'postcss:develop'],
},
},
})
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-postcss');
const getHugoArgs = () => {
return grunt.option.flags().filter(a => a !== '--color').filter(a => !a.startsWith("--gruntfile"));
}
grunt.registerTask("hugo-build", function() {
const done = this.async();
const args = getHugoArgs();
const toml = require('toml');
const config = toml.parse(grunt.file.read('config.toml'));
const latestProducts = Object.entries(config.params.products).map(([key, product]) => ({
identifier: product.identifier,
latest: product.latest
}));
// Move latest product versions to content/product/latest
grunt.log.writeln(`Moving latest product versions → content/product/latest`);
latestProducts.forEach(product => {
grunt.file.copy(`content/${product.identifier}/${product.latest}`, `content/${product.identifier}/latest`);
grunt.file.delete(`content/${product.identifier}/${product.latest}`);
});
grunt.log.writeln("Running hugo build");
grunt.util.spawn({
cmd: "hugo",
args: [...args],
},
function(error, result, code) {
// Restore directories
grunt.log.writeln('Restoring moved product directories...');
latestProducts.forEach(product => {
grunt.file.copy(`content/${product.identifier}/latest`, `content/${product.identifier}/${product.latest}`);
grunt.file.delete(`content/${product.identifier}/latest`);
});
if (code == 0) {
grunt.log.ok("Successfully built site");
} else {
grunt.fail.fatal(error);
}
done();
});
});
grunt.registerTask("hugo-build-offline", function() {
const done = this.async();
const toml = require('toml');
const config = toml.parse(grunt.file.read('config.toml'));
const latestProducts = Object.entries(config.params.products).map(([key, product]) => ({
identifier: product.identifier,
latest: product.latest
}));
grunt.log.writeln("Running hugo build");
grunt.util.spawn(
{
cmd: "hugo",
args: ['--layoutDir=offline'],
},
function(error, result, code) {
if (code == 0) {
grunt.log.ok("Successfully built site");
} else {
grunt.fail.fatal(error);
}
done();
}
);
});
grunt.registerTask("hugo-server", function() {
const done = this.async();
const args = getHugoArgs();
console.debug({ args });
grunt.log.writeln("Running Hugo server");
grunt.util.spawn({
cmd: "hugo",
args: ["server", "--disableFastRender", ...args], // pass arguments down
opts: {stdio: 'inherit'}
},
function(error, result, code) {
if (code == 0) {
grunt.log.ok("Thanks for using Hugo!");
} else {
grunt.fail.fatal(error);
}
done();
});
});
grunt.registerTask("print-hugo-version", function() {
const done = this.async();
grunt.util.spawn({
cmd: "hugo",
args: ["version"],
opts: {stdio: 'inherit'}
},
function(error, result, code) {
if (code == 0) {
} else {
grunt.fail.fatal(error);
}
done();
});
});
grunt.registerTask("default", ["env", "hugo-version", "sass:dist", "postcss:dist", "hugo-build",]);
grunt.registerTask("build-offline", ["env", "hugo-version", "sass:dist", "postcss:dist", "hugo-build-offline",]);
grunt.registerTask("server", ["env", "hugo-version", "sass:develop", "postcss:develop", "concurrent:target"]);
grunt.registerTask("hugo-version", ["env", "print-hugo-version",]);
};