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: pm-x generator #11

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,37 @@ The `pm-install` generator generates commands for several JavaScript package man
- `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`).

### `pm-x`

The `pm-x` generator generates commands for running a package through JavaScript package managers.

#### Usage

<!-- AUTOMD_START generator="pm-x" name="package-name" usage="[files] <flags>" -->
<!-- AUTOMD_END -->

**Updated Result:**

<!-- AUTOMD_START generator="pm-x" name="package-name" usage="[files] <flags>" -->

```sh
# npm
npx package-name@latest [files] <flags>

# pnpm
pnpm dlx package-name@latest [files] <flags>

# bun
bunx package-name@latest [files] <flags>
```

<!-- AUTOMD_END -->

#### Arguments

- `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 `""`).

## Development

- Clone this repository
Expand Down
15 changes: 15 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
## Usage

<!-- AUTOMD_START generator="pm-x" name="package-name" usage="[path]" -->

```sh
# npm
npx package-name@latest [path]

# pnpm
pnpm dlx package-name@latest [path]

# bun
bunx package-name@latest [path]
```

<!-- AUTOMD_END -->

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

```sh
Expand Down
4 changes: 3 additions & 1 deletion src/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Generator } from "../generator";
import jsdocs from "./jsdocs";
import pmInstall from "./pm-install";
import pmInstall from "./pm/install";
import pmX from "./pm/x";

export default {
jsdocs,
"pm-install": pmInstall,
"pm-x": pmX,
} as Record<string, Generator>;
47 changes: 0 additions & 47 deletions src/generators/pm-install.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/generators/pm/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { codeBlock } from "omark";
import { defineGenerator } from "../../generator";
import { INSTALL_COMMANDS, inferPackageName } from "./utils";

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 -->",
};
}

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

return {
contents: codeBlock(
INSTALL_COMMANDS.map(
([cmd, install]) =>
`# ${cmd.includes("nypm") ? "✨ Auto-detect" : cmd}\n${cmd} ${install}${args.dev ? " -D" : ""} ${name}`,
).join("\n\n"),
"sh",
),
};
},
});
21 changes: 21 additions & 0 deletions src/generators/pm/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readPackageJSON } from "pkg-types";

export async function inferPackageName(dir: string) {
const pkgName = await readPackageJSON(dir)
.then((pkg) => pkg?.name)
.catch(() => undefined);
return pkgName || process.env.npm_package_name;
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved

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

export const RUN_COMMANDS = [
["npm", "npx"],
["pnpm", "pnpm dlx"],
["bun", "bunx"],
];
26 changes: 26 additions & 0 deletions src/generators/pm/x.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { codeBlock } from "omark";
import { defineGenerator } from "../../generator";
import { RUN_COMMANDS, inferPackageName } from "./utils";

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

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

return {
contents: codeBlock(
RUN_COMMANDS.map(
([pm, cmd]) =>
`# ${pm}\n${cmd} ${name}@latest${args.usage ? ` ${args.usage}` : ""}`,
).join("\n\n"),
"sh",
),
};
},
});
Loading