forked from jquery/learn.jquery.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grunt.js
199 lines (169 loc) · 4.32 KB
/
grunt.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
var yaml = require( "js-yaml" ),
config = require("./config.json");
module.exports = function( grunt ) {
"use strict";
grunt.loadNpmTasks( "grunt-clean" );
grunt.loadNpmTasks( "grunt-html" );
grunt.loadNpmTasks( "grunt-wordpress" );
grunt.loadNpmTasks( "grunt-jquery-content" );
grunt.loadNpmTasks( "grunt-check-modules" );
grunt.initConfig({
clean: {
wordpress: "dist/"
},
htmllint: {
resources: "resources/*.html"
},
jshint: {
options: {
undef: true,
node: true
}
},
lint: {
grunt: "grunt.js"
},
watch: {
pages: {
files: "page/**",
tasks: "deploy"
}
},
"build-pages": {
all: grunt.file.expandFiles( "page/**" )
},
"build-resources": {
all: grunt.file.expandFiles( "resources/**/*" )
},
wordpress: grunt.utils._.extend({
dir: "dist/wordpress",
order: "order.yml"
}, grunt.file.readJSON( "config.json" ) )
});
// Process a YAML order file and return an object of page slugs and their ordinal indices
grunt.registerHelper( "read-order", function( orderFile ) {
var order,
map = {},
index = 0;
try {
order = yaml.load( grunt.file.read( orderFile ) );
} catch( error ) {
grunt.warn( "Invalid order file: " + orderFile );
return null;
}
function flatten( item, folder ) {
var title,
path = folder ? [ folder ] : [];
if ( grunt.utils._.isObject( item ) ) {
title = Object.keys( item )[ 0 ];
path.push( title );
path = path.join( "/" );
map[ path ] = ++index;
item[ title ].forEach(function( item ) {
flatten( item, path );
});
} else {
path.push( item );
map[ path.join( "/" ) ] = ++index;
}
}
order.forEach(function( item ) {
flatten( item );
});
return map;
});
grunt.registerHelper( "contributor-attribution", function( post, fileName, fn ) {
var contribs = [],
_ = grunt.utils._,
parseRE = /^(.*)<(.*)>$/; // could certainly be better.
// Read contributors from git file information
grunt.utils.spawn({
cmd: "git",
args: [ "log", "--format=%aN <%aE>", fileName ]
}, function( err, result ) {
if ( err ) {
grunt.verbose.error();
grunt.log.error( err );
return;
}
// make unique.
contribs = _.uniq( result.stdout.split( /\r?\n/g ) );
// make object { name: 'name', email: 'email@address.com' }
contribs.forEach(function(str, idx) {
var m = parseRE.exec(str);
if ( m ) {
contribs[idx] = { name: m[1].trim(), email: m[2] };
}
else {
contribs[idx] = { name: str };
}
});
// Alphabetize by 'last name' (relatively crude)
contribs = _.sortBy( contribs, function(a) {
return a.name.split(' ').pop().toLowerCase();
});
// Handle "legacy" content - content authored outside of the learn site
// and attributed with metadata in the file,
// push those contributors to the front of the list
if ( post.attribution ) {
post.attribution.forEach(function(str, idx) {
var contrib, m;
// Handling specifically for articles originally from jQuery Fundamentals
if (str == "jQuery Fundamentals") {
contribs.unshift({
name: str,
// Use the jQuery Gravatar
email: "github@jquery.com",
source: post.source
});
} else {
m = parseRE.exec(str);
if ( m ) {
contrib = { name: m[1].trim(), email: m[2] };
}
else {
contrib = { name: str };
}
if ( post.source ) {
contrib.source = post.source;
}
contribs.unshift( contrib );
}
});
}
if ( post.customFields ) {
post.customFields.push({
key: "contributors",
value: JSON.stringify( contribs )
});
} else {
post.customFields = [{
key: "contributors",
value: JSON.stringify( contribs )
}];
}
fn();
});
});
grunt.registerHelper( "build-pages-preprocess", (function() {
var orderMap = grunt.helper( "read-order", "order.yml" );
return function( post, fileName, done ) {
grunt.utils.async.series([
function applyOrder( fn ) {
var slug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ),
menuOrder = orderMap[ slug ];
if ( menuOrder ) {
post.menuOrder = menuOrder;
}
fn();
},
function applyContribs( fn ) {
grunt.helper( "contributor-attribution", post, fileName, fn );
}
], done );
};
})());
grunt.registerTask( "default", "wordpress-deploy" );
grunt.registerTask( "build-wordpress", "check-modules clean lint build-pages build-resources");
grunt.registerTask( "deploy", "wordpress-deploy" );
};