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

安装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)

转载于:https://my.oschina.net/jackywyz/blog/727431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值