Skip to content

Commit

Permalink
fix(cli): package installation and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Mar 13, 2024
1 parent 4593d92 commit 640ccc3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
50 changes: 30 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,42 @@ A simple GraphQL server for Deno Fresh.

## Installation

1. [Create a fresh project](https://fresh.deno.dev/docs/getting-started/create-a-project)
or checkout your existing Fresh project.
```bash
# Create a Fresh project
> deno run -A -r https://fresh.deno.dev

1. Run `deno run jsr:@vicary/fresh-graphql` to patch your `dev.ts`, or do it
manually:
# Add fresh-graphql to your project
> deno run -A jsr:@vicary/fresh-graphql
```

### Manual Installation

You may also patch the files manually if you have modified `dev.ts` or
`deno.json` for an existing Fresh project.

```diff
// dev.ts
#### dev.ts

import "https://deno.land/x/dotenv/load.ts";
```diff
-#!/usr/bin/env -S deno run -A --watch=static/,routes/
+#!/usr/bin/env -S deno run -A --watch=static/,routes/,graphql/

import dev from "$fresh/dev.ts";
+import { dev as graphql } from "@vicary/fresh-graphql";
import "https://deno.land/x/dotenv/load.ts";

+await graphql(import.meta.url);
await dev(import.meta.url, "./main.ts");
```
import dev from "$fresh/dev.ts";
+import { dev as graphql } from "@vicary/fresh-graphql";

```diff
// deno.json
+await graphql(import.meta.url);
await dev(import.meta.url, "./main.ts");
```

#### deno.json

"tasks": {
- "start": "deno run -A --watch=static/,routes/ dev.ts",
+ "start": "deno run -A --watch=static/,routes/,graphql/ dev.ts",
}
```
```diff
"tasks": {
- "start": "deno run -A --watch=static/,routes/ dev.ts",
+ "start": "deno run -A --watch=static/,routes/,graphql/ dev.ts",
}
```

## Usage

Expand Down Expand Up @@ -138,7 +148,7 @@ export const resolver = async function* (_, { from }) {

Supported, documentations coming.

You may read `dev.ts` if you need it now.
If you need it now, you may read our source code.

### Side notes

Expand Down
33 changes: 23 additions & 10 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,26 @@ log.setup({
});

async function main() {
if (!await installPackage()) {
log.error("Unable to install fresh-graphql package.");
return;
}

if (!await ensurePatchExecutable()) {
log.warn("`patch` not found in your environment.");
return;
}

if (promptYesNo("Apply patch to `dev.ts`?", true)) {
if (promptYesNo("Do you want fresh-graphql to patch your `dev.ts`?", true)) {
if (!await patchDev()) {
log.error("Unable to patch dev.ts, please try to do it manually.");
log.info(
`Our patch works with an unmodified dev.ts generated after Fresh 1.6.5.`,
);
}
}

if (promptYesNo("Apply patch to `deno.json`?", true)) {
if (promptYesNo("Do you want fresh-graphql to patch to `deno.json`?", true)) {
if (!await patchDenoJson()) {
log.error("Unable to patch deno.json, please try to do it manually.");
}
Expand All @@ -59,6 +67,15 @@ async function main() {

await main();

async function installPackage() {
const { success } = await new Deno.Command(
"deno",
{ args: ["add", "jsr:@vicary/fresh-graphql"] },
).spawn().status;

return success;
}

async function ensurePatchExecutable() {
try {
const { success } = await new Deno.Command(
Expand Down Expand Up @@ -110,10 +127,6 @@ async function patchDev() {
return false;
}

log.info(
`Our patch works with unmodified dev.ts generated after Fresh 1.6.5.`,
);

return await attemptPatch();

async function attemptPatch(): Promise<boolean> {
Expand All @@ -131,7 +144,7 @@ async function patchDev() {
const stdout = decodeOutput(out.stdout);

if (stdout.includes("previously applied")) {
log.info(`Patch already applied.`);
log.info("Your dev.ts is already patched, skipping.");

return true;
}
Expand All @@ -150,7 +163,7 @@ async function patchDev() {
return false;
}

log.info(`Patch applied successfully.`);
log.info(`dev.ts patched successfully.`);

return true;
}
Expand All @@ -173,7 +186,7 @@ async function patchDenoJson(): Promise<boolean> {
if (
task.includes("--watch=static/,routes/,graphql/ dev.ts")
) {
log.info(`Patch already applied.`);
log.info(`Your deno.json is already patched, skipping.`);

return true;
}
Expand All @@ -195,7 +208,7 @@ async function patchDenoJson(): Promise<boolean> {
return false;
}

log.info(`Patch applied successfully.`);
log.info(`deno.json patched successfully.`);

return true;
}
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vicary/fresh-graphql",
"version": "0.2.0",
"version": "0.2.1",
"exports": "./mod.ts",
"lock": false,
"nodeModulesDir": false
Expand Down
2 changes: 1 addition & 1 deletion examples/graphql-yoga/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"$fresh/": "https://deno.land/x/[email protected]/",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"@vicary/fresh-graphql": "jsr:@vicary/fresh-graphql@^0.1.0",
"@vicary/fresh-graphql": "jsr:@vicary/fresh-graphql@^0.2.0",
"graphql-yoga": "npm:graphql-yoga@^5.1.1",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
Expand Down

0 comments on commit 640ccc3

Please sign in to comment.