Skip to content

Commit

Permalink
feat: pm-install generator (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
Co-authored-by: uncenter <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent 957c995 commit c4bdecb
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 21 deletions.
59 changes: 45 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,20 @@ There are several available generators for automd each supporting different argu

See [open issues](https://github.com/unjs/automd/issues?q=is%3Aopen+is%3Aissue+label%3Agenerator) for proposed generators and feel free to suggest any generator ideas to be included!

### `jsdocs` Generator
### `jsdocs`

jsdocs generator can automatically read through your code and extract and sync documentation of function exports leveraging JSDocs and TypeScript hints.
The `jsdocs` generator can automatically read through your code and extract and sync documentation of function exports leveraging JSDocs and TypeScript hints.

Internally it uses [untyped](https://untyped.unjs.io/) and [jiti](https://github.com/unjs/jiti) loader for JSDocs parsing and TypeScript support.

#### Usage

```md
<!-- AUTOMD_START generator="jsdocs" src="./src/index" -->

...

<!-- AUTOMD_END -->
```
<!-- AUTOMD_START generator="jsdocs" src="./src/index" -->
<!-- AUTOMD_END -->

(make sure to have some utility exports in `src/index.ts` annotated with JSDocs.)

**Example Output:**

## Utils
**Updated Result:**

<!-- AUTOMD_START generator="jsdocs" src="./src/index" -->

Expand All @@ -84,11 +77,49 @@ Internally it uses [untyped](https://untyped.unjs.io/) and [jiti](https://github

<!-- AUTOMD_END -->

### Args supported for `jsdocs`
#### Arguments

- `src`: Path to the source file. The default is `./src/index` and can be omitted.
- `headingLevel`: Nested level for markdown group headings (default is `2` => `##`). Note: Each function uses `headingLevel+1` for the title in nested levels.
- `group`: Only render function exportes annotated with `@group name`. By default, there is no group filter. Value can be a string or array of strings.
- `group`: Only render function exports annotated with `@group name`. By default, there is no group filter. Value can be a string or an array of strings.

### `pm-install`

The `pm-install` generator generates commands for several JavaScript package managers.

#### Usage

<!-- AUTOMD_START generator="pm-install" name="package-name" dev="true" -->
<!-- AUTOMD_END -->

**Updated Result:**

<!-- AUTOMD_START generator="pm-install" name="package-name" dev="true" -->

```sh
# ✨ Auto-detect
npx nypm i -D package-name

# npm
npm install -D package-name

# yarn
yarn add -D package-name

# pnpm
pnpm install -D package-name

# bun
bun install -D package-name
```

<!-- AUTOMD_END -->

#### Arguments

- `name`: The package name (by default tries to read from the `name` field in `package.json`).
- `dev`: Install as a dev dependency. (defaults to `false`).
- `auto`: Auto-detect package manager using [unjs/nypm](https://github.com/unjs/nypm#-nypm). (defaults to `true`).

## Development

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"consola": "^3.2.3",
"destr": "^2.0.2",
"magic-string": "^0.30.7",
"omark": "^0.1.0",
"pkg-types": "^1.0.3",
"scule": "^1.3.0",
"untyped": "^1.4.2"
},
Expand Down
25 changes: 24 additions & 1 deletion playground/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
## Usage

<!-- AUTOMD_START generator="pm-install" name="package-name" dev="true" -->

```sh
# ✨ Auto-detect
npx nypm i -D package-name

# npm
npm install -D package-name

# yarn
yarn add -D package-name

# pnpm
pnpm install -D package-name

# bun
bun install -D package-name
```

<!-- AUTOMD_END -->

## Utils

<!-- AUTOMD_START generator="jsdocs" src="./src/index" -->

#### `add(a, b)`
### `add(a, b)`

Adds two numbers together.

Expand Down
16 changes: 10 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Generator } from "../generator";
import jsdocs from "./jsdocs";
import pmInstall from "./pm-install";

export default {
jsdocs,
"pm-install": pmInstall,
} as Record<string, Generator>;
47 changes: 47 additions & 0 deletions src/generators/pm-install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readPackageJSON } from "pkg-types";
import { codeBlock } from "omark";
import { defineGenerator } from "../generator";

export default defineGenerator({
name: "pm-install",
async generate({ options, args }) {
const name = args.name || (await inferPackageName(options.dir));

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

const pkgInstalls = [
["npm", "install"],
["yarn", "add"],
["pnpm", "install"],
["bun", "install"],
];

// TODO: support noAuto/no-auto
if (args.auto ?? true) {
pkgInstalls.unshift(["npx nypm", "i"]);
}

return {
contents: codeBlock(
pkgInstalls
.map(
([cmd, install]) =>
`# ${cmd.includes("nypm") ? "✨ Auto-detect" : cmd}\n${cmd} ${install}${args.dev ? " -D" : ""} ${name}`,
)
.join("\n\n"),
"sh",
),
};
},
});

async function inferPackageName(dir: string) {
const pkgName = await readPackageJSON(dir)
.then((pkg) => pkg?.name)
.catch(() => undefined);
return pkgName || process.env.npm_package_name;
}

0 comments on commit c4bdecb

Please sign in to comment.