UNPKG

4.24 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3const badges = require("./src/badges");
4const fsPromises = require("fs/promises");
5const fs = require("fs");
6const chalk = require("chalk");
7const { startPlaceholder, endPlaceholder } = require("./src/constants/strings");
8const updateNotifier = require("update-notifier");
9const pkg = require("./package.json");
10updateNotifier({ pkg }).notify();
11async 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 // backup readme
74 await fsPromises.copyFile(markDownPath, "readme.md.bk");
75 try {
76 // truncate the existing readme
77 await fsPromises.truncate(markDownPath, 0);
78 // copy content from start to start placeholder to the truncated file
79 // along with start placeholder
80 await fsPromises.appendFile(
81 markDownPath,
82 readmeContent.slice(
83 0,
84 startPlaceholderIndex + startPlaceholder.length + 1
85 )
86 );
87 // append all generated badges
88 await fsPromises.appendFile(markDownPath, "\n\n" + allBadgesString);
89 let contentToAppend;
90 // if both start and end placeholder are present in the file
91 // this means user already used auto-badger on this file
92 // we now need to be more careful
93 if (startPlaceholderIndex > -1 && endPlaceholderIndex > -1) {
94 // content from end of end placeholder to end of file
95 contentToAppend = readmeContent.slice(
96 endPlaceholderIndex + endPlaceholder.length + 1,
97 readmeContent.length
98 );
99 } else if (startPlaceholderIndex > -1) {
100 // content from end of start placeholder to end of file
101 contentToAppend = readmeContent.slice(
102 startPlaceholderIndex + startPlaceholder.length + 1,
103 readmeContent.length
104 );
105 }
106 // add promotional content and end of badges
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 // add end placeholder
113 await fsPromises.appendFile(markDownPath, "\n\n" + endPlaceholder);
114 // append the remaining content
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
125module.exports = autoBadger;