您的位置:首页 > Web前端 > Node.js

几种语言原生开发环境构建之--Typescript语言

2016-08-04 18:11 387 查看

安装nodejs和npm

安装nvm版本工具

export NVM_DIR="$HOME/.nvm" && (
git clone https://github.com/creationix/nvm.git "$NVM_DIR"
cd "$NVM_DIR"
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" origin`
) && . "$NVM_DIR/nvm.sh"

将以上代码加入~/.profile文件

安装node和npm

$ nvm install node   #node新版已经集成npm工具
$ node -v
$ npm -v      #常用install -g  ,install --save |--save-dev , uninstall ,link, test


自定义npm配置
vim ~/.npmrc

prefix=/home/someuser/node_global/  #自定义npm模块安装目录
registry=http://registry.npm.taobao.org/ #npm镜像源

安装typescript语言支持

安装typescript编译器

$ npm install typescript -g
$ tsc --help


安装repl命令行

$ npm install ts-node -g
$ ts-node


安装Typings:一个包管理器用来获取声明文件

$ npm install typings -g
$ typings -h

项目构建

初始化

$  mkdir helloworld && cd helloworld && mkdir src && mkdir src/test
$  npm init  #生成package.json配置文件
$  typings init  #生成typings.json配置文件


新建一个针对于typescript的tsconfig.json的配置文件内容:

{
"filesGlob" :[
"src/**/*.ts"
] ,
"files": [

"src/index.ts"
,"src/bin/tool.ts"
],

"compileOnSave": false,
"compilerOptions": {
"outDir": "dist/",
"moduleResolution": "node",
"noImplicitAny": true,
"target": "es5"
,"sourceMap": true
,"module": "commonjs"
,"newLine": "LF"
}
,"exclude": [
"node_modules"
]
}


安装typescript支持

$  npm link typescript


开始编译

$ tsc    #在dist目录产生js代码

集成测试

安装测试工具

$ npm install mocha -g
$ npm link mocha
$ npm install chai  --save
$ typings install dt~chai --save  #安装chai声明文件
$ typings install dt~mocha --save --global#安装mocha声明文件


配置文件package.json配置:

"scripts": {
"pretest":"tsc typings/index.d.ts src/test/*.ts --outDir dist"
, "test": " mocha dist/test"
}


添加vim src/test/codeTest.ts 测试代码

import chai = require('chai');

var expect = chai.expect;

describe('User Model Unit Tests:', () => {

describe('2 + 4', () => {
it('should be 6', (done) => {
expect(2+4).to.equals(6);
done();
});

it('should not be 7', (done) => {
expect(2+4).to.not.equals(7);
done();
});
});
});


测试

$ npm test

###结尾
项目代码

官方融合typescript2.x+node例子

(https://github.com/Microsoft/TypeScript-Node-Starter)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Node.js TypeScript npm