Skip to content

Commit

Permalink
feat: add latest version tip
Browse files Browse the repository at this point in the history
  • Loading branch information
leedom92 committed Apr 20, 2024
1 parent adbd828 commit 4bfa441
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 10 deletions.
12 changes: 9 additions & 3 deletions bin/leedom-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs')
const glob = require('glob')
const { program } = require('commander')
const inquirer = require('inquirer')
const { success, error, info, banner, initGitRepo, downloadTemplate, fixDirectoryPath } = require('../lib/utils')
const { success, error, info, banner, initGitRepo, downloadTemplate, fixDirectoryPath, checkNewVersion } = require('../lib/utils')
const templates = require('../lib/templates')
const { name, version } = require('../package')

Expand All @@ -24,6 +24,12 @@ init(firstParam)
async function init(projectName) {
console.clear()
console.log(banner(`\n${name} v${version}\n`))

await checkNewVersion()
await confirmProjectName(projectName)
}

async function confirmProjectName(projectName) {
if (!projectName) {
const { myProject } = await inquirer.prompt([
{
Expand Down Expand Up @@ -52,9 +58,9 @@ function checkDuplicateDir(projectName) {

if (hasDuplicateNameDir.length) {
const existDirectoryName = path.resolve(process.cwd(), path.join('.', hasDuplicateNameDir[0]))
console.log(`当前目录 ${info(fixDirectoryPath(existDirectoryName))} 已经存在,请另起项目名!`)
console.log(`当前目录 ${info(fixDirectoryPath(existDirectoryName))} 已经存在,请另起项目名!\n`)
timeout = setTimeout(() => {
init()
confirmProjectName()
}, 2000)
} else {
selectStartWay(projectName)
Expand Down
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const degit = require('degit')
const ora = require('ora')
const gradient = require('gradient-string')
const inquirer = require('inquirer')
const isOnline = require('is-online')
const { lt } = require('semver')
const { name, version } = require('../package.json')

/**
* chalk自定义主题
Expand Down Expand Up @@ -64,9 +67,23 @@ const downloadTemplate = async function(target, url) {
})
}

// fix outputing wrong path in Windows
// 修复在Windows中输出的错误路径
const fixDirectoryPath = path => path.replaceAll('\\', '/')

// 检测新版本
const checkNewVersion = async () => {
const spinner = ora()
const online = await isOnline()
if(online) {
const { stdout : latestVersion} = await execa('npm',['view', name, 'version']);

if (lt(version, latestVersion)) {
spinner.warn(`最新版本为 ${warning(`v${latestVersion}`)} ,你可以执行 ${success.underline(`npm install -g ${name}`)} 进行升级。`)
console.log();
}
}
}

module.exports = {
success,
error,
Expand All @@ -76,4 +93,5 @@ module.exports = {
initGitRepo,
downloadTemplate,
fixDirectoryPath,
checkNewVersion
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"glob": "^10.3.10",
"gradient-string": "^2.0.2",
"inquirer": "8.0.0",
"ora": "5.4.1"
"is-online": "^8.5.1",
"ora": "5.4.1",
"semver": "^7.6.0"
},
"devDependencies": {
"@leedomjs/eslint-config-basic": "^0.8.3",
Expand Down
Loading

0 comments on commit 4bfa441

Please sign in to comment.