Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 79e1fbd

Browse files
committed
Async Functions Sample for Node v4.0.0
1 parent 7546da7 commit 79e1fbd

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,6 @@ FakesAssemblies/
187187
# LightSwitch generated files
188188
GeneratedArtifacts/
189189
_Pvt_Extensions/
190-
ModelManifest.xml
190+
ModelManifest.xml
191+
192+
node_modules/

async/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/
2+
.vscode/

async/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Async Functions
2+
3+
*Async Functions* are functions that can suspend their execution to wait for the completion of an
4+
asynchronous operation. This allows complex algorithms that require asynchronous control flow to
5+
be written sequentially.
6+
7+
This sample uses *Async Functions* to query the GitHub API to view information about recent pull
8+
requests.
9+
10+
This sample requires a minimum of NodeJS v4.0.0.
11+
12+
**Fetch dependencies**
13+
```
14+
npm install
15+
```
16+
17+
**Run**
18+
```
19+
npm test
20+
```
21+
22+
**Environment variables**
23+
24+
| Name | Description |
25+
|:------------------|:----------------------------------------------------------------------------|
26+
| GITHUB_TOKEN | The Authentication token to use for GitHub API Requests. (Optional) |
27+
| GITHUB_REPOSITORY | The GitHub repository to use for queries. (Default: "Microsoft/TypeScript") |

async/gulpfile.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var gulp = require('gulp')
2+
, sourcemaps = require('gulp-sourcemaps')
3+
, ts = require('gulp-typescript')
4+
, typescript = require('typescript')
5+
, del = require('del')
6+
, merge = require('merge2')
7+
, path = require('path')
8+
, spawn = require('child_process').spawn;
9+
10+
var lib = {
11+
project: ts.createProject('./src/lib/tsconfig.json', { typescript: typescript }),
12+
bin: "./bin/ts-async-github-sample",
13+
main: "./lib/github",
14+
base: "./src/lib/",
15+
dest: "./lib/",
16+
src: ["./src/lib/**/*.ts"],
17+
out: ["./lib/**/*"]
18+
};
19+
20+
gulp.task("clean:lib", clean(lib));
21+
gulp.task("clean", ["clean:lib"]);
22+
gulp.task("build:lib", build(lib));
23+
gulp.task("build", ["build:lib"]);
24+
gulp.task("test", ["build"], test(lib));
25+
gulp.task("watch:lib", ["build:lib"], watch(src(lib), ["build:lib"]));
26+
gulp.task("watch", ["build", "test"], watch(src(lib), ["test"]));
27+
gulp.task("default", ["build"]);
28+
29+
function src() {
30+
return Array.from(arguments).reduce(function (ar, opts) {
31+
return ar.concat(opts.src);
32+
}, []);
33+
}
34+
35+
function clean(opts) {
36+
return function (done) {
37+
del(opts.out, done);
38+
};
39+
}
40+
41+
function build(opts) {
42+
return function () {
43+
var tee = gulp
44+
.src(opts.src, { base: opts.base })
45+
.pipe(sourcemaps.init())
46+
.pipe(ts(opts.project));
47+
return merge([
48+
tee.dts
49+
.pipe(gulp.dest(opts.dest)),
50+
tee.js
51+
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: path.relative(opts.dest, opts.base) }))
52+
.pipe(gulp.dest(opts.dest))
53+
]);
54+
};
55+
}
56+
57+
function test(opts) {
58+
return function (done) {
59+
var args = [opts.bin];
60+
console.log("Executing test...");
61+
spawn(process.argv[0], args, { stdio: "inherit" }).on("exit", function (code) {
62+
done(code !== 0 ? "Error executing script." : undefined);
63+
});
64+
};
65+
}
66+
67+
function watch(src, tasks) {
68+
return function () {
69+
return gulp.watch(src, tasks);
70+
}
71+
}

async/jsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6"
4+
}
5+
}

async/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "ts-node-github-async-sample",
3+
"version": "1.0.0",
4+
"description": "Sample for Async Functions in NodeJS v4.0.0",
5+
"private": true,
6+
"main": "lib/github.js",
7+
"bin": {
8+
"sample": "./bin/ts-async-github-sample"
9+
},
10+
"dependencies": {},
11+
"devDependencies": {
12+
"del": "^2.0.2",
13+
"gulp": "^3.9.0",
14+
"gulp-sourcemaps": "^1.5.2",
15+
"gulp-typescript": "^2.9.0",
16+
"merge2": "^0.3.6",
17+
"typescript": "^1.7.0"
18+
},
19+
"scripts": {
20+
"test": "./node_modules/.bin/gulp test"
21+
},
22+
"author": "Microsoft Corp.",
23+
"license": "Apache-2.0",
24+
"engines": {
25+
"node": ">=4.0.0"
26+
}
27+
}

0 commit comments

Comments
 (0)