From 080ee94bf5feb78e2feccec27734b27d9e5122c1 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 30 Apr 2015 21:04:45 -0700 Subject: [PATCH 1/2] initial version of UMD/TypeScript sample --- umd/.gitignore | 4 ++++ umd/README.md | 22 ++++++++++++++++++++++ umd/browser/app.css | 8 ++++++++ umd/browser/app.ts | 13 +++++++++++++ umd/index.html | 15 +++++++++++++++ umd/node/app.ts | 8 ++++++++ umd/node/os.d.ts | 3 +++ umd/package.json | 19 +++++++++++++++++++ umd/shared/timeReporter.ts | 23 +++++++++++++++++++++++ umd/tsconfig.json | 12 ++++++++++++ 10 files changed, 127 insertions(+) create mode 100644 umd/.gitignore create mode 100644 umd/README.md create mode 100644 umd/browser/app.css create mode 100644 umd/browser/app.ts create mode 100644 umd/index.html create mode 100644 umd/node/app.ts create mode 100644 umd/node/os.d.ts create mode 100644 umd/package.json create mode 100644 umd/shared/timeReporter.ts create mode 100644 umd/tsconfig.json diff --git a/umd/.gitignore b/umd/.gitignore new file mode 100644 index 0000000..d751509 --- /dev/null +++ b/umd/.gitignore @@ -0,0 +1,4 @@ +node_modules +shared/timeReporter.js +browser/app.js +node/app.js \ No newline at end of file diff --git a/umd/README.md b/umd/README.md new file mode 100644 index 0000000..99c298d --- /dev/null +++ b/umd/README.md @@ -0,0 +1,22 @@ +**Fetch dependencies:** +``` +npm install +``` + +**Build and run sample in browser (using port 8080)** +``` +npm run browser +``` + +**Build and run sample using NodeJS** +``` +npm run node +``` + +**Run http-server on custom port (for browser version)** +``` +node node_modules/http-server/bin/http-server -p 8080 -o +``` + +'-p' sets the port to use, default port is 8080. If it is taken pick any port that is free. +After server is started open 'localhost:8080' in a browser. \ No newline at end of file diff --git a/umd/browser/app.css b/umd/browser/app.css new file mode 100644 index 0000000..eb24fc6 --- /dev/null +++ b/umd/browser/app.css @@ -0,0 +1,8 @@ +body +{ + font-family: 'Segoe UI', sans-serif +} + +span { + font-style: italic +} \ No newline at end of file diff --git a/umd/browser/app.ts b/umd/browser/app.ts new file mode 100644 index 0000000..a2445f7 --- /dev/null +++ b/umd/browser/app.ts @@ -0,0 +1,13 @@ +import { TimeReporter } from '../shared/timeReporter' + +let element = document.getElementById("content"); +element.innerText += "The time is: "; +let span = document.createElement("span"); +element.appendChild(span); + +function printer(s: string): void { + span.innerText = s; +} + +let timeReporter = new TimeReporter(printer); +timeReporter.start(); diff --git a/umd/index.html b/umd/index.html new file mode 100644 index 0000000..b0f2761 --- /dev/null +++ b/umd/index.html @@ -0,0 +1,15 @@ + + + + + + TypeScript HTML App + + + + +

UMD/TypeScript sample

