diff --git a/packages/uni-cli-shared/lib/uts/utils.js b/packages/uni-cli-shared/lib/uts/utils.js index 25e08eb9e717902f9e4c96a9b553c9967a40e8f9..86fd7baa6fffc7f19ebbb5288d6d6b14040dd651 100644 --- a/packages/uni-cli-shared/lib/uts/utils.js +++ b/packages/uni-cli-shared/lib/uts/utils.js @@ -1,6 +1,37 @@ +const fs = require('fs') +const path = require('path') const { normalizePath } = require('../util') + +function hasProjectYarn (cwd) { + return fs.existsSync(path.join(cwd, 'yarn.lock')) +} + +function hasProjectPnpm (cwd) { + return fs.existsSync(path.join(cwd, 'pnpm-lock.yaml')) +} + +function getInstallCommand (cwd) { + return hasProjectYarn(cwd) + ? 'yarn add' + : hasProjectPnpm(cwd) + ? 'pnpm i' + : 'npm i' +} + +function installDepTips ( + type, + module, + version +) { + const command = + `${getInstallCommand(process.cwd())} ${module + (version ? '@' + version : '')}${type === 'devDependencies' ? ' -D' : ''}` + return `Cannot find module: ${module} +Please run \`${command}\` and try again.` +} + module.exports = { - normalizePath + normalizePath, + installDepTips } diff --git a/packages/uni-cli-shared/lib/uts/uts.js b/packages/uni-cli-shared/lib/uts/uts.js index 334e7269d72c36d9e12b843b1fb062ef199ba579..3d366bfe1154ad2ff76ee5f936c75f160f81f6f3 100644 --- a/packages/uni-cli-shared/lib/uts/uts.js +++ b/packages/uni-cli-shared/lib/uts/uts.js @@ -75,22 +75,20 @@ function resolveUtsFile(dir, extensions = ['.uts', '.ts', '.js']) { } function resolveUTSCompiler() { let compilerPath = ''; - if ((0, hbx_1.runByHBuilderX)()) { + if ((0, hbx_1.isInHBuilderX)()) { try { compilerPath = require.resolve(path_1.default.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'uniapp-uts-v1')); } catch (e) { } } - if (!compilerPath) { - try { - compilerPath = require.resolve('@dcloudio/uni-uts-v1', { - paths: [process.env.UNI_CLI_CONTEXT], - }); - } - catch (e) { } + try { + compilerPath = require.resolve('@dcloudio/uni-uts-v1', { + paths: [process.env.UNI_CLI_CONTEXT], + }); } - if (!compilerPath) { - throw 'uts compiler is not found'; + catch (e) { + console.error((0, utils_1.installDepTips)('devDependencies', '@dcloudio/uni-uts-v1', require('@dcloudio/uni-cli-shared/package.json').version)); + process.exit(0); } return require(compilerPath); }