UNPKG

4.38 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");
10
11updateNotifier({ pkg }).notify();
12async 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 // backup readme
80 await fsPromises.copyFile(markDownPath, "readme.md.bk");
81 try {
82 // truncate the existing readme
83 await fsPromises.truncate(markDownPath, 0);
84 // copy content from start to start placeholder to the truncated file
85 // along with start placeholder
86 await fsPromises.appendFile(
87 markDownPath,
88 readmeContent.slice(
89 0,
90 startPlaceholderIndex + startPlaceholder.length + 1
91 )
92 );
93 // append all generated badges
94 await fsPromises.appendFile(markDownPath, "\n\n" + allBadgesString);
95 let contentToAppend;
96 // if both start and end placeholder are present in the file
97 // this means user already used auto-badger on this file
98 // we now need to be more careful
99 if (startPlaceholderIndex > -1 && endPlaceholderIndex > -1) {
100 // content from end of end placeholder to end of file
101 contentToAppend = readmeContent.slice(
102 endPlaceholderIndex + endPlaceholder.length + 1,
103 readmeContent.length
104 );
105 } else if (startPlaceholderIndex > -1) {
106 // content from end of start placeholder to end of file
107 contentToAppend = readmeContent.slice(
108 startPlaceholderIndex + startPlaceholder.length + 1,
109 readmeContent.length
110 );
111 }
112 // add promotional content and end of badges
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 // add end placeholder
119 await fsPromises.appendFile(markDownPath, "\n\n" + endPlaceholder);
120 // append the remaining content
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
131module.exports = autoBadger;