Skip to content

Commit

Permalink
feat: add inputFromScript, and make SystemRunner use execa's reject: …
Browse files Browse the repository at this point in the history
…false (#74)

* feat: add inputFromScript, and make SystemRunner use execa's reject: false

* pnpm dedupe
  • Loading branch information
JoshuaKGoldberg authored Dec 19, 2024
1 parent 62b2a1d commit 9405d14
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 12 deletions.
6 changes: 4 additions & 2 deletions packages/create/src/system/createSystemRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { execa, parseCommandString } from "execa";

export function createSystemRunner(directory: string) {
const executor = execa({ cwd: directory });
import { SystemRunner } from "../types/system.js";

export function createSystemRunner(directory: string): SystemRunner {
const executor = execa({ cwd: directory, reject: false });

return (command: string) => executor`${parseCommandString(command)}`;
}
4 changes: 2 additions & 2 deletions packages/create/src/types/system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Result } from "execa";
import { ExecaError, Result } from "execa";
import { Octokit } from "octokit";

import { TakeContext } from "./context.js";
Expand Down Expand Up @@ -31,7 +31,7 @@ export interface SystemFetchers {
octokit: Octokit;
}

export type SystemRunner = (command: string) => Promise<Result>;
export type SystemRunner = (command: string) => Promise<ExecaError | Result>;

export interface WritingFileSystem extends InputFileSystem {
writeDirectory: FileSystemWriteDirectory;
Expand Down
5 changes: 2 additions & 3 deletions packages/input-from-file-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
<p align="center"><code>create</code> input that reads a file as JSON.</p>

<p align="center">
<a href="https://github.com/JoshuaKGoldberg/input-from-file-json/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="🤝 Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
<a href="https://codecov.io/gh/JoshuaKGoldberg/input-from-file-json" target="_blank"><img alt="🧪 Coverage" src="https://img.shields.io/codecov/c/github/JoshuaKGoldberg/input-from-file-json?label=%F0%9F%A7%AA%20coverage" /></a>
<a href="https://github.com/JoshuaKGoldberg/input-from-file-json/blob/main/LICENSE.md" target="_blank"><img alt="📝 License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg"></a>
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="🤝 Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/LICENSE.md" target="_blank"><img alt="📝 License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg"></a>
<a href="http://npmjs.com/package/input-from-file-json"><img alt="📦 npm version" src="https://img.shields.io/npm/v/input-from-file-json?color=21bb42&label=%F0%9F%93%A6%20npm" /></a>
<img alt="💪 TypeScript: Strict" src="https://img.shields.io/badge/%F0%9F%92%AA_typescript-strict-21bb42.svg" />
</p>
Expand Down
2 changes: 1 addition & 1 deletion packages/input-from-file-json/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "input-from-file-json",
"version": "0.1.0-alpha.2",
"description": "Test utilities for composable, testable, type-safe templates. ⚗️",
"description": "create input that reads a file as JSON.",
"repository": {
"type": "git",
"url": "https://github.com/JoshuaKGoldberg/create",
Expand Down
5 changes: 2 additions & 3 deletions packages/input-from-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
<p align="center"><code>create</code> input that reads a file as JSON.</p>

<p align="center">
<a href="https://github.com/JoshuaKGoldberg/input-from-file/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="🤝 Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
<a href="https://codecov.io/gh/JoshuaKGoldberg/input-from-file" target="_blank"><img alt="🧪 Coverage" src="https://img.shields.io/codecov/c/github/JoshuaKGoldberg/input-from-file?label=%F0%9F%A7%AA%20coverage" /></a>
<a href="https://github.com/JoshuaKGoldberg/input-from-file/blob/main/LICENSE.md" target="_blank"><img alt="📝 License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg"></a>
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="🤝 Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/LICENSE.md" target="_blank"><img alt="📝 License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg"></a>
<a href="http://npmjs.com/package/input-from-file"><img alt="📦 npm version" src="https://img.shields.io/npm/v/input-from-file?color=21bb42&label=%F0%9F%93%A6%20npm" /></a>
<img alt="💪 TypeScript: Strict" src="https://img.shields.io/badge/%F0%9F%92%AA_typescript-strict-21bb42.svg" />
</p>
Expand Down
2 changes: 1 addition & 1 deletion packages/input-from-file/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "input-from-file",
"version": "0.1.0-alpha.2",
"description": "Test utilities for composable, testable, type-safe templates. ⚗️",
"description": "create input that reads a file as text.",
"repository": {
"type": "git",
"url": "https://github.com/JoshuaKGoldberg/create",
Expand Down
31 changes: 31 additions & 0 deletions packages/input-from-script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1 align="center">input-from-script</h1>

<p align="center"><code>create</code> input that runs a script.</p>

<p align="center">
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"><img alt="🤝 Code of Conduct: Kept" src="https://img.shields.io/badge/%F0%9F%A4%9D_code_of_conduct-kept-21bb42" /></a>
<a href="https://github.com/JoshuaKGoldberg/create/blob/main/LICENSE.md" target="_blank"><img alt="📝 License: MIT" src="https://img.shields.io/badge/%F0%9F%93%9D_license-MIT-21bb42.svg"></a>
<a href="http://npmjs.com/package/input-from-script"><img alt="📦 npm version" src="https://img.shields.io/npm/v/input-from-script?color=21bb42&label=%F0%9F%93%A6%20npm" /></a>
<img alt="💪 TypeScript: Strict" src="https://img.shields.io/badge/%F0%9F%92%AA_typescript-strict-21bb42.svg" />
</p>

```shell
npm i input-from-script
```

```ts
import { inputFromScript } from "input-from-script";

await take(inputFromScript, { command: "npm whoami" });
```

## Options

`inputFromScript` takes a single argument, `command`, of type `string`.

It runs the `command` with [`execa`](https://www.npmjs.com/package/execa) and returns either:

- `Error`: If an error was caught running the script
- `Result`: The type from `execa`, including the `stdout` property

See **[create-josh.vercel.app > Engine > Runtime > Inputs](https://create-josh.vercel.app/engine/runtime/inputs)** for more documentation on Inputs.
38 changes: 38 additions & 0 deletions packages/input-from-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "input-from-script",
"version": "0.1.0-alpha.2",
"description": "create input that runs a script.",
"repository": {
"type": "git",
"url": "https://github.com/JoshuaKGoldberg/create",
"directory": "packages/input-from-script"
},
"license": "MIT",
"author": {
"name": "Josh Goldberg ✨",
"email": "npm@joshuakgoldberg.com"
},
"type": "module",
"main": "./lib/index.js",
"files": [
"lib/",
"package.json",
"LICENSE.md",
"README.md"
],
"scripts": {
"build": "tsc"
},
"dependencies": {
"zod": "^3.24.1"
},
"devDependencies": {
"create-testers": "workspace:*"
},
"peerDependencies": {
"create": "workspace:*"
},
"engines": {
"node": ">=18"
}
}
20 changes: 20 additions & 0 deletions packages/input-from-script/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { testInput } from "create-testers";
import { describe, expect, it, vi } from "vitest";

import { inputFromScript } from "./index.js";

describe("inputFromScript", () => {
it("returns the result from running the command", async () => {
const command = "echo 123";
const expected = { stdout: "123" };
const runner = vi.fn().mockResolvedValue(expected);

const actual = await testInput(inputFromScript, {
args: { command },
runner,
});

expect(actual).toBe(expected);
expect(runner).toHaveBeenCalledWith(command);
});
});
11 changes: 11 additions & 0 deletions packages/input-from-script/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createInput } from "create";
import { z } from "zod";

export const inputFromScript = createInput({
args: {
command: z.string(),
},
async produce({ args, runner }) {
return await runner(args.command);
},
});
8 changes: 8 additions & 0 deletions packages/input-from-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"extends": "../../tsconfig.base.json",
"include": ["src"]
}
8 changes: 8 additions & 0 deletions packages/input-from-script/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineProject } from "vitest/config";

export default defineProject({
test: {
clearMocks: true,
exclude: ["lib", "node_modules"],
},
});
1 change: 1 addition & 0 deletions packages/site/src/content/docs/engine/runtime/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const inputNpmUsername = createInput({

- [`input-from-file`](https://www.npmjs.com/package/input-from-file): Reads a file's contents as text
- [`input-from-file-json`](https://www.npmjs.com/package/input-from-file-json): Parse's a file's text contexts with `JSON.parse`
- [`input-from-script`](https://www.npmjs.com/package/input-from-script): Runs a script command

See those individual packages for their documentation.

Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{ "path": "./packages/create-testers" },
{ "path": "./packages/input-from-file" },
{ "path": "./packages/input-from-file-json" },
{ "path": "./packages/input-from-script" },
{ "path": "./packages/site" }
]
}

0 comments on commit 9405d14

Please sign in to comment.