forked from johnagan/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-readme.js
72 lines (64 loc) · 2.52 KB
/
generate-readme.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
/**
* adds content to the repos README.md file
*/
const path = require('path');
const url = require('url');
const markdownMagic = require('markdown-magic');
const remoteRequest = require('markdown-magic/lib/transforms/remote').remoteRequest;
function toTitleCase(str) {
return str.replace(/\w\S*/g, txt =>
txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
}
function formatPluginName(string) {
return toTitleCase(string.replace(/-/g, ' '));
}
function username(repo) {
if (!repo) {
return null;
}
const o = url.parse(repo);
var urlPath = o.path; // eslint-disable-line
if (urlPath.length && urlPath.charAt(0) === '/') {
urlPath = urlPath.slice(1);
}
urlPath = urlPath.split('/')[0];
return urlPath;
}
const config = {
transforms: {
GENERATE_SERVERLESS_EXAMPLES_TABLE(content, options) { // eslint-disable-line
const examplesUrl = 'https://raw.githubusercontent.com/serverless/examples/master/community-examples.json';
const remoteContent = remoteRequest(examplesUrl);
var md = '| Project Name | Author |\n'; // eslint-disable-line
md += '|:-------------|:------:|\n';
JSON.parse(remoteContent).forEach((data) => {
const userName = username(data.githubUrl);
const profileURL = `http://github.com/${userName}`;
md += `| **[${formatPluginName(data.name)}](${data.githubUrl})** <br/>`;
md += ` ${data.description} | [${userName}](${profileURL}) | \n`;
});
return md.replace(/^\s+|\s+$/g, '');
},
GENERATE_SERVERLESS_PLUGIN_TABLE(content, options) { // eslint-disable-line
const pluginUrl = 'https://raw.githubusercontent.com/serverless/plugins/master/plugins.json';
const remoteContent = remoteRequest(pluginUrl);
var md = '| Plugin | Author |\n'; // eslint-disable-line
md += '|:-------|:------:|\n';
JSON.parse(remoteContent).sort((a, b) => // eslint-disable-line
a.name < b.name ? -1 : 1
).forEach((data) => {
const userName = username(data.githubUrl);
const profileURL = `http://github.com/${userName}`;
md += `| **[${formatPluginName(data.name)}](${data.githubUrl})** <br/>`;
md += ` ${data.description} | [${userName}](${profileURL}) | \n`;
});
return md.replace(/^\s+|\s+$/g, '');
},
},
};
const markdownPath = path.join(__dirname, '..', 'README.md');
// const markdownPath = path.join(__dirname, '..', 'test/fixtures/test.md')
markdownMagic(markdownPath, config, () => {
console.log(`${markdownPath} updated!`); // eslint-disable-line
});