fangmd / blogsource

6 stars 0 forks source link

自定义 cli #62

Open fangmd opened 2 years ago

fangmd commented 2 years ago

创建过程

mkdir template-cli
npm init

npm i -D typescript @types/node

tsc --init
// tsconfig.json
{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "sourceMap": true,
    "outDir": "./lib",
  },
  "include": [".eslintrc.js", "src/**/*"],
  "exclude": ["node_modules", "lib/**/*"],
}
npm install -save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin @eslint/config
// .eslintrc.js
module.exports = {
  root: true,

  env: {
    node: true,
    es2021: true,
  },

  parser: '@typescript-eslint/parser',

  parserOptions: {
    ecmaVersion: 12,
    sourceType: 'module',
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
  },

  plugins: ['@typescript-eslint'],

  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
  ],
}
// .eslintignore
lib
// package.json
"scripts": {
    "lint": "eslint --ext .js .",
    "lint:fix": "eslint --fix --ext .js ."
},
npm link
// package.json
"scripts": {
    "dev": "tsc && node lib/index.js",
    "build": "tsc",
},
//src/index.ts
#!/usr/bin/env node

const msg = 'hello'

console.log(msg)
fangmd commented 2 years ago

常用工具