+ +
+ + \ No newline at end of file diff --git a/umd/node/app.ts b/umd/node/app.ts new file mode 100644 index 0000000..2dd78fe --- /dev/null +++ b/umd/node/app.ts @@ -0,0 +1,8 @@ +import { TimeReporter } from '../shared/timeReporter' +import { EOL } from 'os'; +function printer(s): void { + console.log("The time is: " + s + EOL); +} + +let timeReporter = new TimeReporter(printer); +timeReporter.start(); \ No newline at end of file diff --git a/umd/node/os.d.ts b/umd/node/os.d.ts new file mode 100644 index 0000000..e817365 --- /dev/null +++ b/umd/node/os.d.ts @@ -0,0 +1,3 @@ +declare module "os" { + export var EOL: string; +} \ No newline at end of file diff --git a/umd/package.json b/umd/package.json new file mode 100644 index 0000000..34dea5a --- /dev/null +++ b/umd/package.json @@ -0,0 +1,19 @@ +{ + "name": "typescript-umd", + "version": "1.0.0", + "description": "UMD/TypeScript demo", + "repository": { + "type": "git", + "url": "https://github.com/Microsoft/TypeScriptSamples.git" + }, + "dependencies": { + "http-server": "0.8.0" + }, + "devDependencies": { + "typescript": "mhegazy/typescript#v1.5-beta2" + }, + "scripts": { + "browser": "node node_modules/typescript/bin/tsc.js && node node_modules/http-server/bin/http-server browser -o", + "node": "node node_modules/typescript/bin/tsc.js && node node/app.js" + } +} \ No newline at end of file diff --git a/umd/shared/timeReporter.ts b/umd/shared/timeReporter.ts new file mode 100644 index 0000000..2832cc4 --- /dev/null +++ b/umd/shared/timeReporter.ts @@ -0,0 +1,23 @@ +export type Printer = (s: string) => void; + +export class TimeReporter +{ + printer: Printer + timerToken: number; + + constructor (printer: Printer) + { + this.printer = printer; + this.printer(new Date().toUTCString()); + } + + start() + { + this.timerToken = setInterval(() => this.printer(new Date().toUTCString()), 500); + } + + stop() + { + clearTimeout(this.timerToken); + } +} diff --git a/umd/tsconfig.json b/umd/tsconfig.json new file mode 100644 index 0000000..31e412e --- /dev/null +++ b/umd/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "UMD", + "target": "es5" + }, + "files": [ + "shared/timeReporter.ts", + "browser/app.ts", + "node/os.d.ts", + "node/app.ts" + ] +} \ No newline at end of file From 2e595e5a6d5c0a465b5720a2222433f273959ef0 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 7 Aug 2015 18:21:20 -0700 Subject: [PATCH 2/2] update reference to typescript package --- umd/README.md | 30 ++++++++++++++++++++++-------- umd/package.json | 4 ++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/umd/README.md b/umd/README.md index 99c298d..76ab775 100644 --- a/umd/README.md +++ b/umd/README.md @@ -3,20 +3,34 @@ npm install ``` -**Build and run sample in browser (using port 8080)** +**Compile TypeScript source code** + ``` -npm run browser +node node_modules/typescript/bin/tsc.js ``` -**Build and run sample using NodeJS** +**Start browser version of the sample using http-server on custom port (code should be compiled prior to this step)** ``` -npm run node +node node_modules/http-server/bin/http-server -p 8080 -o ``` +'-p' sets the port to use, default port is 8080. If it is taken pick any port that is free. +After server is started open 'localhost:8080' in a browser. + + +**Run example using Node (code should be compiled prior to this step)** -**Run http-server on custom port (for browser version)** ``` -node node_modules/http-server/bin/http-server -p 8080 -o +node node/app.js ``` -'-p' sets the port to use, default port is 8080. If it is taken pick any port that is free. -After server is started open 'localhost:8080' in a browser. \ No newline at end of file +Shortcuts for doing compile\run steps from above: + +*Browser* +``` +npm run browser +``` + +*Node* +``` +npm run node +``` \ No newline at end of file diff --git a/umd/package.json b/umd/package.json index 34dea5a..c04ab60 100644 --- a/umd/package.json +++ b/umd/package.json @@ -10,10 +10,10 @@ "http-server": "0.8.0" }, "devDependencies": { - "typescript": "mhegazy/typescript#v1.5-beta2" + "typescript": "^1.5.3" }, "scripts": { - "browser": "node node_modules/typescript/bin/tsc.js && node node_modules/http-server/bin/http-server browser -o", + "browser": "node node_modules/typescript/bin/tsc.js && node node_modules/http-server/bin/http-server -o", "node": "node node_modules/typescript/bin/tsc.js && node node/app.js" } } \ No newline at end of file