diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000000..2e920f4796 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,16 @@ +# Deno Example Programs + +These files are accessible for import via "https://deno.land/x/examples/". + +Try it: +``` +> deno https://deno.land/x/examples/gist.ts README.md +``` + +## Alias Based Installation + +Add this to your `.bash_profile` +``` +export GIST_TOKEN=ABC # Generate at https://github.com/settings/tokens +alias gist="deno https://deno.land/x/examples/gist.ts --allow-net --allow-env" +``` diff --git a/examples/cat.ts b/examples/cat.ts new file mode 100644 index 0000000000..d8862d4249 --- /dev/null +++ b/examples/cat.ts @@ -0,0 +1,11 @@ +import * as deno from "deno"; + +async function cat(filenames: string[]): Promise { + for (let filename of filenames) { + let file = await deno.open(filename); + await deno.copy(deno.stdout, file); + file.close(); + } +} + +cat(deno.args.slice(1)); diff --git a/examples/echo_server.ts b/examples/echo_server.ts new file mode 100644 index 0000000000..1d5b287db7 --- /dev/null +++ b/examples/echo_server.ts @@ -0,0 +1,11 @@ +import { listen, copy } from "deno"; + +(async () => { + const addr = "0.0.0.0:8080"; + const listener = listen("tcp", addr); + console.log("listening on", addr); + while (true) { + const conn = await listener.accept(); + copy(conn, conn); + } +})(); diff --git a/examples/gist.ts b/examples/gist.ts new file mode 100755 index 0000000000..04a1235604 --- /dev/null +++ b/examples/gist.ts @@ -0,0 +1,63 @@ +#!/usr/bin/env deno --allow-net --allow-env + +import { args, env, exit, readFile } from "deno"; +import parseArgs from "https://deno.land/x/parseargs/index.ts"; + +function pathBase(p: string): string { + const parts = p.split("/"); + return parts[parts.length - 1]; +} + +async function main() { + const token = env()["GIST_TOKEN"]; + if (!token) { + console.error("GIST_TOKEN environmental variable not set."); + console.error("Get a token here: https://github.com/settings/tokens"); + exit(1); + } + + const parsedArgs = parseArgs(args.slice(1)); + + if (parsedArgs._.length === 0) { + console.error( + "Usage: gist.ts --allow-env --allow-net [-t|--title Example] some_file [next_file]" + ); + exit(1); + } + + const files = {}; + for (const filename of parsedArgs._) { + const base = pathBase(filename); + const content = await readFile(filename); + const contentStr = new TextDecoder().decode(content); + files[base] = { content: contentStr }; + } + + const content = { + description: parsedArgs.title || parsedArgs.t || "Example", + public: false, + files: files + }; + const body = JSON.stringify(content); + + const res = await fetch("https://api.github.com/gists", { + method: "POST", + headers: [ + ["Content-Type", "application/json"], + ["User-Agent", "Deno-Gist"], + ["Authorization", "token " + token] + ], + body + }); + + if (res.ok) { + let resObj = await res.json(); + console.log("Success"); + console.log(resObj["html_url"]); + } else { + let err = await res.text(); + console.error("Failure to POST", err); + } +} + +main(); diff --git a/examples/test.ts b/examples/test.ts new file mode 100644 index 0000000000..4d2dea1571 --- /dev/null +++ b/examples/test.ts @@ -0,0 +1,20 @@ +import { run } from "deno"; +import { test, assertEqual } from "https://deno.land/x/testing/testing.ts"; + +/** Example of how to do basic tests */ +test(function t1() { + assertEqual("hello", "hello"); +}); + +test(function t2() { + assertEqual("world", "world"); +}); + +/** A more complicated test that runs a subprocess. */ +test(async function catSmoke() { + const p = run({ + args: ["deno", "examples/cat.ts", "README.md"] + }); + const s = await p.status(); + assertEqual(s.code, 0); +}); diff --git a/test.ts b/test.ts index 94534825da..703ce639d6 100755 --- a/test.ts +++ b/test.ts @@ -11,6 +11,7 @@ import "flags/test.ts"; import "net/bufio_test.ts"; import "net/http_test.ts"; import "net/textproto_test.ts"; +import "examples/test.ts"; import { runTests, completePromise } from "net/file_server_test.ts"; // logging tests