@ptkdev/all-shields-cli
Version:
Tool to help automate your badges of shields.io, badgen.net, fury.io, github action and snyk.io from .all-shieldsrc dotfile for your markdown files. You can use Liquid variables like {{name}} or {{version}} which refer to your package.json. Inspired by al
148 lines • 7.19 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = void 0;
/**
* Write correct badges on markdown files
* =====================
*
* @contributors: Patryk Rzucidło [@ptkdev] <support@ptkdev.io> (https://ptk.dev)
*
* @license: MIT License
*
*/
const fs_1 = __importDefault(require("fs"));
const util_1 = __importDefault(require("util"));
const writeFile = util_1.default.promisify(fs_1.default.writeFile);
const readFile = util_1.default.promisify(fs_1.default.readFile);
/**
* Create badges
* =====================
*
* Make markdown of badges
*
* @param {ShieldInterface} shield - json parameters from .all-shieldsrc badges array
*
* @return {string} markdown - json of badge converted to markdown string
*/
const badges = ({ shield }) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
let markdown = "";
const badge = {
"custom": (_a = shield === null || shield === void 0 ? void 0 : shield.custom) !== null && _a !== void 0 ? _a : "",
"url": (_b = shield === null || shield === void 0 ? void 0 : shield.url) !== null && _b !== void 0 ? _b : "",
"color": (_c = shield === null || shield === void 0 ? void 0 : shield.color) !== null && _c !== void 0 ? _c : "#D3D3D3",
"label": (_d = shield === null || shield === void 0 ? void 0 : shield.label) !== null && _d !== void 0 ? _d : "",
"title": (_e = shield === null || shield === void 0 ? void 0 : shield.title) !== null && _e !== void 0 ? _e : "",
"message": (_f = shield === null || shield === void 0 ? void 0 : shield.message) !== null && _f !== void 0 ? _f : "",
"style": (_g = shield === null || shield === void 0 ? void 0 : shield.style) !== null && _g !== void 0 ? _g : "",
"logo": (_h = shield === null || shield === void 0 ? void 0 : shield.logo) !== null && _h !== void 0 ? _h : "",
"server_id": (_j = shield === null || shield === void 0 ? void 0 : shield.server_id) !== null && _j !== void 0 ? _j : "",
"platform": (_k = shield === null || shield === void 0 ? void 0 : shield.platform) !== null && _k !== void 0 ? _k : "shields",
};
switch (badge.platform) {
case "discord":
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[](${badge.url}) `;
}
break;
case "fury":
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[, "%20").replace(new RegExp("-", "g"), "--")}.svg)](${badge.url}) `;
}
break;
case "snyk":
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[, "%20").replace(new RegExp("-", "g"), "--")}.svg)](${badge.url}) `;
}
break;
case "github":
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[](${badge.url}) `;
}
break;
case "badgen":
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[, "%20")}/${badge.message.replace(new RegExp("\\s", "g"), "%20")}/${badge.color.replace("#", "")}/?icon=${badge.logo})](${badge.url}) `;
}
break;
default:
if (badge.custom !== "") {
markdown = `[](${badge.url}) `;
}
else {
markdown = `[.replace(new RegExp("-", "g"), "--")}-${encodeURIComponent(badge.message).replace(new RegExp("-", "g"), "--")}-${badge.color.replace("#", "")}.svg?style=${badge.style}&logo=${badge.logo})](${badge.url}) `;
}
}
return markdown;
});
/**
* Generate Markdown
* =====================
*
* Each merge json and generate markdown
*
* @param {MergeDotfilesResponseInterface} json - merged JSON object from package.json and all-shieldsrc file
*
* @return {Promise<ErrorInterface>} { error } - description of error, catch error
*
*/
const generate = ({ json }) => __awaiter(void 0, void 0, void 0, function* () {
for (let j = 0; j < json.files.length; j++) { // each markdown files from .all-shieldsrc
let file;
try {
file = yield readFile(json.files[j], "utf8");
}
catch (error) {
return { error };
}
for (let z = 0; z < json.shields.length; z++) { // each shields from .all-shieldsrc
let markdown = "";
for (let i = 0; i < json.shields[z].badges.length; i++) { // each badge images from current shield
try {
markdown += yield badges({ shield: json.shields[z].badges[i] });
}
catch (error) {
return { error };
}
}
file = file.replace(new RegExp(`(<!-- all-shields/${json.shields[z].id}:START -->)([\\S\\s]*)(<!-- all-shields/${json.shields[z].id}:END -->)`, "gm"), `$1\n\n${markdown.trim()}\n\n$3`);
}
try {
yield writeFile(json.files[j], file, "utf8");
}
catch (error) {
return { error };
}
}
return {};
});
exports.generate = generate;
exports.default = { generate: exports.generate };
//# sourceMappingURL=generate.js.map