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