diff --git a/README.md b/README.md index e8c5ee0..f2c4632 100644 --- a/README.md +++ b/README.md @@ -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 + + + + +**Updated Result:** + + + + ```sh + # npm + npx package-name@latest [files] + + # pnpm + pnpm dlx package-name@latest [files] + + # bun + bunx package-name@latest [files] + ``` + + + +#### 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 diff --git a/playground/README.md b/playground/README.md index a91b2c5..4b90d14 100644 --- a/playground/README.md +++ b/playground/README.md @@ -1,29 +1,44 @@ ## Usage - + + +```sh +# npm +npx automd@0.1.5 . + +# pnpm +pnpm dlx automd@0.1.5 . + +# bun +bunx automd@0.1.5 . +``` + + + + ```sh # ✨ Auto-detect -npx nypm i -D package-name +npx nypm i -D automd@^0.1.5 # npm -npm install -D package-name +npm install -D automd@^0.1.5 # yarn -yarn add -D package-name +yarn add -D automd@^0.1.5 # pnpm -pnpm install -D package-name +pnpm install -D automd@^0.1.5 # bun -bun install -D package-name +bun install -D automd@^0.1.5 ``` ## Utils - + ### `add(a, b)` diff --git a/src/_utils.ts b/src/_utils.ts new file mode 100644 index 0000000..9a485fc --- /dev/null +++ b/src/_utils.ts @@ -0,0 +1,17 @@ +import { readPackageJSON } from "pkg-types"; + +export async function getPkg( + dir: string, + input: { name?: string; version?: string } = {}, +) { + if (input.name && input.version) { + return input; + } + const pkg = await readPackageJSON(dir).catch(() => undefined); + return { + name: process.env.npm_package_name, + version: process.env.npm_package_name, + ...pkg, + ...input, + }; +} diff --git a/src/generators/index.ts b/src/generators/index.ts index e9cec19..29721a8 100644 --- a/src/generators/index.ts +++ b/src/generators/index.ts @@ -1,8 +1,9 @@ import { Generator } from "../generator"; -import jsdocs from "./jsdocs"; -import pmInstall from "./pm-install"; +import { jsdocs } from "./jsdocs"; +import { pmX, pmInstall } from "./pm"; export default { jsdocs, "pm-install": pmInstall, + "pm-x": pmX, } as Record; diff --git a/src/generators/jsdocs.ts b/src/generators/jsdocs.ts index 944c201..37db76c 100644 --- a/src/generators/jsdocs.ts +++ b/src/generators/jsdocs.ts @@ -4,7 +4,7 @@ import type { Schema } from "untyped"; import { titleCase } from "scule"; import { defineGenerator } from "../generator"; -export default defineGenerator({ +export const jsdocs = defineGenerator({ name: "jsdocs", async generate({ options, args }) { const entryPath = resolve(options.dir, args.src || "./src/index"); diff --git a/src/generators/pm-install.ts b/src/generators/pm-install.ts deleted file mode 100644 index c933c25..0000000 --- a/src/generators/pm-install.ts +++ /dev/null @@ -1,47 +0,0 @@ -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: "", - }; - } - - 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; -} diff --git a/src/generators/pm.ts b/src/generators/pm.ts new file mode 100644 index 0000000..a211538 --- /dev/null +++ b/src/generators/pm.ts @@ -0,0 +1,67 @@ +import { codeBlock } from "omark"; +import { defineGenerator } from "../generator"; +import { getPkg } from "../_utils"; + +const INSTALL_COMMANDS = [ + ["npm", "install"], + ["yarn", "add"], + ["pnpm", "install"], + ["bun", "install"], +]; + +const RUN_COMMANDS = [ + ["npm", "npx"], + ["pnpm", "pnpm dlx"], + ["bun", "bunx"], +]; + +export const pmInstall = defineGenerator({ + name: "pm-install", + async generate({ options, args }) { + const { name, version } = await getPkg(options.dir, args); + + if (!name) { + return { + contents: "", + }; + } + + // 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}${version ? `@^${version}` : ""}`, + ).join("\n\n"), + "sh", + ), + }; + }, +}); + +export const pmX = defineGenerator({ + name: "pm-x", + async generate({ options, args }) { + const { name, version } = await getPkg(options.dir, args); + + if (!name) { + return { + contents: "", + }; + } + + return { + contents: codeBlock( + RUN_COMMANDS.map( + ([pm, cmd]) => + `# ${pm}\n${cmd} ${name}@${version ? `${version}` : ""}${args.args ? ` ${args.args}` : ""}`, + ).join("\n\n"), + "sh", + ), + }; + }, +});