From 91b4d239ad84a8a00f8674d71e969a8932a7d992 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 2 Jun 2021 13:28:44 +0200 Subject: [PATCH] docs: add getting started + basic info (#16) --- README.md | 3 +- docs/file-system-routing.md | 28 ++++++++++++ docs/getting-started.md | 89 +++++++++++++++++++++++++++++++++++++ docs/project-structure.md | 25 +++++++++++ src/cli/routes.ts | 2 +- 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 docs/file-system-routing.md create mode 100644 docs/getting-started.md create mode 100644 docs/project-structure.md diff --git a/README.md b/README.md index 758e9cee..b8019a29 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,5 @@ To deploy the script to [Deno Deploy](https://deno.com/deploy), push your project to GitHub, create a `fresh` project, and link it to `main.ts` file in the created repository. -To learn more about how to use `fresh`, visit the documentation. +For a more in-depth getting started guide, visit the +[Getting Started](./docs/getting-started.md) page in the `fresh` docs. diff --git a/docs/file-system-routing.md b/docs/file-system-routing.md new file mode 100644 index 00000000..cd819df0 --- /dev/null +++ b/docs/file-system-routing.md @@ -0,0 +1,28 @@ +# File system routing + +The naming and location of files in the `pages` folder is important because +these are used to specify the route where a specific API endpoint or HTML page +is available. This concept is called file system routing. + +As an example, the HTML page described by `index.tsx` would be served at `/`, +while the `about.tsx` page would be served at `/about`. File system routing is +also capable of dynamic path segments: `/posts/[post].jsx` would be be used for +requests to `/posts/foo`, `/posts/bar`, or `/posts/baz`, but not for +`/posts/foo/comments` (that would be matched by `/posts/[post]/comments.tsx`). +Multi-level dynamic segments are also possible: `/[owner]/[repo]/[...path]` +would match `/denoland/deno/README.md` and `/lucacasonato/fresh/cli/build.rs`. + +Multi level dynamic segments can only be used as the last path segment. + +## Comparison with path-to-regex + +If you are more familiar with path-to-regex style routes, here is a simple +translation table to get you up to speed. + +| `path-to-regex` | File system routing | +| ----------------------- | ------------------------------------------------------------------ | +| `/` | `/index.tsx` | +| `/about` | `/about.tsx` or `/about/index.tsx` | +| `/posts/:name` | `/posts/[name].tsx` or `/posts/[name]/index.tsx` | +| `/posts/:name/comments` | `/posts/[name]/comments.tsx` or `/posts/[name]/comments/index.tsx` | +| `/x/:module/:path*` | `/x/[module]/[...path].tsx` | diff --git a/docs/getting-started.md b/docs/getting-started.md new file mode 100644 index 00000000..08bc791b --- /dev/null +++ b/docs/getting-started.md @@ -0,0 +1,89 @@ +# Getting Started + +Welcome to the `fresh` documentation. + +## Requirements + +The documentation assumes you have Deno 1.9.2 or later, and `deployctl` 0.3.0 +installed. + +To install Deno, follow the installation instructions in the manual: +https://deno.land/manual/getting_started/installation + +To install `deployctl`, follow the installation instructions here: +https://deno.land/x/deploy#install + +## Setup + +`fresh` comes with a CLI tool, conveniently also called `fresh`. It is used to +scaffold new projects and generate the route manifest. You will only need this +tool if you are creating a new project, or are creating or moving pages around +inside an existing project. The `fresh` CLI is not required for most development +or in CI, as `fresh` does not have a build step. To install `fresh`: + +``` +deno install -A -f --no-check https://raw.githubusercontent.com/lucacasonato/fresh/main/cli.ts +``` + +## Creating a new project + +Creating a new project can be done as follows: + +``` +fresh init my-project +``` + +The first argument passed to `fresh init` (in the above case `my-project`), is +the path to a folder you would like to initalize a `fresh` project in. To +initalize a project in the current working directory, pass `.` as the first +argument. + +Now move into the directory you just created / initalized (for example +`cd my-project`). + +To learn more about how projects are structured, take a look at the +[Project Structure](./project-structure.md) documentation. + +## Running the project locally + +You can now run your new project locally with `deployctl`: + +``` +deployctl run --watch --no-check ./main.ts +``` + +The `--watch` option will cause your script to be reloaded on any changes to the +source. If you do not want this you can omit the `--watch` option. + +After you have started the project, you can view it in your browser at +http://localhost:8080. You will first get served a JIT server rendered page, +which will get hydrated using client side JS after a few moments. + +## Deploying the project to Deno Deploy + +To deploy your project you will need to push the source code to either a web +server, or GitHub.com. Fresh currently only supports public GitHub repositories. +For this guide we will use GitHub.com and the Deno Deploy GitHub integration. + +First create a new repository on GitHub.com with the same account you signed up +to Deno Deploy with (or an organization you can access from that account). Then +follow the setup instructions for GitHub on your machine, and push your code +into the repository. + +Now go back to GitHub and open the `main.ts` file that you pushed. Copy the URL +in the address bar. It should look something like this: +`https://github.com/lucacasonato/fresh/blob/main/example/main.ts`. Visit your +project on dash.deno.com, enter the URL you copied into the +`Link GitHub repository` input box in the project settings, and press `Link`. +You might need to grant the Deno Deploy app permission to the repository at this +point (follow prompts on the Deno Deploy dashboard). + +Your project is now linked to Deno Deploy and will automatically deployed on +every push. Your project now has a URL you can visit to view your site live. +Example: https://fresh.deno.dev + +## Next steps + +Now that the project is deployed, it is time to make some changes to the default +project. For that read the guides on [project structure](./project-structure.md) +and [file system routing](./file-system-routing.md) diff --git a/docs/project-structure.md b/docs/project-structure.md new file mode 100644 index 00000000..7d11b6e0 --- /dev/null +++ b/docs/project-structure.md @@ -0,0 +1,25 @@ +# Project strcture + +Initalizing a fresh project will generate multiple files. For now the most +important files are `main.ts`, `routes.gen.ts`, and the pages and api routes in +the `pages` subfolder. + +The `main.ts` file is the entrypoint to your project. It is the file you link in +Deno Deploy, or run locally with `deployctl`. + +The `routes.gen.ts` file is a manifest of all pages and api routes in a project. +When moving around or creating new pages, this file needs to be updated. This is +done automatically when you run the `fresh routes` command. You never have to +edit this file manually. + +The files in the `pages` subfolder describe the HTML pages and API endpoints of +your application. Files inside the `pages/api` subdirectory are treated as API +routes, while all other files are treated as possibly dynamic HTML pages. More +info about how the `pages` subfolder works can be found on the +[file system routing](./file-system-routing.md) page. + +Finally there is a `deps.ts` and `api_deps.ts`. These files are the central +location for all external imports. The `deps.ts` file is used for runtime +dependencies (ones that are used both on the server and on the client), and the +`api_deps.ts` file is used for server only dependencies (for example ones used +in API routes). diff --git a/src/cli/routes.ts b/src/cli/routes.ts index 77e877a4..d17b0067 100644 --- a/src/cli/routes.ts +++ b/src/cli/routes.ts @@ -38,7 +38,7 @@ export async function routesSubcommand(rawArgs: Record) { export async function routes(directory: string) { const files = []; - const pagesDir = join(Deno.cwd(), directory, "./pages"); + const pagesDir = join(directory, "./pages"); const pagesUrl = new URL(pagesDir, "file:///"); const folder = walk(pagesDir, { includeDirs: false,