1 | #! /usr/bin/env node
|
2 | const badges = require("./src/badges");
|
3 | const path = require('path');
|
4 | const fs = require('fs/promises');
|
5 | const minimist = require('minimist');
|
6 | const { startPlaceholder, endPlaceholder } = require("./src/constants/strings");
|
7 | async function index() {
|
8 |
|
9 | }
|
10 |
|
11 |
|
12 | function printHelp() {
|
13 | console.log(`
|
14 | auto-badger Utility - Adds all necessary badges to your projects readme.
|
15 |
|
16 | [USAGE]
|
17 | $ auto-badger [option=value]
|
18 |
|
19 | [AVAILABLE OPTIONS ARE]
|
20 | --community-provider Pass name of community provider viz. discord, spectrum, gitter
|
21 | --community-name Name of the community of specified community provider
|
22 | --community-server-url Url to the community server (this is required only in case of discord)
|
23 | --twitter-username Pass your twitter username for twitter badges
|
24 | --help Print this help
|
25 | `);
|
26 | }
|
27 | async function cli() {
|
28 | const cliArgs = minimist(process.argv.slice(2));
|
29 | if (cliArgs.help) {
|
30 | return printHelp();
|
31 | }
|
32 | const readmePath = path.resolve(process.cwd(), 'README.md');
|
33 | const readmeBuffer = await fs.readFile(readmePath);
|
34 | const readmeContent = readmeBuffer.toString();
|
35 | const startPlaceholderIndex = readmeContent.indexOf(startPlaceholder);
|
36 | const endPlaceholderIndex = readmeContent.indexOf(endPlaceholder);
|
37 | if (startPlaceholderIndex === -1) {
|
38 | console.error("No placeholder found in readme");
|
39 | return;
|
40 | }
|
41 | let allBadges = await Promise.all([
|
42 | badges.build.generate(),
|
43 | badges.version.generate(),
|
44 | badges.dependancies.generate(),
|
45 | badges.size.generate(),
|
46 | badges.downloads.generate(),
|
47 | badges.coverage.generate(),
|
48 | badges.github.generate(),
|
49 | badges.license.generate(),
|
50 | badges.twitter.generate(cliArgs['twitter-username']),
|
51 | badges.community.generate({
|
52 | communityProvider: cliArgs['community-provider'],
|
53 | communityId: cliArgs['community-name'],
|
54 | communityServerUrl: cliArgs['community-server-url']
|
55 | })
|
56 | ]);
|
57 | [
|
58 | buildBadge,
|
59 | versionBadge,
|
60 | dependanciesBadge,
|
61 | sizeBadge,
|
62 | downloadsBadge,
|
63 | coverageBadge,
|
64 | allContributersBadge,
|
65 | codeOfConduct,
|
66 | starsBadge,
|
67 | forkBadge,
|
68 | licenseBadge,
|
69 | twitterBadge,
|
70 | communityBadge
|
71 | ] = allBadges.flat(Infinity);
|
72 | const allBadgesString = [
|
73 | [
|
74 | buildBadge,
|
75 | versionBadge,
|
76 | sizeBadge,
|
77 | coverageBadge,
|
78 | licenseBadge
|
79 | ].filter(Boolean).join("\n"),
|
80 | [
|
81 | dependanciesBadge,
|
82 | downloadsBadge,
|
83 | allContributersBadge,
|
84 | codeOfConduct
|
85 | ].filter(Boolean).join("\n"),
|
86 | [
|
87 | communityBadge,
|
88 | starsBadge,
|
89 | forkBadge,
|
90 | twitterBadge
|
91 | ].filter(Boolean).join("\n")
|
92 | ].join("\n\n");
|
93 | console.log("Generated Badges Are");
|
94 | console.log(allBadgesString);
|
95 |
|
96 | await fs.copyFile(readmePath, "readme.md.bk");
|
97 | try {
|
98 | await fs.truncate(readmePath, 0);
|
99 | await fs.appendFile(readmePath, readmeContent.slice(0, startPlaceholderIndex + startPlaceholder.length + 1));
|
100 | await fs.appendFile(readmePath, "\n\n" + allBadgesString);
|
101 | let contentToAppend;
|
102 | if (startPlaceholderIndex > -1 && endPlaceholderIndex > -1) {
|
103 | contentToAppend = readmeContent.slice(endPlaceholderIndex + endPlaceholder.length + 1, readmeContent.length);
|
104 | } else if (startPlaceholderIndex > -1) {
|
105 | contentToAppend = readmeContent.slice(startPlaceholderIndex + startPlaceholder.length + 1, readmeContent.length);
|
106 | }
|
107 |
|
108 | await fs.appendFile(readmePath, "\n\n" + endPlaceholder);
|
109 | await fs.appendFile(readmePath, "\n\n" + contentToAppend);
|
110 | } catch (err) {
|
111 | console.err(err);
|
112 | console.err("Sorry something is wrong you might want to report an issue.");
|
113 | await fs.copyFile("readme.md.bk", readmePath);
|
114 | } finally {
|
115 | await fs.unlink("readme.md.bk");
|
116 | }
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | cli(); |
\ | No newline at end of file |