zhangyuhan 3 жил өмнө
parent
commit
8d34ad060c

+ 4 - 1
.eslintrc

@@ -6,6 +6,9 @@
   "rules": {
     "@typescript-eslint/no-require-imports": "off",
     "@typescript-eslint/dot-notation": "off",
-    "object-curly-spacing": "off"
+    "object-curly-spacing": "off",
+    "unicorn/prefer-module": "off",
+    "comma-dangle": ["error", "never"],
+    "indent": ["error", 2, { "SwitchCase": 1 }]
   }
 }

+ 1 - 0
package.json

@@ -32,6 +32,7 @@
   "devDependencies": {
     "@oclif/test": "^2",
     "@types/chai": "^4",
+    "@types/ejs": "^3.1.1",
     "@types/inquirer": "^8.2.1",
     "@types/mocha": "^9.0.0",
     "@types/node": "^16.9.4",

+ 4 - 4
src/commands/create/index.ts

@@ -7,7 +7,7 @@ export default class Create extends Command {
   static description = '创建项目-命令行交互式'
 
   static examples = [
-    '$ jianyu create',
+    '$ jianyu create'
   ]
 
   async run(): Promise<void> {
@@ -18,7 +18,7 @@ export default class Create extends Command {
         type: 'input',
         name: 'name',
         default: 'new-project',
-        message: '输入项目名称(同时用于目录名)',
+        message: '输入项目名称(同时用于目录名)'
       },
       {
         type: 'list',
@@ -26,9 +26,9 @@ export default class Create extends Command {
         default: TemplateNames[0],
         choices: TemplateNames.map(v => ({
           name: `${v}  ${TEMPLATES[v].title}`,
-          value: v,
+          value: v
         })),
-        message: '选择项目模板',
+        message: '选择项目模板'
       }
     ])
 

+ 11 - 11
src/commands/init/index.ts

@@ -20,11 +20,11 @@ export default class Init extends Command {
   static examples = [
     `$ jianyu init new-dir --from component
 创建目录并拉取指定模版初始化项目
-`,
+`
   ]
 
   static flags = {
-    from: Flags.string({ char: 'f', description: '指定拉取模版的名称', required: true }),
+    from: Flags.string({ char: 'f', description: '指定拉取模版的名称', required: true })
   }
 
   static args = [{ name: 'dir', description: '目录名,同时用于项目名', required: true }]
@@ -52,7 +52,7 @@ export default class Init extends Command {
 
     const isEmpty = await isDirEmpty(destinationFolder)
     if (!isEmpty) {
-      const canCover = await inquirer.prompt(  {
+      const canCover = await inquirer.prompt({
         type: 'input',
         name: 'confirm',
         message: '目录不为空,是否覆盖 (no / yes)',
@@ -81,16 +81,16 @@ export default class Init extends Command {
         }
 
         return url
-      },
+      }
     })
 
     await dl.get({
       repository: repo,
       remoteFolder: assets,
-      destinationFolder,
+      destinationFolder
     }).then(() => {
-      this.log(`🪜 模板拉取成功`)
-      this.log(`🎨 准备中`)
+      this.log('🪜 模板拉取成功')
+      this.log('🎨 准备中')
     })
     await render(destinationFolder, {
       package: {
@@ -102,13 +102,13 @@ export default class Init extends Command {
     }).then(() => {
       this.log(`🎉 初始化 ${flags.from} ${title} 成功`)
       this.log('👉 使用下面这些命令开始项目:')
-      this.log(`  $ \x1B[36m  cd ${args.dir}  \x1B[0m`)
-      this.log('  $ \x1B[36m  yarn  \x1B[0m')
-    }).catch((err: Error) => {
+      this.log(`  $ \u001B[36m  cd ${args.dir}  \u001B[0m`)
+      this.log('  $ \u001B[36m  yarn  \u001B[0m')
+    }).catch((error: Error) => {
       this.warn(`🚨 [template error] 请提供错误截图及描述至 issues: ${PACKAGE.bugs} `)
       this.log('💀 撤销任务操作 清空目录')
       fs.rmSync(destinationFolder, { recursive: true, force: true })
-      this.error(err)
+      this.error(error)
     })
   }
 }

+ 2 - 3
src/commands/sync/index.ts

@@ -5,7 +5,7 @@ export default class Sync extends Command {
   static description = '同步到物料平台(内网注册表)'
 
   static examples = [
-    '$ jianyu sync',
+    '$ jianyu sync'
   ]
 
   async run(): Promise<void> {
@@ -15,8 +15,7 @@ export default class Sync extends Command {
     try {
       execSync('npm publish --registry http://192.168.3.207:4873/')
     } catch (error) {
-      // @ts-ignore
-      this.error(error)
+      this.error((error as Error) || '')
     }
   }
 }

+ 1 - 1
src/commands/update/index.ts

@@ -5,7 +5,7 @@ export default class Update extends Command {
   static description = '检测 CLI 版本更新'
 
   static examples = [
-    '$ jianyu update',
+    '$ jianyu update'
   ]
 
   async run(): Promise<void> {

+ 20 - 20
src/template.ts

@@ -20,14 +20,14 @@ const PackagePrompt = [
     type: 'input',
     name: 'packageName',
     message: '输入包名称',
-    default: function ({ name = ''} = {}) {
+    default: function ({ name = ''} = {}): string {
       return name
     },
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'package'
     },
-    validate: function (s = '') {
-      const result = /^[a-z|A-z|\-]+$/.test(s)
+    validate: function (s = ''): (string | boolean) {
+      const result = /^[A-z|-]+$/.test(s)
       return result ? result : '不支持@scope,示例:输入 new-package ==> @jianyu/new-package'
     }
   },
@@ -35,21 +35,21 @@ const PackagePrompt = [
     type: 'input',
     name: 'packageDescribe',
     message: '输入包描述',
-    default: function ({ packageName = ''} = {}) {
+    default: function ({ packageName = ''} = {}): string {
       return packageName
     },
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'package'
-    },
+    }
   },
   {
     type: 'input',
     name: 'packageAuthor',
     message: '输入包作者',
     default: 'jianyu-efe',
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'package'
-    },
+    }
   }
 ]
 
@@ -58,14 +58,14 @@ const ComponentPrompt = [
     type: 'input',
     name: 'packageName',
     message: '输入组件包名称',
-    default: function ({ name = ''} = {}) {
+    default: function ({ name = ''} = {}): string {
       return name
     },
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'component'
     },
-    validate: function (s = '') {
-      const result = /^[a-z|A-z|\-]+$/.test(s)
+    validate: function (s = ''): (string | boolean) {
+      const result = /^[A-z|-]+$/.test(s)
       return result ? result : '不支持@scope,示例:输入 new-package ==> @jianyu/new-component'
     }
   },
@@ -73,10 +73,10 @@ const ComponentPrompt = [
     type: 'input',
     name: 'packageLib',
     message: '输入编译后 UMD 名称 (对应编译后文件名称)',
-    default: function ({ packageName = ''} = {}) {
+    default: function ({ packageName = ''} = {}): string {
       return packageName
     },
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'component'
     }
   },
@@ -84,21 +84,21 @@ const ComponentPrompt = [
     type: 'input',
     name: 'packageDescribe',
     message: '输入描述',
-    default: function ({ packageName = ''} = {}) {
+    default: function ({ packageName = ''} = {}): string {
       return packageName
     },
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'component'
-    },
+    }
   },
   {
     type: 'input',
     name: 'packageAuthor',
     message: '输入包作者',
     default: 'jianyu-efe',
-    when: function ({ template = '' } = {}) {
+    when: function ({ template = '' } = {}): boolean {
       return template === 'component'
-    },
+    }
   }
 ]
 

+ 6 - 8
src/utils/render.ts

@@ -1,15 +1,12 @@
-import { PACKAGE } from '../template'
-
-const ejs = require('ejs')
+import ejs = require('ejs')
 const replace = require('replace-in-file')
 
-function filterFiles (dir: string) {
+function filterFiles(dir: string) {
   const support = ['js', 'ts', 'json', 'md']
   return support.map(s => `${dir}/**/*.${s}`)
-
 }
 
-function render (dir: string, args = {}) {
+function render(dir: string, args = {}): Promise<void> {
   let stop = false
   return new Promise((resolve, reject) => {
     replace({
@@ -20,10 +17,11 @@ function render (dir: string, args = {}) {
         let result = content
         try {
           result = ejs.render(content, args)
-        } catch (e) {
-          reject(e)
+        } catch (error) {
+          reject(error)
           stop = true
         }
+
         return result
       }
     }).then(resolve).catch(reject)

+ 8 - 8
src/utils/update.ts

@@ -1,24 +1,24 @@
 import { PACKAGE } from '../template'
 import { execSync } from 'node:child_process'
 
-function formatVersion (version: string) {
-  return +version.replace(/\./g, '000').replace(/^0+/, '')
+function formatVersion(version: string) {
+  return Number(version.replace(/\./g, '000').replace(/^0+/, ''))
 }
 
-function checkUpdate () {
+function checkUpdate(): void {
   console.log('⌛️ Detect the latest version')
   const now = formatVersion(PACKAGE.version)
-  const updateTip = `Update run shell:  \x1B[36m yarn global add ${PACKAGE.name} \x1B[0m`
+  const updateTip = `Update run shell:  \u001B[36m yarn global add ${PACKAGE.name} \u001B[0m`
   try {
     const latestQueryResult = execSync('npm view @jianyu/cli version')
     const latest = formatVersion(latestQueryResult.toString())
     console.log(`
-      \x1B[42m${PACKAGE.name}\x1B[0m version check
-       >>> now version: \x1B[35m${PACKAGE.version}\x1B[0m
-       >>> latest version: \x1B[32m${latestQueryResult.toString()}\x1B[0m
+      \u001B[42m${PACKAGE.name}\u001B[0m version check
+       >>> now version: \u001B[35m${PACKAGE.version}\u001B[0m
+       >>> latest version: \u001B[32m${latestQueryResult.toString()}\u001B[0m
       ${latest > now ? updateTip : ''}
     `)
-  } catch (e) {
+  } catch {
     console.log('Not find latest version of ' + PACKAGE.name)
   }
 }

+ 5 - 5
test/commands/hello/index.test.ts

@@ -2,9 +2,9 @@ import { expect, test } from '@oclif/test'
 
 describe('hello', () => {
   test
-  .stdout()
-  .command(['hello', 'friend', '--from=oclif'])
-  .it('runs hello cmd', ctx => {
-    expect(ctx.stdout).to.contain('hello friend from oclif!')
-  })
+    .stdout()
+    .command(['hello', 'friend', '--from=oclif'])
+    .it('runs hello cmd', ctx => {
+      expect(ctx.stdout).to.contain('hello friend from oclif!')
+    })
 })

+ 5 - 5
test/commands/hello/world.test.ts

@@ -2,9 +2,9 @@ import { expect, test } from '@oclif/test'
 
 describe('hello world', () => {
   test
-  .stdout()
-  .command(['hello:world'])
-  .it('runs hello world cmd', ctx => {
-    expect(ctx.stdout).to.contain('hello world!')
-  })
+    .stdout()
+    .command(['hello:world'])
+    .it('runs hello world cmd', ctx => {
+      expect(ctx.stdout).to.contain('hello world!')
+    })
 })

+ 10 - 10
test/commands/init.test.ts

@@ -2,16 +2,16 @@ import { expect, test } from '@oclif/test'
 
 describe('init', () => {
   test
-  .stdout()
-  .command(['init'])
-  .it('runs hello', ctx => {
-    expect(ctx.stdout).to.contain('hello world')
-  })
+    .stdout()
+    .command(['init'])
+    .it('runs hello', ctx => {
+      expect(ctx.stdout).to.contain('hello world')
+    })
 
   test
-  .stdout()
-  .command(['init', '--name', 'jeff'])
-  .it('runs hello --name jeff', ctx => {
-    expect(ctx.stdout).to.contain('hello jeff')
-  })
+    .stdout()
+    .command(['init', '--name', 'jeff'])
+    .it('runs hello --name jeff', ctx => {
+      expect(ctx.stdout).to.contain('hello jeff')
+    })
 })

+ 5 - 0
yarn.lock

@@ -758,6 +758,11 @@
   resolved "https://registry.npmjs.org/@types/chai/-/chai-4.3.0.tgz"
   integrity sha512-/ceqdqeRraGolFTcfoXNiqjyQhZzbINDngeoAq9GoHa8PPK1yNzTaxWjA6BFWp5Ua9JpXEMSS4s5i9tS0hOJtw==
 
+"@types/ejs@^3.1.1":
+  version "3.1.1"
+  resolved "http://192.168.3.42:4873/@types%2fejs/-/ejs-3.1.1.tgz#29c539826376a65e7f7d672d51301f37ed718f6d"
+  integrity sha512-RQul5wEfY7BjWm0sYY86cmUN/pcXWGyVxWX93DFFJvcrxax5zKlieLwA3T77xJGwNcZW0YW6CYG70p1m8xPFmA==
+
 "@types/expect@^1.20.4":
   version "1.20.4"
   resolved "https://registry.npmmirror.com/@types/expect/-/expect-1.20.4.tgz"