1 | #! /usr/bin/env node
|
2 |
|
3 | const badges = require("./src/badges");
|
4 | const fsPromises = require("fs/promises");
|
5 | const fs = require("fs");
|
6 | const chalk = require("chalk");
|
7 | const { startPlaceholder, endPlaceholder } = require("./src/constants/strings");
|
8 | const updateNotifier = require("update-notifier");
|
9 | const pkg = require("./package.json");
|
10 |
|
11 | updateNotifier({ pkg }).notify();
|
12 | async function autoBadger(input, cliArgs) {
|
13 | console.log(cliArgs);
|
14 | const markDownPath = cliArgs.markdownPath || "./README.md";
|
15 | if (!fs.existsSync(markDownPath)) {
|
16 | console.error(
|
17 | chalk.red.bgYellow(`${cliArgs.markdownPath} not found. Aborting...`)
|
18 | );
|
19 | return;
|
20 | }
|
21 | const readmeBuffer = await fsPromises.readFile(markDownPath);
|
22 | const readmeContent = readmeBuffer.toString();
|
23 | const startPlaceholderIndex = readmeContent.indexOf(startPlaceholder);
|
24 | const endPlaceholderIndex = readmeContent.indexOf(endPlaceholder);
|
25 | if (startPlaceholderIndex === -1) {
|
26 | console.error(
|
27 | chalk.yellow("No placeholder found in markdown. Aborting...")
|
28 | );
|
29 | return;
|
30 | }
|
31 | let allBadges = await Promise.all([
|
32 | badges.build.generate(),
|
33 | badges.version.generate(),
|
34 | badges.dependancies.generate(),
|
35 | badges.size.generate(),
|
36 | badges.downloads.generate(),
|
37 | badges.coverage.generate(),
|
38 | badges.github.generate(),
|
39 | badges.license.generate(),
|
40 | badges.twitter.generate(cliArgs.twitterUsername),
|
41 | badges.community.generate({
|
42 | communityProvider: cliArgs.communityProvider,
|
43 | communityId: cliArgs.communityName,
|
44 | communityServerUrl: cliArgs.communityServerUrl,
|
45 | }),
|
46 | ]);
|
47 | const [
|
48 | buildBadge,
|
49 | versionBadge,
|
50 | dependanciesBadge,
|
51 | sizeBadge,
|
52 | downloadsBadge,
|
53 | coverageBadge,
|
54 | allContributersBadge,
|
55 | codeOfConduct,
|
56 | starsBadge,
|
57 | forkBadge,
|
58 | licenseBadge,
|
59 | twitterBadge,
|
60 | communityBadge,
|
61 | ] = allBadges.flat(Infinity);
|
62 | const allBadgesString = [
|
63 | [buildBadge, versionBadge, sizeBadge, coverageBadge, licenseBadge]
|
64 | .filter(Boolean)
|
65 | .join("\n"),
|
66 | [
|
67 | dependanciesBadge,
|
68 | downloadsBadge,
|
69 | allContributersBadge,
|
70 | codeOfConduct,
|
71 | communityBadge,
|
72 | ]
|
73 | .filter(Boolean)
|
74 | .join("\n"),
|
75 | [starsBadge, forkBadge, twitterBadge].filter(Boolean).join("\n"),
|
76 | ].join("\n\n");
|
77 | console.log(chalk.blue("Generated Badges Are"));
|
78 | console.log(chalk.blue(allBadgesString));
|
79 |
|
80 | await fsPromises.copyFile(markDownPath, "readme.md.bk");
|
81 | try {
|
82 |
|
83 | await fsPromises.truncate(markDownPath, 0);
|
84 |
|
85 |
|
86 | await fsPromises.appendFile(
|
87 | markDownPath,
|
88 | readmeContent.slice(
|
89 | 0,
|
90 | startPlaceholderIndex + startPlaceholder.length + 1
|
91 | )
|
92 | );
|
93 |
|
94 | await fsPromises.appendFile(markDownPath, "\n\n" + allBadgesString);
|
95 | let contentToAppend;
|
96 |
|
97 |
|
98 |
|
99 | if (startPlaceholderIndex > -1 && endPlaceholderIndex > -1) {
|
100 |
|
101 | contentToAppend = readmeContent.slice(
|
102 | endPlaceholderIndex + endPlaceholder.length + 1,
|
103 | readmeContent.length
|
104 | );
|
105 | } else if (startPlaceholderIndex > -1) {
|
106 |
|
107 | contentToAppend = readmeContent.slice(
|
108 | startPlaceholderIndex + startPlaceholder.length + 1,
|
109 | readmeContent.length
|
110 | );
|
111 | }
|
112 |
|
113 | await fsPromises.appendFile(
|
114 | markDownPath,
|
115 | "\n\n" +
|
116 | `###### :clap: & :heart: to [auto badger](https://github.com/technikhil314/auto-badger) for making badging simple</small>`
|
117 | );
|
118 |
|
119 | await fsPromises.appendFile(markDownPath, "\n\n" + endPlaceholder);
|
120 |
|
121 | await fsPromises.appendFile(markDownPath, "\n\n" + contentToAppend);
|
122 | } catch (err) {
|
123 | console.err(err);
|
124 | console.err("Sorry something is wrong you might want to report an issue.");
|
125 | await fsPromises.copyFile("readme.md.bk", markDownPath);
|
126 | } finally {
|
127 | await fsPromises.unlink("readme.md.bk");
|
128 | }
|
129 | }
|
130 |
|
131 | module.exports = autoBadger;
|