Skip to content

Commit

Permalink
Started making helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
taras committed Feb 13, 2024
1 parent b14fe1c commit c54e7eb
Show file tree
Hide file tree
Showing 18 changed files with 621 additions and 1 deletion.
2 changes: 1 addition & 1 deletion www/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"exclude": ["docs/esm"]
},
"compilerOptions": {
"lib": ["deno.ns", "dom.iterable", "dom"],
"lib": ["deno.ns", "dom.iterable", "dom", "deno.unstable"],
"jsx": "react-jsx",
"jsxImportSource": "revolution"
},
Expand Down
278 changes: 278 additions & 0 deletions www/deno.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions www/lib/use-cache-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useKv } from "./use-kv.ts";

export function* useCacheDirectory({ path }) {
const kv = yield* useKv();


}
8 changes: 8 additions & 0 deletions www/lib/use-cwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createContext, Operation } from 'effection';

export const CwdContext = createContext("Deno.cwd", Deno.cwd());

export function* useCwd(): Operation<string> {
const cwd = yield* CwdContext;
return cwd;
}
72 changes: 72 additions & 0 deletions www/lib/use-github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Octokit } from "npm:[email protected]";
import { createTokenAuth } from "npm:@octokit/auth-token";
import { assert } from "../../lib/deps.js";
import { call, createContext, resource, Operation } from "../../mod.js";

const GithubContext = createContext<Octokit>("octokit");

export function* setupGithub() {
const octokit = yield* resource<Octokit>(function* (provide) {
const token = Deno.env.get("GITHUB_TOKEN");

assert(token, "GITHUB_TOKEN environment variable is missing");

provide(
new Octokit({
auth: createTokenAuth(token),
}),
);
});

yield* GithubContext.set(octokit);
}

export function* useGithub() {
return yield* GithubContext;
}

export function* getEffectionReleaseTags(): Operation<string[]> {
const octokit = yield* useGithub();

return yield* call(() =>
octokit.paginate(
"GET /repos/{owner}/{repo}/tags",
{
owner: "thefrontside",
repo: "effection",
},
(response) =>
response.data
.filter(({ name }) =>
/^effection-v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(name),
)
.map(({ name }) => name),
),
);
}

export function* createEffectionRelease({
name,
tag_name,
body,
}: {
tag_name: string;
name?: string | undefined;
body?: string | undefined;
}) {
const octokit = yield* useGithub();

return yield* call(() =>
octokit.request("POST /repos/{owner}/{repo}/releases", {
owner: "thefrontside",
repo: "effection",
target_commitish: "master",
name,
tag_name,
body,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
}),
);
}
23 changes: 23 additions & 0 deletions www/lib/use-kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { call, createContext, resource } from 'effection';

const KV_URL = "https://api.deno.com/databases/2b605a51-5827-4718-bcc5-5adca69f87a5/connect";

const KvContext = createContext<Deno.Kv>("Deno.Kv")

export function* openKv() {
return (
yield *
resource<Deno.Kv>(function* (provide) {
const kv = yield* call(() => Deno.openKv(KV_URL));
try {
yield* provide(kv);
} finally {
kv.close();
}
})
);
}

export function* useKv() {
yield* KvContext;
}
14 changes: 14 additions & 0 deletions www/lib/use-mkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isAbsolute } from "https://deno.land/[email protected]/path/is_absolute.ts";
import { Operation, call } from "effection";
import { useCwd } from "./use-cwd.ts";
import { join } from "https://deno.land/[email protected]/path/join.ts";

export function* useMkdir(path: string, options?: Deno.MkdirOptions): Operation<void> {
if (isAbsolute(path)) {
return yield* call(() => Deno.mkdir(path, options));
} else {
const cwd = yield* useCwd();
console.log({ cwd })
return yield* call(() => Deno.mkdir(join(cwd, path), options));
}
}
69 changes: 69 additions & 0 deletions www/lib/use-read-dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Operation, Scope, Stream, each, resource, useScope } from "effection";
import { expect } from "https://deno.land/x/expect/mod.ts";
import { writeSync } from "https://deno.land/x/[email protected]/mod.ts";
import { beforeEach, describe, it } from "../test/deno.bdd.ts";
import { CwdContext, useCwd } from "./use-cwd.ts";
import { useMkdir } from "./use-mkdir.ts";
import { useReadDir } from "./use-read-dir.ts";

function* useTmpCwd(scope: Scope): Operation<string> {
return yield* resource<string>(function* (provide) {
const cwd = yield* useCwd();
const tmp = Deno.makeTempFileSync();

try {
console.log({ tmp });
scope.set(CwdContext, tmp);
provide(tmp);
} finally {
scope.set(CwdContext, cwd);
}
});
}

function* capture<T, TReturn>(stream: Stream<T, TReturn>): Operation<T[]> {
const result: T[] = [];
for (let value of yield* each(stream)) {
result.push(value);
yield* each.next();
}
return result;
}

