-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pooya Parsa <pooya@pi0.io> Co-authored-by: uncenter <47499684+uncenter@users.noreply.github.com>
- Loading branch information
1 parent
c04df9d
commit 8db8058
Showing
7 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import { readPackageJSON } from "pkg-types"; | ||
import { readPackageJSON, type PackageJson } from "pkg-types"; | ||
import _consola from "consola"; | ||
import { defu } from "defu"; | ||
|
||
export const consola = _consola.withTag("automd"); | ||
|
||
export async function getPkg( | ||
dir: string, | ||
input: { name?: string; version?: string } = {}, | ||
) { | ||
if (input.name && input.version) { | ||
return input; | ||
} | ||
export async function getPkg(dir: string, input: Record<string, string> = {}) { | ||
const pkg = await readPackageJSON(dir).catch(() => undefined); | ||
return { | ||
name: process.env.npm_package_name, | ||
version: process.env.npm_package_name, | ||
...pkg, | ||
...input, | ||
}; | ||
return defu( | ||
{ | ||
name: input.name, | ||
version: input.version, | ||
github: input.github || input.gh, | ||
}, | ||
{ | ||
name: pkg?.name, | ||
version: pkg?.version, | ||
github: _getGitRepo(pkg?.repository), | ||
}, | ||
{ | ||
name: process.env.npm_package_name, | ||
version: process.env.npm_package_version, | ||
}, | ||
); | ||
} | ||
|
||
function _getGitRepo(repo: PackageJson["repository"]) { | ||
const url = typeof repo === "string" ? repo : repo?.url; | ||
if (!url || typeof url !== "string") { | ||
return; | ||
} | ||
const match = | ||
/(?:https:\/\/github\.com\/|gh:|github:|)([\w-]+)\/([\w-]+)/.exec(url); | ||
if (match && match[1] && match[2]) { | ||
return `${match[1]}/${match[2]}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { image, link } from "omark"; | ||
import { defineGenerator } from "../generator"; | ||
import { getPkg } from "../_utils"; | ||
|
||
type BadgeType = keyof typeof badgeTypes; | ||
type BadgeProvider = Record<BadgeType, string> & { _params?: string[] }; | ||
|
||
const badgeTypes = { | ||
npmVersion: { | ||
name: "npm version", | ||
to: "https://npmjs.com/package/{name}", | ||
}, | ||
npmDownloads: { | ||
name: "npm downloads", | ||
to: "https://npmjs.com/package/{name}", | ||
}, | ||
bundlephobia: { | ||
name: "bundle size", | ||
to: "https://bundlephobia.com/package/{name}", | ||
}, | ||
codecov: { | ||
name: "codecov", | ||
to: "https://codecov.io/gh/{github}", | ||
}, | ||
}; | ||
|
||
const badgeProviders = <Record<string, BadgeProvider>>{ | ||
shields: { | ||
_params: ["style", "color", "labelColor"], | ||
npmVersion: "https://img.shields.io/npm/v/{name}", | ||
npmDownloads: "https://img.shields.io/npm/dm/{name}", | ||
bundlephobia: "https://img.shields.io/bundlephobia/minzip/{name}", | ||
codecov: "https://img.shields.io/codecov/c/gh/{github}", | ||
}, | ||
badgen: { | ||
npmVersion: "https://flat.badgen.net/npm/v/{name}", | ||
npmDownloads: "https://flat.badgen.net/npm/dm/{name}", | ||
bundlephobia: "https://flat.badgen.net/bundlephobia/minzip/{name}", | ||
codecov: "https://flat.badgen.net/codecov/c/github/{github}", | ||
}, | ||
badgenClassic: { | ||
npmVersion: "https://badgen.net/npm/v/{name}", | ||
npmDownloads: "https://badgen.net/npm/dm/{name}", | ||
bundlephobia: "https://badgen.net/bundlephobia/minzip/{name}", | ||
codecov: "https://badgen.net/codecov/c/github/{github}", | ||
}, | ||
}; | ||
|
||
export const badges = defineGenerator({ | ||
name: "badges", | ||
async generate({ config, args }) { | ||
const pkg = await getPkg(config.dir, args); | ||
const ctx: Record<string, any> = { | ||
name: pkg.name, | ||
github: pkg.github, | ||
...args, | ||
}; | ||
|
||
const fillStr = (str: string) => | ||
str.replace(/{(\w+)}/g, (_, key) => ctx[key] || ""); | ||
|
||
const provider = badgeProviders[args.provider] || badgeProviders.shields; | ||
const providerParams = (provider._params || []) | ||
.filter((key) => ctx[key]) | ||
.map( | ||
(key) => `${encodeURIComponent(key)}=${encodeURIComponent(ctx[key])}`, | ||
) | ||
.join("&"); | ||
|
||
const badges = { | ||
npmVersion: { | ||
enabled: ctx.name, | ||
...badgeTypes.npmVersion, | ||
}, | ||
npmDownloads: { | ||
enabled: ctx.name, | ||
...badgeTypes.npmDownloads, | ||
}, | ||
bundlephobia: { | ||
enabled: args.bundlephobia && ctx.name, | ||
...badgeTypes.bundlephobia, | ||
}, | ||
codecov: { | ||
enabled: args.codecov && ctx.github, | ||
...badgeTypes.codecov, | ||
}, | ||
} as const; | ||
|
||
const md: string[] = []; | ||
|
||
for (const [badgeType, badge] of Object.entries(badges)) { | ||
if (!badge.enabled || !provider[badgeType as BadgeType]) { | ||
continue; | ||
} | ||
const to = fillStr(badge.to); | ||
const imgURL = | ||
fillStr(provider[badgeType as BadgeType]) + | ||
(providerParams ? `?${providerParams}` : ""); | ||
md.push(link(to, image(imgURL, badge.name))); | ||
} | ||
|
||
return { | ||
contents: md.join("\n"), | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { Generator } from "../generator"; | ||
import { jsdocs } from "./jsdocs"; | ||
import { badges } from "./badges"; | ||
import { pmX, pmInstall } from "./pm"; | ||
|
||
export default { | ||
jsdocs, | ||
badges, | ||
"pm-install": pmInstall, | ||
"pm-x": pmX, | ||
} as Record<string, Generator>; |