This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcredits.js
executable file
·135 lines (119 loc) · 3.57 KB
/
credits.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
#!/usr/bin/env node
/*
@license
@a11ygato/platform
Copyright (C) 2018 Orange
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const util = require('util');
const fs = require('fs');
const meow = require('meow');
const Listr = require('listr');
const path = require('path');
const chalk = require('chalk');
var checker = require('license-checker');
const cli = meow(`
Usage
$ ./credits.js
Options
-h Show help
Examples
$ ./credits.js
`, {
description:'Generate CREDITS.md for each module',
flags:{
help:{
type:'boolean',
alias:'h'
}
}
});
if(cli.flags.help) return cli.showHelp(0);
const writeFile = util.promisify(fs.writeFile);
const checkLicences = util.promisify(checker.init);
const tasks = new Listr([
{
title:'a11ygato-platform',
task:() => generateCredits()
},
{
title:'@a11ygato/audit-engine',
task:() => generateCredits(path.join('modules', 'audit-engine'))
},
{
title:'@a11ygato/cli',
task:() => generateCredits(path.join('modules', 'cli'))
},
{
title: '@a11ygato/dashboard',
task: () => generateCredits(path.join('modules', 'dashboard'))
},
{
title:'@a11ygato/webservice',
task:() => generateCredits(path.join('modules', 'webservice'))
}
]);
tasks.run().catch(err => {
console.log();
console.error(chalk.red(err && err.message || err));
});
/////////////////
async function generateCredits(folder = '.') {
const dependencies = await checkLicences({ start:folder });
const keys = Object.keys(dependencies);
let buffer = `# Credits (${keys.length})\n\n`;
let currentLicense = '';
keys.map(key => {
const dep = dependencies[key];
dep.key = key;
return dep;
}).sort((a, b) => {
if(a.licenses < b.licenses){
return -1;
}
if(a.licenses > b.licenses){
return 1;
}
if(a.licenses === b.licenses){
if(a.publisher < b.publisher){
return -1;
}
if(a.publisher > b.publisher){
return 1;
}
}
return 0;
}).forEach(dep => {
if(dep.licenses !== currentLicense) buffer += `## ${dep.licenses}\n\n`;
currentLicense = dep.licenses;
// console.log('dep:', dep);
buffer += '- ';
if(dep.repository){
buffer += `[${dep.key}](${dep.repository})`;
} else{
buffer += `${dep.key}`;
}
if(dep.publisher){
if(dep.email){
buffer += ` from [${dep.publisher}](mailto:${dep.email})`;
} else{
buffer += ` from ${dep.publisher}`;
}
}
// if(dep.licenses){
// buffer += ` (${dep.licenses})`;
// }
buffer += '\n';
});
return writeFile(path.join(folder, 'CREDITS.md'), buffer, 'utf8');
}