Skip to content

Commit

Permalink
fix(cli): 优化 npm 安装包版本的查找
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 26, 2018
1 parent 4745b44 commit 5395f43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
11 changes: 11 additions & 0 deletions packages/taro-cli/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,14 @@ exports.UPDATE_PACKAGE_LIST = [
]

exports.pascalCase = (str) => str.charAt(0).toUpperCase() + _.camelCase(str.substr(1))

exports.getInstalledNpmPkgVersion = function (pkgName, basedir) {
const resolvePath = require('resolve')
try {
const pkg = resolvePath.sync(`${pkgName}/package.json`, { basedir })
const pkgJson = fs.readJSONSync(pkg)
return pkgJson.version
} catch (err) {
return null
}
}
33 changes: 9 additions & 24 deletions packages/taro-cli/src/weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const template = require('babel-template')
const autoprefixer = require('autoprefixer')
const minimatch = require('minimatch')
const _ = require('lodash')
const envinfo = require('envinfo')

const postcss = require('postcss')
const pxtransform = require('postcss-pxtransform')
Expand Down Expand Up @@ -978,23 +977,16 @@ async function compileScriptFile (content, sourceFilePath, outputFilePath, adapt

async function checkCliAndFrameworkVersion () {
const frameworkName = `@tarojs/taro-${buildAdapter}`
let pkgInfo = await envinfo.run(
{
npmPackages: [frameworkName]
},
{ json: true, console: false, showNotFound: true }
)
pkgInfo = JSON.parse(pkgInfo)
const frameworkVersionInfo = pkgInfo.npmPackages[frameworkName]
if (typeof frameworkVersionInfo === 'object' && frameworkVersionInfo.installed) {
if (frameworkVersionInfo.installed !== Util.getPkgVersion()) {
const frameworkVersion = Util.getInstalledNpmPkgVersion(frameworkName, nodeModulesPath)
if (frameworkVersion) {
if (frameworkVersion !== Util.getPkgVersion()) {
Util.printLog(Util.pocessTypeEnum.ERROR, '版本问题', `Taro CLI 与本地安装的小程序框架 ${frameworkName} 版本不一致,请确保一致`)
console.log(`Taro CLI: ${Util.getPkgVersion()}`)
console.log(`${frameworkName}: ${frameworkVersionInfo.installed}`)
console.log(`${frameworkName}: ${frameworkVersion}`)
process.exit(1)
}
} else {
Util.printLog(Util.pocessTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装!`))
Util.printLog(Util.pocessTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装,或安装有误!`))
}
}

Expand Down Expand Up @@ -1024,19 +1016,12 @@ async function buildFrameworkInfo () {
if (buildAdapter === Util.BUILD_TYPES.SWAN) {
const frameworkInfoFileName = '.frameworkinfo'
const frameworkName = `@tarojs/taro-${buildAdapter}`
let pkgInfo = await envinfo.run(
{
npmPackages: [frameworkName]
},
{ json: true, console: false, showNotFound: true }
)
pkgInfo = JSON.parse(pkgInfo)
const frameworkVersionInfo = pkgInfo.npmPackages[frameworkName]
if (typeof frameworkVersionInfo === 'object' && frameworkVersionInfo.installed) {
const frameworkVersion = Util.getInstalledNpmPkgVersion(frameworkName, nodeModulesPath)
if (frameworkVersion) {
const frameworkinfo = {
toolName: 'Taro',
toolCliVersion: Util.getPkgVersion(),
toolFrameworkVersion: frameworkVersionInfo.installed,
toolFrameworkVersion: frameworkVersion,
createTime: new Date(projectConfig.date).getTime()
}
fs.writeFileSync(
Expand All @@ -1045,7 +1030,7 @@ async function buildFrameworkInfo () {
)
Util.printLog(Util.pocessTypeEnum.GENERATE, '框架信息', `${outputDirName}/${frameworkInfoFileName}`)
} else {
Util.printLog(Util.pocessTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装!`))
Util.printLog(Util.pocessTypeEnum.WARNING, '依赖安装', chalk.red(`项目依赖 ${frameworkName} 未安装,或安装有误!`))
}
}
}
Expand Down

0 comments on commit 5395f43

Please sign in to comment.