Skip to content

Commit

Permalink
feat: adding init command
Browse files Browse the repository at this point in the history
  • Loading branch information
4lejandrito committed Oct 10, 2023
1 parent 9360359 commit 8046c40
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 164 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ npx fetchbook run --demo -v
## Installation

To use Fetchbook in you own projects, you can install it like this:
To use Fetchbook in you own project:

```bash
npx fetchbook init
```

To create a separate Fetchbook project:

```bash
npx fetchbook init <project name>
```

Alternatively, you can install it manually:

```bash
npm install fetchbook
Expand Down Expand Up @@ -110,6 +122,26 @@ npx fetchbook export --format=curl --demo -a
fetchbook export --format curl -a
```

### Init

```bash
npx fetchbook init [name]
```

Initialize a fetchbook project.

#### Arguments

- `[name]` (optional): Name of the project. If set Fetchbook will create a new folder with a `package.json` file and a bunch of sample [fetch story files](#fetch-story-files). If not set Fetchbook will initialize an existing project.

#### Examples

1. Run a single [fetch story file](#fetch-story-files):

```bash
npx fetchbook init my-fetchbook-project
```

## Fetch Story Files

Fetch story files are TypeScript modules ending with `.fetch.ts` that must comply with the following type definition:
Expand Down
14 changes: 14 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ import visit from "./lib/visit";
import findStories from "./lib/find-stories";
import getCurl from "./lib/get-curl";
import run from "./lib/run";
import { createProject, initProject } from "./lib/project";
import picocolors from "picocolors";

program.name("fetchbook").description("Manage your HTTP requests");

program
.command("init")
.argument(
"[name]",
"name of the project, omit it to initialize an existing one",
)
.description("initialize a fetchbook project")
.action(async (name) => {
await (name ? createProject(name) : initProject());
console.log(picocolors.green("✔"), "Project initialized successfully");
});

program
.command("run")
.description("run your stories")
Expand Down
32 changes: 32 additions & 0 deletions lib/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { mkdir, writeJson, copy, exists } from "fs-extra";
import path from "path";
import { $ } from "execa";
import { version } from "../package.json";

export async function createProject(name: string) {
await mkdir(name);
await writeJson(path.join(name, "package.json"), {
name: `${name}-fetchbook`,
version: "1.0.0",
description: `${name}'s fetchbook`,
scripts: {
test: "fetchbook run --all",
},
});
await initProject(name);
await $`prettier --write ${name}`;
}

export async function initProject(cwd = ".") {
if (!(await exists(path.join(cwd, "package.json")))) {
throw "You must run init inside the root of a node project";
}
await $`add-dependencies ${path.join(
cwd,
"package.json",
)} --dev fetchbook@^${version}`;
await copy(
path.join(__dirname, "..", "examples"),
path.join(cwd, "fetchbook"),
);
}
Loading

0 comments on commit 8046c40

Please sign in to comment.