This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
forked from serverless/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-docs.js
74 lines (64 loc) · 2.51 KB
/
generate-docs.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
/**
* This example generated adds content to the repos README.md file
*/
const fs = require('fs')
const url = require('url')
const path = require('path')
const markdownMagic = require('markdown-magic')
const commonPartRe = /(?:(?:^|-)serverless-plugin(?:-|$))|(?:(?:^|-)serverless(?:-|$))/;
const config = {
transforms: {
/*
In readme.md the below comment block adds the list to the readme
<!-- AUTO-GENERATED-CONTENT:START (GENERATE_SERVERLESS_PLUGIN_TABLE)-->
plugin list will be generated here
<!-- AUTO-GENERATED-CONTENT:END -->
*/
GENERATE_SERVERLESS_PLUGIN_TABLE: function(content, options) {
const commandsFile = path.join(__dirname, 'plugins.json')
const plugins = JSON.parse(fs.readFileSync(commandsFile, 'utf8'))
let md = '| Plugin | Stats |\n'
md += '|:---------------------------|:-----------:|\n'
plugins.sort(function (a, b) {
const aName = a.name.toLowerCase();
const bName = b.name.toLowerCase();
return aName.replace(commonPartRe, '').localeCompare(bName.replace(commonPartRe, '')) ||
aName.localeCompare(bName);
}).forEach(function(data) {
const userName = username(data.githubUrl)
const profileURL = `http://github.com/${userName}`
const repoName = data.githubUrl.split('.com/')[1];
md += `| **[${formatPluginName(data.name)} - \`${data.name.toLowerCase()}\`](${data.githubUrl})** <br/> by [${userName}](${profileURL}) <br/>`
md += ` ${data.description} | `
md += `![Github Stars](https://img.shields.io/github/stars/${repoName}.svg?label=Stars&style=for-the-badge) <br/> ![NPM Downloads](https://img.shields.io/npm/dt/${data.name}.svg?label=Downloads&style=for-the-badge)|\n`
});
return md.replace(/^\s+|\s+$/g, '')
}
}
}
function username(repo) {
if (!repo) {
return null;
}
var o = url.parse(repo);
var path = o.path;
if (path.length && path.charAt(0) === '/') {
path = path.slice(1);
}
path = path.split('/')[0];
return path;
}
function formatPluginName (string) {
return toTitleCase(string.toLowerCase().replace(commonPartRe, '').replace(/-/g, ' '))
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
)
}
const markdownPath = path.join(__dirname, 'README.md')
// const markdownPath = path.join(__dirname, '..', 'test/fixtures/test.md')
markdownMagic(markdownPath, config, function() {
console.log('Docs updated!')
})