Skip to content

Commit 9b8eefc

Browse files
committed
feat: add TypeScript framework-ts example
need commits of eggjs/egg#2321
1 parent 207d223 commit 9b8eefc

23 files changed

+971
-0
lines changed

framework-ts/app/.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- '8'
5+
install:
6+
- npm i npminstall && npminstall
7+
script:
8+
- npm run ci
9+
after_script:
10+
- npminstall codecov && codecov
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Controller, Service } from 'yadan-ts'
2+
3+
export default class HomeController extends Controller {
4+
async render() {
5+
const { ctx } = this
6+
7+
// use service defined in framework
8+
const data = await ctx.service.test.get(123);
9+
await ctx.render('home.tpl', data);
10+
}
11+
}
12+
13+
declare module 'egg' {
14+
export interface IController {
15+
home: HomeController;
16+
}
17+
}
18+
19+
// @FIXME
20+
declare module 'egg' {
21+
export interface IService {
22+
test: any
23+
}
24+
}

framework-ts/app/app/router.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from 'egg'
2+
3+
export default (app: Application) => {
4+
const controller = app.controller
5+
6+
app.router.get('/', app.controller.home.render);
7+
}

framework-ts/app/app/view/home.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi, {{ name }}

framework-ts/app/appveyor.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
environment:
2+
matrix:
3+
- nodejs_version: '8'
4+
5+
install:
6+
- ps: Install-Product node $env:nodejs_version
7+
- npm i npminstall && node_modules\.bin\npminstall
8+
9+
test_script:
10+
- node --version
11+
- npm --version
12+
- npm run test
13+
14+
build: off

framework-ts/app/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "framework-ts-example",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"egg": "^2.6.0",
6+
"egg-scripts": "^2.6.0",
7+
"yadan-ts": "../yadan"
8+
},
9+
"devDependencies": {
10+
"egg-bin": "^4.3.5",
11+
"egg-mock": "^3.13.1"
12+
},
13+
"engines": {
14+
"node": ">=8.10.0"
15+
},
16+
"scripts": {
17+
"autod": "autod",
18+
"clean": "rimraf app/**/*.{js,map} test/**/*.{js,map} config/**/*.{js,map}",
19+
"ci": "npm run lint && npm run cov",
20+
"cov": "egg-bin cov",
21+
"debug": "egg-bin debug",
22+
"dev": "egg-bin dev",
23+
"lint": "tslint --fix -p tsconfig.json -t stylish",
24+
"test": "npm run tsc && npm run test-local",
25+
"test-local": "egg-bin test",
26+
"start": "egg-scripts start --daemon --port=7001 --title=egg-win-test --workers=2",
27+
"stop": "egg-scripts stop --title=egg-win-test",
28+
"tsc": "tsc -p tsconfig.json",
29+
"tsc:w": "tsc -p tsconfig.json -w"
30+
},
31+
"egg": {
32+
"framework": "yadan-ts"
33+
}
34+
}

framework-ts/app/tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
4+
"charset": "utf8",
5+
"declaration": true, /* Generates corresponding '.d.ts' file. */
6+
"experimentalDecorators": true,
7+
"emitDecoratorMetadata": true,
8+
"importHelpers": false,
9+
"inlineSourceMap": true,
10+
"module": "commonjs", /* 'commonjs', 'amd', 'system', 'umd' or 'es2015'. */
11+
"newLine": "lf",
12+
"noFallthroughCasesInSwitch": true,
13+
// "outDir": "./dist",
14+
"pretty": true,
15+
"skipLibCheck": true,
16+
"sourceMap": false,
17+
"strict": true, /* Enable all strict type-checking options. */
18+
// "noImplicitAny": false,
19+
// "noImplicitThis": false,
20+
"target": "ES2017",
21+
"types" : ["node"]
22+
},
23+
"include": [
24+
"app/**/*.ts",
25+
"config/**/*.ts",
26+
"test/**/*.ts"
27+
],
28+
"exclude": [
29+
"app/public",
30+
"app/views",
31+
"node_modules",
32+
"**/*.d.ts",
33+
"**/*.spec.ts"
34+
]
35+
}

