summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-12-19 13:50:48 -0500
committerGitHub <noreply@github.com>2018-12-19 13:50:48 -0500
commit3542f2de0cecdbcf07d3bea3de6439e6a87376df (patch)
tree53c39ae3456fa8d6a360485378e8d82949b09e9a
parentecde6a4c503584aa712f40e70c8e1aa78959e110 (diff)
Add examples/ directory (denoland/deno_std#28)
Previously https://github.com/denoland/deno_examples Original: https://github.com/denoland/deno_std/commit/14be9a0e82d1b54d5b0f04291d9154d1d7da29f7
-rw-r--r--examples/README.md16
-rw-r--r--examples/cat.ts11
-rw-r--r--examples/echo_server.ts11
-rwxr-xr-xexamples/gist.ts63
-rw-r--r--examples/test.ts20
-rwxr-xr-xtest.ts1
6 files changed, 122 insertions, 0 deletions
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 000000000..2e920f479
--- /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 000000000..d8862d424
--- /dev/null
+++ b/examples/cat.ts
@@ -0,0 +1,11 @@
+import * as deno from "deno";
+
+async function cat(filenames: string[]): Promise<void> {
+ 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 000000000..1d5b287db
--- /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 000000000..04a123560
--- /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 000000000..4d2dea157
--- /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 94534825d..703ce639d 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