describe("use-read-dir", () => {
beforeEach(function* () {
const scope = yield* useScope();
yield* useTmpCwd(scope);
yield* useMkdir("testdir");

writeSync("testdir", {
"index.html": "<html><body><h1>Effection Docs</h1></body></html>",
"fuse.js": `console.log("hello world")`,
"page.css": "body { background: black; }",
"~": {
"main.html": "<html><body><h1>main()</h1></body></html>",
},
});
});

it("reads files from tmp testdir", function* () {
expect(yield* capture(yield* useReadDir("testdir"))).toEqual([
{
name: "index.html",
isFile: true,
},
{
name: "page.css",
isFile: true,
},
{
name: 'fuse.js',
isFile: true,
},
{
name: '~',
isDirectory: true
}
]);
});
});
13 changes: 13 additions & 0 deletions www/lib/use-read-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Operation, stream, Stream } from "effection";
import { isAbsolute } from "https://deno.land/[email protected]/path/is_absolute.ts";
import { join } from "https://deno.land/[email protected]/path/join.ts";
import { useCwd } from "./use-cwd.ts";

export function* useReadDir(path: string): Operation<Stream<Deno.DirEntry, unknown>> {
if (isAbsolute(path)) {
return stream(Deno.readDir(path));
} else {
const cwd = yield* useCwd();
return stream(Deno.readDir(join(cwd, path)));
}
}
13 changes: 13 additions & 0 deletions www/lib/use-read-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isAbsolute } from "https://deno.land/[email protected]/path/is_absolute.ts";
import { join } from "https://deno.land/[email protected]/path/join.ts";
import { useCwd } from "./use-cwd.ts";
import { Operation, call } from "effection";

export function* useReadFile(path: string): Operation<Uint8Array> {
if (isAbsolute(path)) {
return yield* call(() => Deno.readFile(path));
} else {
const cwd = yield* useCwd();
return yield* call(() => Deno.readFile(join(cwd, path)));
}
}
27 changes: 27 additions & 0 deletions www/tasks/create-release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { main } from '../../lib/main.ts';
import { setupGithub } from '../lib/use-github.ts';
import { useCommand } from "../lib/use-command.ts";
import { assert } from "../../lib/deps.ts";

function* createRelease({ ref_name }: { ref_name: string }) {

const version = ref_name.replace('effection-v', '');

yield* useCommand(`deno doc --html --name=effection@${version} mod.ts`);

yield* useCommand(`tar cfvz api-docs.tgz -C docs .`);


}

await main(function*() {
yield* setupGithub();

const ref_name = Deno.env.get('GITHUB_REF_NAME');
assert(ref_name, `Missing GITHUB_REF_NAME environment variable`)

yield* createRelease({
ref_name,
});
})

12 changes: 12 additions & 0 deletions www/tasks/recreate-releases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { main } from "../lib/main.ts";
import { setupGithub, getEffectionReleaseTags } from "../www/lib/use-github.ts";

function* recreateReleases() {
const tags = yield* getEffectionReleaseTags();

}

await main(function* () {
yield* setupGithub();
yield* recreateReleases();
});
46 changes: 46 additions & 0 deletions www/test/deno.bdd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as bdd from "https://deno.land/[email protected]/testing/bdd.ts";
import { TestScope, createTestScope } from "./test-scope.ts";
import type { Operation } from "effection";

let scope: TestScope | void = void 0;

// Integrate test scope with the Deno test runner by wrapping the `describe()`, `it()` and `beforeEach()` methods.
function describeWithScope<T>(...args: bdd.DescribeArgs<T>): bdd.TestSuite<T> {
let [name, def] = args;
return bdd.describe(name as string, () => {
bdd.beforeEach(() => {
if (!scope) {
scope = createTestScope();
}
});

if (def && typeof def === "function") {
def();
}
});
}

describeWithScope.only = bdd.describe.only;
describeWithScope.ignore = bdd.describe.ignore;

export const describe: typeof bdd.describe = describeWithScope;

export function beforeEach(op: () => Operation<void>): void {
bdd.beforeEach(() => scope!.addSetup(op));
}

export function it(desc: string, op?: () => Operation<void>): void {
if (op) {
return bdd.it(desc, () => scope!.runTest(op));
} else {
return bdd.it.ignore(desc, () => {});
}
}

it.only = function only(desc: string, op?: () => Operation<void>): void {
if (op) {
return bdd.it.only(desc, () => scope!.runTest(op));
} else {
return bdd.it.ignore(desc, () => {});
}
};
34 changes: 34 additions & 0 deletions www/test/test-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { run, type Operation } from "effection";
/**
* Source https://gist.github.com/cowboyd/70e4596e61aadafd852f403dc6d8ec6f#file-01-test-scope-ts
*/
export interface TestScope {
/**
* Call from your runner's "beforeEach" or equivalent
*/
addSetup(op: () => Operation<void>): void;

/**
* Call from runner's `it()` or equivalent;
*/
runTest(op: () => Operation<void>): Promise<void>;
}

export function createTestScope(): TestScope {
let setup = [] as Array<() => Operation<void>>;

return {
addSetup(op) {
setup.push(op);
},

runTest(op) {
return run(function* () {
for (let step of setup) {
yield* step();
}
yield* op();
});
},
};
}
1 change: 1 addition & 0 deletions www/testdir/fuse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello world")
1 change: 1 addition & 0 deletions www/testdir/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body><h1>Effection Docs</h1></body></html>
1 change: 1 addition & 0 deletions www/testdir/page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { background: black; }
1 change: 1 addition & 0 deletions www/testdir/~/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body><h1>main()</h1></body></html>

0 comments on commit c54e7eb

Please sign in to comment.