Skip to content

Commit

Permalink
make fresh-project executable
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Apr 17, 2024
1 parent 4eaf5e5 commit cd607f6
Show file tree
Hide file tree
Showing 27 changed files with 451 additions and 88 deletions.
7 changes: 1 addition & 6 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,7 @@ impl DenoRuntime {

let op_fs = {
if is_user_worker {
Arc::new(sb_fs::static_fs::StaticFs::new(
static_files,
vfs_path,
vfs,
npm_snapshot,
)) as Arc<dyn deno_fs::FileSystem>
Arc::new(DenoCompileFileSystem::from_rc(vfs)) as Arc<dyn deno_fs::FileSystem>
} else {
Arc::new(DenoCompileFileSystem::from_rc(vfs)) as Arc<dyn deno_fs::FileSystem>
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sb_core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ const MOCK_FN = () => {/* do nothing */};
const DENIED_DENO_FS_API_LIST = ObjectKeys(fsVars)
.reduce(
(acc, it) => {
acc[it] = false;
acc[it] = true;
return acc;
},
{}
Expand Down
12 changes: 7 additions & 5 deletions crates/sb_core/js/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ class PermissionStatus extends EventTarget {
}

[SymbolFor("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${
inspect({ state: this.state, onchange: this.onchange })
}`;
return `${this.constructor.name} ${inspect({ state: this.state, onchange: this.onchange })
}`;
}
}

Expand Down Expand Up @@ -195,6 +194,9 @@ class Permissions {
}

formDescriptor(desc);
if (desc.name === "run") {
return cache(desc, "prompt");
}

const state = opQuery(desc);
return cache(desc, state);
Expand Down Expand Up @@ -251,7 +253,7 @@ function serializePermissions(permissions) {
const serializedPermissions = {};
for (
const key of new SafeArrayIterator(["read", "write", "run", "ffi"])
) {
) {
if (ArrayIsArray(permissions[key])) {
serializedPermissions[key] = ArrayPrototypeMap(
permissions[key],
Expand All @@ -263,7 +265,7 @@ function serializePermissions(permissions) {
}
for (
const key of new SafeArrayIterator(["env", "hrtime", "net", "sys"])
) {
) {
if (ArrayIsArray(permissions[key])) {
serializedPermissions[key] = ArrayPrototypeSlice(permissions[key]);
} else {
Expand Down
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
"singleQuote": true,
"proseWrap": "preserve",
"include": ["examples/**/*.ts"]
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact",
"experimentalDecorators": true
}
}
3 changes: 3 additions & 0 deletions deno.lock

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

11 changes: 11 additions & 0 deletions examples/fresh-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# Fresh build directory
_fresh/
# npm dependencies
node_modules/
5 changes: 5 additions & 0 deletions examples/fresh-project/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"denoland.vscode-deno"
]
}
17 changes: 17 additions & 0 deletions examples/fresh-project/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"deno.enable": true,
"deno.lint": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[javascriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
16 changes: 16 additions & 0 deletions examples/fresh-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Fresh project

Your new Fresh project is ready to go. You can follow the Fresh "Getting
Started" guide here: https://fresh.deno.dev/docs/getting-started

### Usage

Make sure to install Deno: https://deno.land/manual/getting_started/installation

Then start the project:

```
deno task start
```

This will watch the project directory and restart as necessary.
12 changes: 12 additions & 0 deletions examples/fresh-project/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { JSX } from "preact";
import { IS_BROWSER } from "$fresh/runtime.ts";

export function Button(props: JSX.HTMLAttributes<HTMLButtonElement>) {
return (
<button
{...props}
disabled={!IS_BROWSER || props.disabled}
class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors"
/>
);
}
35 changes: 35 additions & 0 deletions examples/fresh-project/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"lock": false,
"tasks": {
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
"manifest": "deno task cli manifest $(pwd)",
"start": "deno run -A --watch=static/,routes/ dev.ts",
"build": "deno run -A dev.ts build",
"preview": "deno run -A main.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
},
"lint": {
"rules": {
"tags": [
"fresh",
"recommended"
]
}
},
"exclude": [
"**/_fresh/*"
],
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"$std/": "https://deno.land/[email protected]/"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
8 changes: 8 additions & 0 deletions examples/fresh-project/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/

import dev from "$fresh/dev.ts";
import config from "./fresh.config.ts";

import "$std/dotenv/load.ts";

await dev(import.meta.url, "./main.ts", config);
3 changes: 3 additions & 0 deletions examples/fresh-project/fresh.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "$fresh/server.ts";

export default defineConfig({});
27 changes: 27 additions & 0 deletions examples/fresh-project/fresh.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import * as $_404 from './routes/_404.tsx';
import * as $_app from './routes/_app.tsx';
import * as $api_joke from './routes/api/joke.ts';
import * as $greet_name_ from './routes/greet/[name].tsx';
import * as $index from './routes/index.tsx';
import * as $Counter from './islands/Counter.tsx';
import { type Manifest } from '$fresh/server.ts';

const manifest = {
routes: {
'./routes/_404.tsx': $_404,
'./routes/_app.tsx': $_app,
'./routes/api/joke.ts': $api_joke,
'./routes/greet/[name].tsx': $greet_name_,
'./routes/index.tsx': $index,
},
islands: {
'./islands/Counter.tsx': $Counter,
},
baseUrl: import.meta.url,
} satisfies Manifest;

export default manifest;
13 changes: 13 additions & 0 deletions examples/fresh-project/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import "$std/dotenv/load.ts";

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import config from "./fresh.config.ts";

await start(manifest, config);
16 changes: 16 additions & 0 deletions examples/fresh-project/islands/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Signal } from "@preact/signals";
import { Button } from "../components/Button.tsx";

interface CounterProps {
count: Signal<number>;
}

export default function Counter(props: CounterProps) {
return (
<div class="flex gap-8 py-6">
<Button onClick={() => props.count.value -= 1}>-1</Button>
<p class="text-3xl tabular-nums">{props.count}</p>
<Button onClick={() => props.count.value += 1}>+1</Button>
</div>
);
}
13 changes: 13 additions & 0 deletions examples/fresh-project/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import "$std/dotenv/load.ts";

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import config from "./fresh.config.ts";

await start(manifest, config);
27 changes: 27 additions & 0 deletions examples/fresh-project/routes/_404.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Head } from "$fresh/runtime.ts";

export default function Error404() {
return (
<>
<Head>
<title>404 - Page not found</title>
</Head>
<div class="px-4 py-8 mx-auto bg-[#86efac]">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
<img
class="my-6"
src="/logo.svg"
width="128"
height="128"
alt="the Fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-4xl font-bold">404 - Page not found</h1>
<p class="my-4">
The page you were looking for doesn't exist.
</p>
<a href="/" class="underline">Go back home</a>
</div>
</div>
</>
);
}
17 changes: 17 additions & 0 deletions examples/fresh-project/routes/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @jsxImportSource preact */
import { type PageProps } from "$fresh/server.ts";
export default function App({ Component }: PageProps) {
return (
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>fresh-project</title>
<link rel="stylesheet" href="/styles.css" />
</head>
<body>
<Component />
</body>
</html>
);
}
21 changes: 21 additions & 0 deletions examples/fresh-project/routes/api/joke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FreshContext } from "$fresh/server.ts";

// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/
const JOKES = [
"Why do Java developers often wear glasses? They can't C#.",
"A SQL query walks into a bar, goes up to two tables and says “can I join you?”",
"Wasn't hard to crack Forrest Gump's password. 1forrest1.",
"I love pressing the F5 key. It's refreshing.",
"Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”",
"There are 10 types of people in the world. Those who understand binary and those who don't.",
"Why are assembly programmers often wet? They work below C level.",
"My favourite computer based band is the Black IPs.",
"What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.",
"An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.",
];

export const handler = (_req: Request, _ctx: FreshContext): Response => {
const randomIndex = Math.floor(Math.random() * JOKES.length);
const body = JOKES[randomIndex];
return new Response(body);
};
5 changes: 5 additions & 0 deletions examples/fresh-project/routes/greet/[name].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PageProps } from "$fresh/server.ts";

export default function Greet(props: PageProps) {
return <div>Hello {props.params.name}</div>;
}
25 changes: 25 additions & 0 deletions examples/fresh-project/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useSignal } from "@preact/signals";
import Counter from "../islands/Counter.tsx";

export default function Home() {
const count = useSignal(3);
return (
<div class="px-4 py-8 mx-auto bg-[#86efac]">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
<img
class="my-6"
src="/logo.svg"
width="128"
height="128"
alt="the Fresh logo: a sliced lemon dripping with juice"
/>
<h1 class="text-4xl font-bold">Welcome to Fresh</h1>
<p class="my-4">
Try updating this message in the
<code class="mx-2">./routes/index.tsx</code> file, and refresh.
</p>
<Counter count={count} />
</div>
</div>
);
}
Binary file added examples/fresh-project/static/favicon.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/fresh-project/static/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cd607f6

Please sign in to comment.