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