Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: badges generator #12

Merged
merged 16 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# πŸ€– automd

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
<!-- automd:badges -->

[![npm version](https://img.shields.io/npm/v/automd?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/automd)
[![npm downloads](https://img.shields.io/npm/dm/automd?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/automd)

<!-- /automd -->

Your automated markdown maintainer!

Expand All @@ -20,10 +24,8 @@ Automd scans for the annotation comments within the markdown document and update

The syntax is like this:

```md
<!-- automd:generator [...args] -->
<!-- /automd -->
```
<!-- automd:generator [...args] -->
<!-- /automd -->

### Using CLI

Expand Down Expand Up @@ -172,6 +174,38 @@ The `pm-x` generator generates commands for running a package through JavaScript
- `name`: The package name (by default tries to read from the `name` field in `package.json`).
- `usage`: An additional string appended at the end of each command suggesting usage. (defaults to `""`).

### `badges`

The `badges` generator generates badges for npm version, npm downloads and some optional ones like codecov and bundle.

#### Usage

<!-- automd:badges name=package-name -->
<!-- /automd -->

**Updated Result:**

<!-- automd:badges name=package-name -->

[![npm version](https://img.shields.io/npm/v/package-name?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/package-name)
[![npm downloads](https://img.shields.io/npm/dm/package-name?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/package-name)

<!-- /automd -->

#### Arguments

- `name`: The package name (by default tries to read from the `name` field in `package.json`).
- `style`: The badge style (defaults to `flat`).
- `colorA`: The left color of the badge (defaults to `18181B`).
- `colorB`: The right color of the badge (defaults to `F0DB4F`).
cpreston321 marked this conversation as resolved.
Show resolved Hide resolved
- `downloads-enabled`: Show npm downloads badge (defaults to `true`).
- `version-enabled`: Show npm version badge (defaults to `true`).
- `codecov-enabled`: Show codecov badge (defaults to `false`).
- `bundle-enabled`: Show bundle size badge (defaults to `false`).

> [!NOTE]
> Each badge `downloads|version|codecov|bundle` can be enabled or disabled using `*-enabled` arguments also can be overridden with custom URLs using `*-src`, `*-href` and badge name `*-name` arguments.
cpreston321 marked this conversation as resolved.
Show resolved Hide resolved

## Development

- Clone this repository
Expand All @@ -186,10 +220,3 @@ The `pm-x` generator generates commands for running a package through JavaScript
Made with πŸ’›

Published under [MIT License](./LICENSE).

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/automd?style=flat&colorA=18181B&colorB=F0DB4F
[npm-version-href]: https://npmjs.com/package/automd
[npm-downloads-src]: https://img.shields.io/npm/dm/automd?style=flat&colorA=18181B&colorB=F0DB4F
[npm-downloads-href]: https://npmjs.com/package/automd
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"scripts": {
"automd": "jiti src/cli.ts",
"build": "unbuild",
"build": "pnpm automd && unbuild",
"dev": "vitest",
"play": "pnpm automd --dir playground",
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src",
Expand Down
9 changes: 9 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# automd playground

<!-- automd:badges -->

[![npm version](https://img.shields.io/npm/v/automd?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/automd)
[![npm downloads](https://img.shields.io/npm/dm/automd?style=flat&colorA=18181B&colorB=F0DB4F)](https://npmjs.com/package/automd)

<!-- /automd -->

## Usage

<!-- automd:pm-x args=. -->
Expand Down
90 changes: 90 additions & 0 deletions src/generators/badges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { image, link } from "omark";
import { defineGenerator } from "../generator";
import { getPkg } from "../_utils";

export const badges = defineGenerator({
name: "badges",
async generate({ config, args }) {
const { name } = await getPkg(config.dir, args);

if (!name) {
return {
contents: "<!-- package name is unspecified for badges -->",
};
}

const colorA = args.colorA || "18181B";
const colorB = args.colorB || "F0DB4F";
const style: "flat" | "flat-square" | "plastic" = args.style || "flat";

const badges = {
version: {
enabled: "true",
name: "npm version",
src: `https://img.shields.io/npm/v/${name}?style=${style}&colorA=${colorA}&colorB=${colorB}`,
href: `https://npmjs.com/package/${name}`,
},
downloads: {
enabled: "true",
name: "npm downloads",
src: `https://img.shields.io/npm/dm/${name}?style=${style}&colorA=${colorA}&colorB=${colorB}`,
href: `https://npmjs.com/package/${name}`,
},
codecov: {
enabled: "false",
name: "Codecov",
src: `https://img.shields.io/codecov/c/gh/${name}/main?style=${style}&colorA=${colorA}&colorB=${colorB}`,
href: `https://codecov.io/gh/${name}`,
},
bundle: {
enabled: "false",
name: "Bundle size",
src: `https://img.shields.io/bundlephobia/minzip/${name}?style=${style}&colorA=${colorA}&colorB=${colorB}`,
href: `https://bundlephobia.com/result?p=${name}`,
},
};

// TODO: Add custom badges? JSON.stringify(args['custom-badges'], null, 2)

// Dynamically replace values with args if they exist
// e.g version-src="Test" -> badges.ersion.src="....."
for (const key in badges) {
const k = key as keyof typeof badges;
for (const prop in badges[k]) {
const p = prop as keyof (typeof badges)[typeof k];
// Make sure the value is a string and exists
if (args[`${k}-${p}`] && typeof args[`${k}-${p}`] === "string") {
if (p === "enabled") {
badges[k][p] = args[`${k}-${p}`] === "true" ? "true" : "false";
continue;
}
badges[k][p] = args[`${k}-${p}`];
}
}
}
cpreston321 marked this conversation as resolved.
Show resolved Hide resolved

return {
contents: generateBadgesMd(badges),
};
},
});

const generateBadgesMd = (badges: Record<string, any>) => {
let str = "";

for (const key in badges) {
const badge = badges[key];

if (badge.enabled === "false") {
continue;
}

str += link(badge.href, image(badge.src, badge.name)) + "\n";
}

if (str.endsWith("\n")) {
str = str.slice(0, -1);
}

return str;
};
2 changes: 2 additions & 0 deletions src/generators/index.ts
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>;
Loading