快速搭建一个TypeScript项目

在团队开发中为了更好的协作,我们应该在项目中制定各种规则,比如:代码风格、变量命名、文件命名、git commit 提交格式等等。 这篇文章就介绍如何搭建快速搭建一个具有自定义规则的 TypeScript 项目。

首先在 GitHub 上创建一个仓库,然后 clone 到本地。

$ git clone git@github.com:ZhiXiao-Lin/create-ts-project.git

进入项目目录,执行 NPM 初始化

$ yarn init

安装 typescript 作为开发阶段的依赖项

$ yarn add typescript -D

在根目录新建 tsconfig.json,配置项具体的意义可以参考 ts 官方文档

{
    "version": "1.8.0",
    "compilerOptions": {
        "outDir": "build/compiled",
        "lib": [ "es6" ],
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "noImplicitAny": true,
        "declaration": true
    },
    "exclude": [ "build", "node_modules" ]
}

安装 @types/node 让 node 的核心包具备类型提示

$ yarn add @types/node -D

在根目录新建 src 目录,用于存放所有的 TypeScript 源文件,然后在 src 下新建 index.ts 作为入口文件

src/index.ts

console.log('Hello TypeScript!');

image.png

在开发阶段为了能直接执行并且监听 ts 文件的变化,安装 ts-node-dev

$ yarn add ts-node-dev -D

在 package.json 中定义一个启动脚本

"scripts": {
    "start": "ts-node-dev --respawn --transpileOnly src/index.ts"
}

试着运行

$ yarn start
yarn run v1.12.3
$ ts-node-dev --respawn --transpileOnly src/index.ts
Using ts-node version 8.0.1, typescript version 3.2.4
Hello TypeScript!

不管团队中的成员用的是什么编辑器,我们都应该保持一致的代码风格,这就需要使用 prettier

$ yarn add prettier -D

然后在根目录下新增一个 .prettierrc.js,用于定义代码格式化的规则

module.exports = {
    // 一行最多 140 字符
    printWidth: 140,
    // 使用 4 个空格缩进
    tabWidth: 4,
    // 不使用缩进符,而使用空格
    useTabs: false,
    // 行尾需要有分号
    semi: true,
    // 使用单引号
    singleQuote: true,
    // jsx 不使用单引号,而使用双引号
    jsxSingleQuote: false,
    // 末尾不需要逗号
    trailingComma: 'none',
    // 大括号内的首尾需要空格
    bracketSpacing: true,
    // jsx 标签的反尖括号需要换行
    jsxBracketSameLine: false,
    // 箭头函数,只有一个参数的时候,也需要括号
    arrowParens: 'always',
    // 每个文件格式化的范围是文件的全部内容
    rangeStart: 0,
    rangeEnd: Infinity,
    // 不需要写文件开头的 @prettier
    requirePragma: false,
    // 不需要自动在文件开头插入 @prettier
    insertPragma: false,
    // 使用默认的折行标准
    proseWrap: 'preserve',
    // 根据显示样式决定 html 要不要折行
    htmlWhitespaceSensitivity: 'css',
    // 换行符使用 lf
    endOfLine: 'lf'
};

在 package.json 中新增一条 script

"scripts": {
    "start": "ts-node-dev --respawn --transpileOnly src/index.ts",
    "style:prettier": "prettier --write \"src/**/*.ts\""
},

运行 yarn style:prettier 将会自动格式化 src 目录下的所有 .ts 文件

$ yarn style:prettier
yarn run v1.12.3
$ prettier --write "src/**/*.ts"
src\index.ts 225ms
Done in 0.66s.

安装 tslint 用于检查项目文件的命名、变量的命名等等

$ yarn add tslint -D

安装 tslint-config-prettier ,这是根据 prettier 定义的 tslint 规则

$ yarn add tslint-config-prettier -D

在根目录下新建 tslint.json,根据自己的需要定义 rules

{
    "defaultSeverity": "error",
    "extend": [ "tslint-config-prettier" ], // 继承 tslint-config-prettier
    "linterOptions": {
        "exclude": [ "**/node_modules/**" ]  // 排除 node_modules
    },
    "jsRules": {
        "no-unused-expression": true
    },
    "rules": {
        "typedef": {
            "options": [ "property-declaration" ]
        },
        "class-name": true,
        "interface-name": true,
        "variable-name": {
            "options": [ "ban-keywords", "check-format", "allow-leading-underscore", "allow-pascal-case" ]
        },
        "no-var-keyword": true,
        "no-var-requires": true,
        "no-any": false,
        "no-string-literal": true,
        "await-promise": true,
        "promise-function-async": true
    },
    "rulesDirectory": []
}

在 package.json 中新增一条 script

