mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 10:09:20 -05:00
Add examples/ directory (denoland/deno_std#28)
Previously https://github.com/denoland/deno_examples
Original: 14be9a0e82
This commit is contained in:
parent
ecde6a4c50
commit
3542f2de0c
6 changed files with 122 additions and 0 deletions
16
examples/README.md
Normal file
16
examples/README.md
Normal file
|
@ -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"
|
||||
```
|
11
examples/cat.ts
Normal file
11
examples/cat.ts
Normal file
|
@ -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));
|
11
examples/echo_server.ts
Normal file
11
examples/echo_server.ts
Normal file
|
@ -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);
|
||||
}
|
||||
})();
|
63
examples/gist.ts
Executable file
63
examples/gist.ts
Executable file
|
@ -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();
|
20
examples/test.ts
Normal file
20
examples/test.ts
Normal file
|
@ -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);
|
||||
});
|
1
test.ts
1
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
|
||||
|
|
Loading…
Reference in a new issue