framework-ts/app/tslint.json

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"extends": ["tslint:latest", "tslint-eslint-rules"],
3+
"linterOptions": {
4+
"exclude": [
5+
"node_modules",
6+
"*.d.ts"
7+
]
8+
},
9+
"rules": {
10+
"adjacent-overload-signatures": true,
11+
"align": [true, "members", "parameters", "statements"],
12+
"array-bracket-spacing": [true, "never", {"singleValue": false, "objectsInArrays": true, "arraysInArrays": true} ], // 设置在括号内使用空格风格
13+
"arrow-parens": [true, "ban-single-arg-parens"],
14+
"await-promise": [true, "Thenable"], // Warns for an awaited value that is not a Promise.
15+
"ban-comma-operator": true,
16+
"block-spacing": [true, "always"], // 强制在单行代码块中使用一致的空格
17+
"brace-style": [true, "stroustrup", { "allowSingleLine": true }], // 大括号风格要求
18+
"comment-format": [true, "check-space", {"ignore-words": ["-", "+"]}], // 强制在注释中 // 或 /* 使用一致的空格
19+
"curly": [true, "ignore-same-line"], // 要求遵循大括号约定
20+
"eofline": true,
21+
"forin": false,
22+
"import-blacklist": [
23+
true,
24+
"rxjs",
25+
"rxjs/Rx"
26+
],
27+
"indent": [true, "spaces", 2],
28+
"interface-name": [ false, "never-prefix" ],
29+
"linebreak-style": [true , "LF" ],
30+
"max-classes-per-file": [ true, 10 ],
31+
"max-line-length": [true, 120],
32+
"member-access": [ false ],
33+
"member-ordering": [true, {"order": [
34+
"public-static-field",
35+
"public-instance-field",
36+
"public-constructor",
37+
"private-static-field",
38+
"private-instance-field",
39+
"private-constructor",
40+
"public-instance-method",
41+
"protected-instance-method",
42+
"private-instance-method"
43+
]}],
44+
"no-arg": true,
45+
"no-angle-bracket-type-assertion": false,
46+
"no-consecutive-blank-lines": [true, 4], // 多个空行控制
47+
"no-console": [ true, "dir", "log", "warn" ],
48+
"no-construct": true, // 禁止对 String,Number 和 Boolean 使用 new 操作符
49+
"no-control-regex": true,
50+
"no-duplicate-variable": true,
51+
"no-duplicate-imports": true, // 禁止单个模块多次导入
52+
"no-duplicate-switch-case": true,
53+
"no-empty": false, // 禁止空块语句
54+
"no-extra-semi": true,
55+
"no-eval": true,
56+
"no-implicit-dependencies": [true, "dev"],
57+
"no-multi-spaces": [ true, { "exceptions": { "PropertyAssignment": true, "VariableDeclaration": false } } ] , // 禁止出现多个空格
58+
"no-object-literal-type-assertion": false,
59+
"no-parameter-reassignment": false, // 禁止参数变量重新赋值
60+
"no-return-await": true,
61+
"no-reference": true, // Disallows /// <reference path=> imports (use ES6-style imports instead).
62+
"no-shadowed-variable": [
63+
true,
64+
{
65+
"class": true,
66+
"enum": true,
67+
"function": true,
68+
"interface": true,
69+
"namespace": true,
70+
"typeAlias": true,
71+
"typeParameter": true
72+
}
73+
],
74+
"no-string-throw": true,
75+
"no-submodule-imports": [true,
76+
"rxjs",
77+
"@angular/platform-browser",
78+
"source-map-support/register"
79+
],
80+
"no-trailing-whitespace": [true, "ignore-comments"], // 禁用行尾空白
81+
"no-unnecessary-initializer": true, // 禁止变量赋值为 undefined
82+
"no-unused-expression": [true, "allow-fast-null-checks"],
83+
"no-unused-variable": {
84+
"severity": "warning",
85+
"options": [true, { "ignore-pattern": "^_" }]
86+
},
87+
"no-unnecessary-type-assertion": false,
88+
"no-use-before-declare": true, // 禁止在变量定义之前使用它们
89+
"no-var-keyword": true, // 要求使用 let 或 const 而不是 var
90+
"no-var-requires": true, // Disallows the use of require statements except in import statements.
91+
"object-curly-spacing": [true, "always"], // 强制在大括号中使用一致的空格
92+
"object-literal-shorthand": [true],
93+
"object-literal-sort-keys": false,
94+
"one-line": [true, "check-open-brace", "check-whitespace"],
95+
"ordered-imports": [
96+
true,
97+
{
98+
"grouped-imports": true,
99+
"import-sources-order": "lowercase-last",
100+
"named-imports-order": "lowercase-first"
101+
}
102+
],
103+
"prefer-for-of": true,
104+
"quotemark": [true, "single", "avoid-escape", "avoid-template"],
105+
"radix": true,
106+
"semicolon": [true, "never"],
107+
"space-before-function-paren": [
108+
true,
109+
{"anonymous": "always", "named": "never"}
110+
],
111+
"ter-arrow-spacing": [true, { "before": true, "after": true }], // 要求箭头函数的箭头之前或之后有空格
112+
"ter-func-call-spacing": [true, "never"], // disallow spacing between function identifiers and their invocations
113+
"ter-indent": [true, 2,
114+
{
115+
"SwitchCase": 1
116+
}
117+
],
118+
"ter-no-irregular-whitespace": true, // 禁止不规则的空白
119+
"trailing-comma": [true,
120+
{
121+
"multiline": {
122+
"arrays": "always",
123+
"exports": "always",
124+
"imports": "always",
125+
"functions": "never",
126+
"objects": "always",
127+
"typeLiterals": "always"
128+
},
129+
"singleline": "never",
130+
"esSpecCompliant": true
131+
}
132+
], // 当最后一个元素或属性与闭括号 ] 或 右大括号 在 不同的行时,要求使用拖尾逗号;当在 同一行时,禁止使用拖尾逗号
133+
"triple-equals": true,
134+
"use-isnan": true, // 要求使用 isNaN() 检查 NaN
135+
"valid-typeof": true, // 强制 typeof 表达式与有效的字符串进行比较
136+
"variable-name": [true, "ban-keywords", "allow-leading-underscore"],
137+
"whitespace": {
138+
"options": [true,
139+
"check-branch",
140+
"check-decl",
141+
"check-operator",
142+
"check-module",
143+
"check-separator",
144+
"check-rest-spread",
145+
"check-type",
146+
"check-typecast",
147+
"check-type-operator",
148+
"check-preblock"]
149+
}
150+
}
151+
}

framework-ts/yadan/.eslintignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# /node_modules/* and /bower_components/* ignored by default
2+
3+
**/*.min.js
4+
.git/*
5+
bin/*
6+
doc/*
7+
src/*

0 commit comments

Comments
 (0)