forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageReadme.ts
More file actions
83 lines (71 loc) · 2.74 KB
/
packageReadme.ts
File metadata and controls
83 lines (71 loc) · 2.74 KB
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
import { existsSync, readdirSync, readFileSync, writeFileSync } from "fs";
import path from "path";
import { parseHeader } from "./header-parser";
import { readmeFilenames, scripts, scriptsPath } from "./utils";
function isValidHttpUrl(string: string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
function makeReadme (script: string) {
const indexJs = path.resolve(scriptsPath, script, 'index.js');
const indexTs = path.resolve(scriptsPath, script, 'index.ts');
const readmePath = path.resolve(scriptsPath, script, 'README.md');
const readmeDefault = [
'# ' + script,
'',
'## Description',
'> This README is auto generated, Edit the README so that users know what this package does.',
'',
'## Credits',
'',
''
];
if (!existsSync(indexJs) && !existsSync(indexTs)) {
console.error(script, "missing index.js and index.ts");
writeFileSync(readmePath, readmeDefault.join('\n'));
return;
};
const indexfile = existsSync(indexTs) ? indexTs : indexJs;
/**
* header
*/
const header = parseHeader(readFileSync(indexfile).toString());
if ('contributors' in header) {
const credits = "These scripts were written by " + header.contributors.map((v) => {
if (isValidHttpUrl(v.url)) return `[${v.name}](${v.url})`;
else return `${v.name} on ${v.url}`;
}).join(', ');
const creditsSubheadingIndex = readmeDefault.findIndex((v) => v.startsWith('## Credits'));
readmeDefault[creditsSubheadingIndex + 1] = credits;
writeFileSync(readmePath, readmeDefault.join('\n'));
}
else {
console.error(script, "doesn't have header in index file");
writeFileSync(readmePath, readmeDefault.join('\n'));
}
}
export function execute (): 0 | 1 {
const scriptsChanged: string[] = [];
for (const script of scripts) {
const files = readdirSync(path.resolve(scriptsPath, script));
// check if readme is in directory
let hasReadme = false;
for (const readmeFile of readmeFilenames) {
if (files.map(v => v.toLowerCase()).includes(readmeFile)) hasReadme = true;
};
if (hasReadme) continue;
console.log(`Script '${script}' does not have README. Adding one automatically.`);
makeReadme(script);
scriptsChanged.push(script);
}
// attempt to commit
if (scriptsChanged.length > 0 && process.env.event_name === 'schedule') console.warn(`Add README to ${scriptsChanged.length} packages.`);
else if (scriptsChanged.length > 0) console.error(`There are ${scriptsChanged.length} packages don't have README. Add a README to your package so that users know how to get started.`);
else console.log("All script packages have a README file.");
return 0;
}