Skip to content

Commit

Permalink
feat: 添加 pure-thin-cli 版本更新提示功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten-K committed Dec 15, 2022
1 parent c41ae98 commit a3d26c4
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 15 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "bin/www.js",
"scripts": {
"dev": "pnpm build && pure init thin demo",
"devc": "pnpm build && pure init aa demo",
"devc": "pnpm build && pure create",
"build": "tsup",
"typecheck": "tsc --noEmit",
"lint:eslint": "eslint --cache --max-warnings 0 \"{src,mock}/**/*.{vue,ts}\" --fix",
Expand Down Expand Up @@ -39,13 +39,15 @@
},
"homepage": "https://github.com/Ten-K/pure-thin-cli#readme",
"dependencies": {
"axios": "^1.2.1",
"cac": "^6.7.14",
"figlet": "^1.5.2",
"fs-extra": "^10.1.0",
"inquirer": "^8.2.4",
"log-symbols": "^4.1.0",
"ora": "^5.4.1",
"picocolors": "^1.0.0",
"semver": "^7.3.8",
"simple-git": "^3.14.1"
},
"devDependencies": {
Expand Down
68 changes: 65 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export const REGISTER = {
npm: 'https://registry.npmjs.org/',
taobao: 'https://registry.npmmirror.com/'
}

export { name, version } from '../package.json'

export const templates = {
thin: {
downloadUrl: 'https://gitee.com/yiming_chang/pure-admin-thin.git', // 模板下载地址
Expand Down
9 changes: 3 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import { cac } from 'cac'
import pc from 'picocolors'
import figlet from 'figlet'
import { create } from './template'
import { templates } from './constants'
import { TTemplateName } from './types'
import { isExistsFile } from './create-dir'
import { hasTemplate, clg } from './utils'
import { isExistsFile } from './create-dir'
import { inputProjectName } from './prompt'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const version: string = require('../package.json').version
import { templates, version } from './constants'

const cli = cac('pure')
cli.version(version)
Expand All @@ -31,7 +28,7 @@ cli
if (!hasTemplate(templateName)) return
const isExists = await isExistsFile(projectName, cmd)
if (isExists) return
create(projectName, templateName)
await create(projectName, templateName)
})

cli.help(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/template/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { clone } from '../utils/index'
import { templates } from '../constants'
import { TTemplateName } from '../types'
import { chooseTemplate } from '../prompt'
import { clone, checkNpmVersion } from '../utils'
import { templates, version, name as npmName } from '../constants'

export const create = async (projectName: string, templateName?: TTemplateName) => {
const run = async (name: TTemplateName) => {
const { downloadUrl, branch } = templates[name]
checkNpmVersion(version, npmName)
await clone(downloadUrl, projectName, ['-b', `${branch}`], name)
}
if (templateName) {
Expand Down
4 changes: 3 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"strict": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "../dist",
"module": "commonjs",
"lib": ["esnext"]
"lib": ["esnext"],
"skipLibCheck": true
},
"include": ["."]
}
78 changes: 78 additions & 0 deletions src/utils/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pc from 'picocolors'
import semver from 'semver'
import { log } from '../utils'
import { REGISTER } from '../constants'
import axios, { AxiosResponse } from 'axios'

/**
* 获取npm或taobao镜像对应的api
* @param registerOrigin npm包信息来源
* @returns
*/
export const getDefaultRegister = (registerOrigin: 'npm' | 'taobao' = 'taobao') => {
return REGISTER[registerOrigin]
}

/**
* 获取npm包信息
* @param npmName 当前npm包名
* @param register npm提供的api地址
* @returns
*/
export const getNpmInfo = async (npmName: string, register = getDefaultRegister()) => {
const npmUrl = register + npmName
let res
try {
res = await axios.get(npmUrl)
} catch (err) {
log.err(err as string)
}
return res
}

/**
* 获取npm包所有版本号
* @param npmName 当前npm包名
* @param register npm提供的api地址
* @returns
*/
export const getNpmVersions = async (npmName: string, register = getDefaultRegister()) => {
const { data } = (await getNpmInfo(npmName, register)) as AxiosResponse
if (!data) return []
const versions = Object.keys(data.versions)
const a = versions.sort((a, b) => {
return semver.gt(a, b) ? -1 : 1
})
return a
}

/**
* 判断当前npm包版本是否需要更新
* @param currentVersion 当前版本号
* @param npmName 当前npm包名
*/
export const checkNpmVersion = async (currentVersion: string, npmName: string) => {
const versions = await getNpmVersions(npmName)
const latestVersion = versions[0]
if (!latestVersion || currentVersion === latestVersion) return
const dim = pc.dim
const magenta = pc.magenta
console.log(
`\n
😀 ${pc.yellow('哇~有更新!')} ${pc.red(currentVersion)}${pc.green(
latestVersion
)}.
💯 ${
magenta('更新日志: ') +
dim(`https://github.com/Ten-K/${npmName}/releases/tag/v${latestVersion}`)
}
👻 ${dim('运行') + magenta(` npm i -g ${npmName} `) + dim('可以更新哦.')}
💕 ${
dim('关注') +
magenta(' pure-thin-cli') +
dim(`了解最新动态: https://github.com/Ten-K/${npmName}`)
}
\n`
)
}
5 changes: 3 additions & 2 deletions src/utils/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export const clone = async (

spinner.succeed() // 下载成功提示
// 模板使用提示
clg(`\r\n 已成功创建项目 ${pc.cyan(projectName)}`)
clg(`\r\n cd ${pc.cyan(projectName)}`)
clg(`\r\n 🎉 已成功创建项目 ${pc.cyan(projectName)}`)
clg(` 👉 开始使用以下命令: \r\n`)
clg(` cd ${pc.cyan(projectName)}`)
clg(' pnpm install \r\n')
if (templateName === 'tauri') {
return clg(' pnpm tauri:dev \r\n')
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './log'
export * from './clone'
export * from './check'

0 comments on commit a3d26c4

Please sign in to comment.