Skip to content

Commit 47c2b40

Browse files
committed
Build: General cleanup and removal of grunt-specific APIs
1 parent 09bb584 commit 47c2b40

File tree

2 files changed

+86
-118
lines changed

2 files changed

+86
-118
lines changed

grunt.js

+84-117
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var rimraf = require( "rimraf" );
1+
var rimraf = require( "rimraf" ),
2+
spawnback = require( "spawnback" );
23

34
module.exports = function( grunt ) {
45

@@ -22,150 +23,116 @@ grunt.registerTask( "clean", function() {
2223
rimraf.sync( "dist" );
2324
});
2425

25-
// Process a JSON order file and return an object of page slugs and their ordinal indices
26-
grunt.registerHelper( "read-order", function( orderFile ) {
27-
var order,
28-
map = {},
26+
function getOrderMap() {
27+
var map = {},
2928
index = 0;
3029

31-
try {
32-
order = JSON.parse( grunt.file.read( orderFile ) );
33-
} catch( error ) {
34-
grunt.warn( "Invalid order file: " + orderFile );
35-
return null;
30+
function walk( items, prefix ) {
31+
items.forEach(function( item ) {
32+
if ( typeof item === "object" ) {
33+
var page = Object.keys( item )[ 0 ];
34+
map[ prefix + page ] = ++index;
35+
walk( item[ page ], prefix + page + "/" );
36+
} else {
37+
map[ prefix + item ] = ++index;
38+
}
39+
});
3640
}
3741

42+
walk( require( "./order" ), "" );
3843

39-
function flatten( item, folder ) {
40-
var title,
41-
path = folder ? [ folder ] : [];
42-
43-
if ( grunt.utils._.isObject( item ) ) {
44-
title = Object.keys( item )[ 0 ];
45-
path.push( title );
46-
path = path.join( "/" );
47-
map[ path ] = ++index;
48-
49-
item[ title ].forEach(function( item ) {
50-
flatten( item, path );
51-
});
52-
} else {
53-
path.push( item );
54-
map[ path.join( "/" ) ] = ++index;
55-
}
56-
}
57-
order.forEach(function( item ) {
58-
flatten( item );
59-
});
6044
return map;
61-
});
45+
}
6246

63-
grunt.registerHelper( "contributor-attribution", function( post, fileName, fn ) {
64-
var contribs = [],
65-
_ = grunt.utils._,
66-
parseRE = /^(.*)<(.*)>$/; // could certainly be better.
47+
function contributorAttribution( post, fileName, callback ) {
48+
var contribs,
49+
parseRE = /^(.*)<(.*)>$/;
6750

6851
// Read contributors from git file information
69-
grunt.utils.spawn({
70-
cmd: "git",
71-
args: [
72-
"log",
73-
"--follow", // Trace history through file rename operations
74-
"--diff-filter=AM", // Only consider "Add" and "Modify" operations
75-
"--format=%aN <%aE>",
76-
fileName
77-
]
78-
}, function( err, result ) {
79-
if ( err ) {
80-
grunt.verbose.error();
81-
grunt.log.error( err );
82-
return;
52+
spawnback( "git", [
53+
"log",
54+
"--follow", // Trace history through file rename operations
55+
"--diff-filter=AM", // Only consider "Add" and "Modify" operations
56+
"--format=%aN <%aE>",
57+
fileName
58+
], function( error, result ) {
59+
if ( error ) {
60+
return callback( error );
8361
}
84-
// make unique.
85-
contribs = _.uniq( result.stdout.split( /\r?\n/g ) );
86-
87-
// make object { name: 'name', email: 'email@address.com' }
88-
contribs.forEach(function(str, idx) {
89-
var m = parseRE.exec(str);
90-
if ( m ) {
91-
contribs[idx] = { name: m[1].trim(), email: m[2] };
92-
}
93-
else {
94-
contribs[idx] = { name: str };
95-
}
96-
});
9762

98-
// Alphabetize by 'last name' (relatively crude)
99-
contribs = _.sortBy( contribs, function(a) {
100-
return a.name.split(' ').pop().toLowerCase();
101-
});
63+
contribs = result.trimRight().split( /\r?\n/g )
64+
65+
// Reduce to a unique list of contributors
66+
.filter(function( value, index, array ) {
67+
return array.indexOf( value ) === index;
68+
})
69+
70+
// Convert to structured objects
71+
.map(function( contributor ) {
72+
var matches = parseRE.exec( contributor );
73+
return {
74+
name: matches[ 1 ].trim(),
75+
email: matches[ 2 ]
76+
};
77+
})
78+
79+
// Alphabetize by 'last name' (relatively crude)
80+
.sort(function( a, b ) {
81+
return a.name.split( " " ).pop().toLowerCase() <
82+
b.name.split( " " ).pop().toLowerCase() ?
83+
-1 : 1;
84+
});
10285

10386
// Handle "legacy" content - content authored outside of the learn site
10487
// and attributed with metadata in the file,
10588
// push those contributors to the front of the list
10689
if ( post.attribution ) {
107-
post.attribution.forEach(function(str, idx) {
108-
var contrib, m;
109-
110-
// Handling specifically for articles originally from jQuery Fundamentals
111-
if (str == "jQuery Fundamentals") {
112-
contribs.unshift({
113-
name: str,
114-
// Use the jQuery Gravatar
115-
email: "github@jquery.com",
116-
source: post.source
117-
});
90+
post.attribution.forEach(function( contributor ) {
91+
var contrib, matches;
92+
93+
if ( contributor === "jQuery Fundamentals" ) {
94+
contrib = {
95+
name: "jQuery Fundamentals",
96+
email: "github@jquery.com"
97+
};
11898
} else {
119-
m = parseRE.exec(str);
120-
if ( m ) {
121-
contrib = { name: m[1].trim(), email: m[2] };
122-
}
123-
else {
124-
contrib = { name: str };
125-
}
126-
if ( post.source ) {
127-
contrib.source = post.source;
128-
}
129-
contribs.unshift( contrib );
99+
matches = parseRE.exec( contributor );
100+
contrib = {
101+
name: matches[ 1 ].trim(),
102+
email: matches[ 2 ]
103+
};
130104
}
131-
});
132-
}
133105

134-
if ( post.customFields ) {
135-
post.customFields.push({
136-
key: "contributors",
137-
value: JSON.stringify( contribs )
106+
if ( post.source ) {
107+
contrib.source = post.source;
108+
}
109+
110+
contribs.unshift( contrib );
138111
});
139-
} else {
140-
post.customFields = [{
141-
key: "contributors",
142-
value: JSON.stringify( contribs )
143-
}];
144112
}
145113

146-
fn();
147-
});
114+
post.customFields = post.customFields || [];
115+
post.customFields.push({
116+
key: "contributors",
117+
value: JSON.stringify( contribs )
118+
});
148119

149-
});
120+
callback( null );
121+
});
122+
}
150123

151124
grunt.registerHelper( "build-pages-preprocess", (function() {
152-
var orderMap = grunt.helper( "read-order", "order.json" );
125+
var orderMap = getOrderMap();
153126

154127
return function( post, fileName, done ) {
155-
grunt.utils.async.series([
156-
function applyOrder( fn ) {
157-
var slug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ),
158-
menuOrder = orderMap[ slug ];
159-
if ( menuOrder ) {
160-
post.menuOrder = menuOrder;
161-
}
162-
fn();
163-
},
128+
var slug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ),
129+
menuOrder = orderMap[ slug ];
164130

165-
function applyContribs( fn ) {
166-
grunt.helper( "contributor-attribution", post, fileName, fn );
167-
}
168-
], done );
131+
if ( menuOrder ) {
132+
post.menuOrder = menuOrder;
133+
}
134+
135+
contributorAttribution( post, fileName, done );
169136
};
170137
})());
171138

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"grunt-wordpress": "1.2.1",
2626
"grunt-jquery-content": "0.13.0",
2727
"grunt-check-modules": "0.1.0",
28-
"rimraf": "2.2.8"
28+
"rimraf": "2.2.8",
29+
"spawnback": "1.0.0"
2930
}
3031
}

0 commit comments

Comments
 (0)