"scripts": {
    "start": "ts-node-dev --respawn --transpileOnly src/index.ts",
    "style:prettier": "prettier --write \"src/**/*.ts\"",
    "style:lint": "tslint -p tsconfig.json -c tslint.json"
},

运行 yarn style:lint,会执行 lint 检查

$ yarn style:lint
yarn run v1.12.3
$ tslint -p tsconfig.json -c tslint.json
Done in 1.45s.

安装 commitizen,让我们的项目具有统一的 git commit 规范

$ npm install -g commitizen
$ commitizen init cz-conventional-changelog --save --save-exact
Attempting to initialize using the npm package cz-conventional-changelog
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN create-ts-project@1.0.0 No description

+ cz-conventional-changelog@2.1.0
added 7 packages from 16 contributors, removed 1 package, updated 131 packages and audited 219 packages in 10.509s
found 0 vulnerabilities

执行后 package.json 中会多出 commitizen 的配置项

"config": {
    "commitizen": {
        "path": "./node_modules/cz-conventional-changelog"
    }
}

将所有的 git commit 操作替换成 git cz

 $ git cz
cz-cli@3.0.5, cz-conventional-changelog@2.1.0


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? Select the type of change that you're committing: (Use arrow keys)
> feat:     A new feature
  fix:      A bug fix
  docs:     Documentation only changes
  style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  refactor: A code change that neither fixes a bug nor adds a feature
  perf:     A code change that improves performance
  test:     Adding missing tests or correcting existing tests
(Move up and down to reveal more choices)

我们还需要在 git 提交的时候进行 commit 规范的检查,首先安装 validate-commit-msg 并在根目录中新建一个 .vcmrc 文件用于定义 commit 的验证规则

$ yarn add validate-commit-msg -D
{
    "types": [ "feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert" ],
    "scope": {
        "required": false,
        "allowed": [ "*" ],
        "validate": false,
        "multiple": false
    },
    "warnOnFail": false,
    "maxSubjectLength": 100,
    "subjectPattern": ".+",
    "subjectPatternErrorMsg": "subject does not match subject pattern!",
    "helpMessage": "",
    "autoFix": true
}

然后安装 husky 让我们可以很方便的定义 git hooks

$  yarn add husky -D

在 package.json 中定义 git hooks,使用 validate-commit-msg 来检查 commit 格式

"husky": {
        "hooks": {
            "commit-msg": "validate-commit-msg"
        }
}

现在如果提交的 commit 不符合 .vcmrc 定义的格式就会提交失败

$ git commit -m "2223"
husky > commit-msg (node v10.14.0)
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
2223
husky > commit-msg hook failed (add --no-verify to bypass)

现在我们还希望能在 commit 时自动执行代码格式化和语法检查,要使用一条命令执行多项操作可以安装 npm-run-all

$  yarn add npm-run-all -D

npm-run-all 提供两个命令 run-s 串行执行多条命令 run-p 并行执行多条命令,在 package.json 中定义一个 script,用于串行执行所有以 style: 开头的 script

"scripts": {
    "start": "ts-node-dev --respawn --transpileOnly src/index.ts",
    "style": "run-s style:**",
    "style:prettier": "prettier --write \"src/**/*.ts\"",
    "style:lint": "tslint -p tsconfig.json -c tslint.json"
},

运行一下,看到确实执行了两条命令

$ yarn style
yarn run v1.12.3
$ run-s style:**
$ prettier --write "src/**/*.ts"
src\index.ts 229ms
$ tslint -p tsconfig.json -c tslint.json
Done in 3.02s.

修改 package.json 定义的 git hooks

"husky": {
    "hooks": {
        "pre-commit": "yarn style",
        "commit-msg": "validate-commit-msg"
    }
},

试着进行一次提交

$ git add .
$ git cz
cz-cli@3.0.5, cz-conventional-changelog@2.1.0


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? Select the type of change that you're committing: feat:     A new feature
? What is the scope of this change (e.g. component or file name)? (press enter to skip)

? Write a short, imperative tense description of the change:
 infrastructure
? Provide a longer description of the change: (press enter to skip)

? Are there any breaking changes? No
? Does this change affect any open issues? No
husky > pre-commit (node v10.14.0)
yarn run v1.12.3
$ run-s style:**
$ prettier --write "src/**/*.ts"
src\index.ts 228ms
$ tslint -p tsconfig.json -c tslint.json
Done in 3.01s.
husky > commit-msg (node v10.14.0)
[master 5c75d75] feat: infrastructure
 6 files changed, 2666 insertions(+)
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 src/index.ts
 create mode 100644 tsconfig.json
 create mode 100644 tslint.json
 create mode 100644 yarn.lock

确实在提交前格式化了所有代码文件,并且进行了 commit 检查,至此我们的团队协作项目就搭建好了,再也不用担心五花八门的代码格式和 commit 提交说明了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容