幾種語言原生開發環境構建之--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

  • 安裝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

###結尾 項目代碼git

官方融合typescript2.x+node例子
(https://github.com/Microsoft/TypeScript-Node-Starter)github

相關文章
相關標籤/搜索