-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
125 lines (123 loc) · 4.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/env node
const badges = require("./src/badges");
const fsPromises = require("fs/promises");
const fs = require("fs");
const chalk = require("chalk");
const { startPlaceholder, endPlaceholder } = require("./src/constants/strings");
const updateNotifier = require("update-notifier");
const pkg = require("./package.json");
updateNotifier({ pkg }).notify();
async function autoBadger(input, cliArgs) {
const markDownPath = cliArgs.markdownPath || "./README.md";
if (!fs.existsSync(markDownPath)) {
console.error(
chalk.red.bgYellow(`${cliArgs.markdownPath} not found. Aborting...`)
);
return;
}
const readmeBuffer = await fsPromises.readFile(markDownPath);
const readmeContent = readmeBuffer.toString();
const startPlaceholderIndex = readmeContent.indexOf(startPlaceholder);
const endPlaceholderIndex = readmeContent.indexOf(endPlaceholder);
if (startPlaceholderIndex === -1) {
console.error(
chalk.yellow("No placeholder found in markdown. Aborting...")
);
return;
}
let allBadges = await Promise.all([
badges.build.generate(cliArgs),
badges.version.generate(cliArgs),
badges.dependancies.generate(cliArgs),
badges.size.generate(cliArgs),
badges.downloads.generate(cliArgs),
badges.coverage.generate(cliArgs),
badges.github.generate(cliArgs),
badges.license.generate(cliArgs),
badges.twitter.generate(cliArgs),
badges.community.generate(cliArgs),
]);
const [
buildBadge,
versionBadge,
dependanciesBadge,
sizeBadge,
downloadsBadge,
coverageBadge,
allContributersBadge,
codeOfConduct,
starsBadge,
forkBadge,
licenseBadge,
twitterBadge,
communityBadge,
] = allBadges.flat(Infinity);
const allBadgesString = [
[buildBadge, versionBadge, sizeBadge, coverageBadge, licenseBadge]
.filter(Boolean)
.join("\n"),
[
dependanciesBadge,
downloadsBadge,
allContributersBadge,
codeOfConduct,
communityBadge,
]
.filter(Boolean)
.join("\n"),
[starsBadge, forkBadge, twitterBadge].filter(Boolean).join("\n"),
].join("\n\n");
console.log(chalk.blue("Generated Badges Are\n"));
console.log(chalk.blue(allBadgesString));
// backup readme
await fsPromises.copyFile(markDownPath, "readme.md.bk");
try {
// truncate the existing readme
await fsPromises.truncate(markDownPath, 0);
// copy content from start to start placeholder to the truncated file
// along with start placeholder
await fsPromises.appendFile(
markDownPath,
readmeContent.slice(
0,
startPlaceholderIndex + startPlaceholder.length + 1
)
);
// append all generated badges
await fsPromises.appendFile(markDownPath, "\n\n" + allBadgesString);
let contentToAppend;
// if both start and end placeholder are present in the file
// this means user already used auto-badger on this file
// we now need to be more careful
if (startPlaceholderIndex > -1 && endPlaceholderIndex > -1) {
// content from end of end placeholder to end of file
contentToAppend = readmeContent.slice(
endPlaceholderIndex + endPlaceholder.length + 1,
readmeContent.length
);
} else if (startPlaceholderIndex > -1) {
// content from end of start placeholder to end of file
contentToAppend = readmeContent.slice(
startPlaceholderIndex + startPlaceholder.length + 1,
readmeContent.length
);
}
// add promotional content and end of badges
await fsPromises.appendFile(
markDownPath,
"\n\n" +
`###### :clap: & :heart: to [auto badger](https://github.com/technikhil314/auto-badger) for making badging simple`
);
// add end placeholder
await fsPromises.appendFile(markDownPath, "\n\n" + endPlaceholder);
// append the remaining content
await fsPromises.appendFile(markDownPath, "\n\n" + contentToAppend);
} catch (err) {
console.err(err);
console.err("Sorry something is wrong you might want to report an issue.");
await fsPromises.copyFile("readme.md.bk", markDownPath);
} finally {
await fsPromises.unlink("readme.md.bk");
}
}
module.exports = autoBadger;