UNPKG

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