Skip to content

Commit 9e37ab1

Browse files
author
Stephan Köninger
committed
chore(yaml): read yml file
1 parent 270a654 commit 9e37ab1

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,17 @@ const r = gitHubConnection.repos.getForOrg({org: ORG})
4141
createPostForEachRepository(reposNames);
4242
});
4343

44-
45-
4644
function createPostForEachRepository(names) {
4745
const name = names.pop();
4846
console.log("create post for: ", name);
4947
return github.getServiceInfo(name)
5048
.then(r => fs.writeFileSync(`./source/_posts/${name}.md`, HexoHelper.createPost(r), 'utf8'))
51-
.then(()=>{
52-
if(names.length === 0){
49+
.then(() => {
50+
if (names.length === 0) {
5351
console.log("finished");
54-
}else {
52+
} else {
5553
createPostForEachRepository(names);
5654
}
57-
});
55+
})
56+
.catch(err => console.error(err));
5857
}
59-
60-
// services.forEach(name => {
61-
// github.getServiceInfo(name)
62-
// .then(r => fs.writeFileSync(`./source/_posts/${name}.md`, HexoHelper.createPost(r), 'utf8'));
63-
// });

lib/hexoHelper.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,52 @@ module.exports = class HexoHelper {
99
post.push(`version: ${r.version}`);
1010
}
1111

12-
if(r.serviceTypes) {
12+
if(HexoHelper.propertyIsSetAndArray(r.serviceTypes)) {
1313
post.push(`serviceTypes:`);
1414
r.serviceTypes.forEach(st => post.push(` - ${st}`))
1515
}
1616

17-
if(r.contributors) {
17+
if(HexoHelper.propertyIsSetAndArray(r.contributors)) {
1818
post.push(`contributors:`);
1919
r.contributors.forEach(c => post.push(` - ${c}`))
2020
}
2121

22-
if(r.responsibleUsers) {
22+
if(HexoHelper.propertyIsSetAndArray(r.responsibleUsers)) {
2323
post.push(`responsible:`);
2424
r.responsibleUsers.forEach(c => post.push(` - ${c}`))
2525
}
2626

27-
if(r.languages) {
27+
if(HexoHelper.propertyIsSetAndArray(r.languages)) {
2828
post.push(`languages:`);
2929
r.languages.forEach(c => post.push(` - ${c}`))
3030
}
3131

32+
if(HexoHelper.propertyIsSetAndArray(r.uses)) {
33+
post.push(`uses:`);
34+
r.uses.forEach(c => post.push(` - ${c}`))
35+
}
36+
37+
if(r.message) {
38+
post.push(`message:`);
39+
if(Array.isArray(r.message.send) && r.message.send.length > 0) {
40+
post.push(` send:`);
41+
r.message.send.forEach(c => post.push(` - ${c}`))
42+
}
43+
if(Array.isArray(r.message.consume) && r.message.consume.length > 0) {
44+
post.push(` consume:`);
45+
r.message.consume.forEach(c => post.push(` - ${c}`))
46+
}
47+
}
48+
3249
post.push("---");
3350
if(r.readme) {
3451
post.push('');
3552
post.push(r.readme);
3653
}
3754
return post.join('\n');
3855
}
56+
57+
static propertyIsSetAndArray(p) {
58+
return Array.isArray(p) && p.length > 0;
59+
}
3960
};

lib/serviceInfo.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
'use strict';
1+
const YAML = require('yamljs');
2+
23
module.exports = class ServiceInfo {
34
constructor(name) {
45
this.name = name;
@@ -36,12 +37,14 @@ module.exports = class ServiceInfo {
3637
* @returns {ServiceInfo}
3738
*/
3839
setLanguages(languages) {
39-
const languageKeys = Object.keys(languages).filter(key => key !== 'meta');
40-
const sum = languageKeys.map(lang => languages[lang]).reduce((prev, cur) => prev + cur, 0);
41-
languageKeys.forEach(lang => {
42-
languages[lang] = Math.ceil((languages[lang] / sum) * 100)
43-
});
44-
this.languages = Object.keys(languages).filter(key => key !== 'meta' && languages[key] >= 15);
40+
if(languages) {
41+
const languageKeys = Object.keys(languages).filter(key => key !== 'meta');
42+
const sum = languageKeys.map(lang => languages[lang]).reduce((prev, cur) => prev + cur, 0);
43+
languageKeys.forEach(lang => {
44+
languages[lang] = Math.ceil((languages[lang] / sum) * 100)
45+
});
46+
this.languages = Object.keys(languages).filter(key => key !== 'meta' && languages[key] >= 15);
47+
}
4548
return this;
4649
}
4750

@@ -50,7 +53,7 @@ module.exports = class ServiceInfo {
5053
* @returns {ServiceInfo}
5154
*/
5255
setContributors(contributors) {
53-
if(Array.isArray(contributors)) {
56+
if (Array.isArray(contributors)) {
5457
this.contributors = contributors.filter(c => c.total !== undefined)
5558
.sort((a, b) => b.total - a.total)
5659
.map(a => `${a.author.login}`).slice(0, 4);
@@ -63,13 +66,19 @@ module.exports = class ServiceInfo {
6366
* @returns {ServiceInfo}
6467
*/
6568
setOverrides(overrides) {
66-
if(overrides && overrides.content) {
69+
if (overrides && overrides.content) {
6770
try {
68-
const jsonStr = new Buffer(overrides.content, overrides.encoding).toString();
69-
overrides = JSON.parse(jsonStr);
71+
const content = new Buffer(overrides.content, overrides.encoding).toString();
72+
overrides = YAML.parse(content);
73+
74+
this.message = overrides.message ? overrides.message : undefined;
75+
this.uses = this._getAsArray(overrides.uses);
76+
this.contributors = this._getAsArray(overrides.contributors);
7077
this.responsibleUsers = this._getAsArray(overrides.responsibleUsers);
7178
this.serviceTypes = this._getAsArray(overrides.serviceTypes);
72-
} catch(e) {}
79+
} catch (e) {
80+
console.error("setOverrides", e);
81+
}
7382
}
7483

7584
return this;
@@ -81,7 +90,7 @@ module.exports = class ServiceInfo {
8190
* @private
8291
*/
8392
_getAsArray(what) {
84-
if(what) {
93+
if (what) {
8594
return Array.isArray(what) ? what : [what];
8695
}
8796
}

lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module.exports = class Util {
9191
}
9292

9393
getServiceDocFile(config) {
94-
return this._getFile(config, '.service.doc');
94+
return this._getFile(config, '.service.yml');
9595
}
9696

9797
_getFile(config, path) {

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"server": "hexo server"
1111
},
1212
"dependencies": {
13+
"bluebird": "^3.4.6",
14+
"github": "^5.2.1",
1315
"hexo": "^3.2.0",
1416
"hexo-generator-archive": "^0.1.4",
1517
"hexo-generator-category": "^0.1.3",
@@ -19,8 +21,7 @@
1921
"hexo-renderer-marked": "^0.2.10",
2022
"hexo-renderer-stylus": "^0.3.1",
2123
"hexo-server": "^0.2.0",
22-
"bluebird": "^3.4.6",
23-
"github": "^5.2.1",
24-
"nconf": "^0.8.4"
24+
"nconf": "^0.8.4",
25+
"yamljs": "^0.2.8"
2526
}
2627
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<li>
22
<a href="<%- config.root %><%- service.path %>">
3-
<%- service.title || service.link%>
3+
<%- service.slug %>
44
</a>
55
</li>
66

themes/service-docs/layout/layout.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
<body>
1212
<div class="wrapper">
1313
<nav>
14-
<h2>Services</h2>
14+
<h2>Services <small>(<%- site.posts.length %>)</small></h2>
1515
<ul class="service-list">
1616
<% var services = [] %>
1717
<% site.posts.each(function(service){ %>
1818
<% services.push(service) %>
1919
<% }); %>
20-
<% services.sort((a,b) => a.title.localeCompare(b.title)); %>
20+
<% services = services.sort((a,b) => a.slug.localeCompare(b.slug)); %>
2121
<% services.forEach(function(service) { %>
2222
<%- partial('_partial/service-list-item', {service: service}) %>
2323
<% }); %>

0 commit comments

Comments
 (0)