From 608aa043c5b55df775ee82ffb82c70407c635617 Mon Sep 17 00:00:00 2001 From: zuixinwang <59717852+zuixinwang@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:59:33 +0800 Subject: [PATCH] fix(cli): template path resolve error after built (#279) fix #267 --- internals/cli/src/commands/clean.ts | 9 +++++++-- .../cli/src/commands/create/create-ui.ts | 11 +++++----- internals/cli/src/commands/sync/sync-icons.ts | 6 +++--- internals/cli/src/shared/module-utils.ts | 4 ++-- internals/cli/src/shared/utils.ts | 20 +++++++++++++++++++ internals/cli/tsup.config.ts | 5 ++++- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/internals/cli/src/commands/clean.ts b/internals/cli/src/commands/clean.ts index 79c374c81..c9ec86acb 100644 --- a/internals/cli/src/commands/clean.ts +++ b/internals/cli/src/commands/clean.ts @@ -1,6 +1,11 @@ import path from 'node:path' import { removeSync } from 'fs-extra' -import { walkFileTree, pathJoin, logGreen, logRed } from '../shared/utils' +import { + walkFileTree, + logGreen, + logRed, + pathJoinFromCLI +} from '../shared/utils' /** * 删除 packages 项目包下插件内部的 dist、runtime、node_modules 文件 @@ -22,7 +27,7 @@ const deleteDistFile = () => { return flag }, - dirPath: pathJoin('..', 'packages'), + dirPath: pathJoinFromCLI('../../packages'), callback() { // empty } diff --git a/internals/cli/src/commands/create/create-ui.ts b/internals/cli/src/commands/create/create-ui.ts index c4517038d..6a0214de9 100644 --- a/internals/cli/src/commands/create/create-ui.ts +++ b/internals/cli/src/commands/create/create-ui.ts @@ -10,11 +10,12 @@ import fs from 'fs-extra' import semver from 'semver' import { getInputCmd, - pathJoin, walkFileTree, capitalizeKebabCase, logGreen, - logYellow + logYellow, + templatePath, + pathJoinFromCLI } from '../../shared/utils' import { createModuleMapping } from '../../shared/module-utils' import handlebarsRender from '../build/handlebars.render' @@ -24,9 +25,9 @@ const args = getInputCmd() if (args.length > 0) { const commands: string[] = [] const components: string[] = [] - const templateDir = pathJoin('../../public/template/component') - const componetDir = pathJoin('../../../../packages/vue/src') - const { version } = fs.readJSONSync(pathJoin('../../../../packages/vue/package.json')) + const templateDir = path.join(templatePath, './component') + const componetDir = pathJoinFromCLI('../../packages/vue/src') + const { version } = fs.readJSONSync(pathJoinFromCLI('../../packages/vue/package.json')) args.forEach((item) => { if (item.indexOf('-') === 0) { diff --git a/internals/cli/src/commands/sync/sync-icons.ts b/internals/cli/src/commands/sync/sync-icons.ts index f3954d18b..722d964a9 100644 --- a/internals/cli/src/commands/sync/sync-icons.ts +++ b/internals/cli/src/commands/sync/sync-icons.ts @@ -13,16 +13,16 @@ import { capitalizeKebabCase, prettierFormat, logGreen, - logRed + logRed, + templatePath } from '../../shared/utils' const svgRE = /\.svg$/ const svgDir = pathJoin('..', 'node_modules', '@opentiny', 'theme', 'svgs') const iconDir = pathJoin('..', 'packages', 'icon') const packageJson = 'package.json' -const templatePath = pathJoin('..', 'template') -// 检查是否按照依赖包 +// 检查是否安装依赖包 if (!fs.existsSync(svgDir)) { logYellow('The @opentiny/vue-theme is not exist , please npm install @opentiny/vue-theme.') } diff --git a/internals/cli/src/shared/module-utils.ts b/internals/cli/src/shared/module-utils.ts index a97c4cfc1..e1e1786ca 100644 --- a/internals/cli/src/shared/module-utils.ts +++ b/internals/cli/src/shared/module-utils.ts @@ -11,7 +11,7 @@ import { capitalizeKebabCase, kebabCase, prettierFormat, - pathJoin + pathJoinFromCLI } from './utils' const require = createRequire(import.meta.url) @@ -434,7 +434,7 @@ const createModuleMapping = (componentName, isMobile = false) => { const moduleJson = quickSort({ sortData: moduleMap, returnType: 'object' }) fs.writeJsonSync( - pathJoin('..', 'modules.json'), + pathJoinFromCLI('../../packages/modules.json'), prettierFormat({ str: JSON.stringify(moduleJson), options: { diff --git a/internals/cli/src/shared/utils.ts b/internals/cli/src/shared/utils.ts index 6805fa770..ab813f238 100644 --- a/internals/cli/src/shared/utils.ts +++ b/internals/cli/src/shared/utils.ts @@ -482,6 +482,23 @@ const filesFragmentReplace = (folderPath, regExpStr: Array | st } } +/** 是否已打包 */ +const isCLIProduction = process.env.TINY_TSUP_ENV === 'production' + +/** 相对 `CLI` 工程目录解析路径 */ +const pathJoinFromCLI = (...args: string[]) => { + const cliDir = path.resolve(dirname, '../../', isCLIProduction ? '../' : '') + return path.join(cliDir, ...args) +} + +/** 相对 `CLI` 入口文件解析目录 */ +const pathJoinFromCLIEntry = (...args: string[]) => { + return path.join(dirname, '../', ...args) +} + +/** 模板所在路径 */ +const templatePath = isCLIProduction ? pathJoinFromCLIEntry('./template') : pathJoinFromCLI('./public/template') + export { assetsPath, capitalize, @@ -517,4 +534,7 @@ export { getBuildTag, getVueVersion, pathFromWorkspaceRoot, + pathJoinFromCLI, + pathJoinFromCLIEntry, + templatePath, } diff --git a/internals/cli/tsup.config.ts b/internals/cli/tsup.config.ts index ab36eb80e..158072114 100644 --- a/internals/cli/tsup.config.ts +++ b/internals/cli/tsup.config.ts @@ -9,7 +9,10 @@ const shared: Options = { target: 'node14', platform: 'node', dts: false, - publicDir: true + publicDir: true, + env: { + TINY_TSUP_ENV: 'production' + } } export default defineConfig([