Typescript 开发环境的最佳实践

Typescript 开发环境的最佳实践

0️⃣ git init(略)

1️⃣️️ 初始化:$ yarn add -D ts-node typescript

2️⃣ 生成 tsconfig.json:$ yarn tsc -init

3️⃣ 配置 TSLint:$ yarn add tslint -D

4️⃣ 生成 tslint.json:$ yarn tslint --init

5️⃣ 创建 src/index.ts:$ mkdir src && echo "console.log('Hello Typescript')" > src/index.ts

6️⃣ 执行 .ts 文件:$ yarn ts-node src/index.ts

7️⃣ 安装 husky(没错,就是二哈?,哈士奇?): $ yarn add husky -D

8️⃣ 打开 package.json,添加 husky 的 hook: 每次提交代码前,都会执行一次TSLint的检查命令。

{
    "husky": {
        "pre-commit": "yarn tslint -c tslint.json './**/*.ts'"
    }
}

9️⃣ 安装命令行交互神器 commander.js: $ yarn add commander

const commander = require('commander');
const pkg = require('../package.json');

commander
  .version(pkg.version)
  .description(pkg.description)
  .usage('[options] <command> [...]')
  .option('-c, --city [name]', 'Add city name')
  .parse(process.argv);

if (process.argv.slice(2).length === 0) {
    commander.help();
    process.exit()
}

1️⃣0️⃣ 测试命令: $ yarn ts-node src/index.ts -h

$ yarn ts-node src/index.ts -h
yarn run v1.10.1
$ C:\Users\Lee\Desktop\ts-weather\node_modules\.bin\ts-node src/index.ts -h
Usage: index.ts [options] <command> [...]

Options:
  -V, --version  output the version number
  -c, --city     Add city name
  -h, --help     output usage information
Done in 1.91s.

1️⃣1️⃣ 获取命令行输入:$ yarn ts-node src/index.ts --city dongguan

console.log(commander.city) // => dongguan

1️⃣2️⃣ 添加命令行颜色$ yarn add colors

const colors = require('colors');
const commander = require('commander');
const pkg = require('../package.json');

commander
  .version(pkg.version)
  .description(pkg.description)
  .usage('[options] <command> [...]')
  .option('-c, --city [name]', 'Add city name')
  .parse(process.argv);

if (process.argv.slice(2).length === 0) {
    commander.outputHelp(colors.red);
    process.exit()
}

转载于:https://www.cnblogs.com/CyLee/p/10926581.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值