fix(cli): template path resolve error after built (#279)

fix #267
This commit is contained in:
zuixinwang 2023-06-12 10:59:33 +08:00 committed by GitHub
parent ede750475e
commit 608aa043c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 13 deletions

View File

@ -1,6 +1,11 @@
import path from 'node:path' import path from 'node:path'
import { removeSync } from 'fs-extra' import { removeSync } from 'fs-extra'
import { walkFileTree, pathJoin, logGreen, logRed } from '../shared/utils' import {
walkFileTree,
logGreen,
logRed,
pathJoinFromCLI
} from '../shared/utils'
/** /**
* packages distruntimenode_modules * packages distruntimenode_modules
@ -22,7 +27,7 @@ const deleteDistFile = () => {
return flag return flag
}, },
dirPath: pathJoin('..', 'packages'), dirPath: pathJoinFromCLI('../../packages'),
callback() { callback() {
// empty // empty
} }

View File

@ -10,11 +10,12 @@ import fs from 'fs-extra'
import semver from 'semver' import semver from 'semver'
import { import {
getInputCmd, getInputCmd,
pathJoin,
walkFileTree, walkFileTree,
capitalizeKebabCase, capitalizeKebabCase,
logGreen, logGreen,
logYellow logYellow,
templatePath,
pathJoinFromCLI
} from '../../shared/utils' } from '../../shared/utils'
import { createModuleMapping } from '../../shared/module-utils' import { createModuleMapping } from '../../shared/module-utils'
import handlebarsRender from '../build/handlebars.render' import handlebarsRender from '../build/handlebars.render'
@ -24,9 +25,9 @@ const args = getInputCmd()
if (args.length > 0) { if (args.length > 0) {
const commands: string[] = [] const commands: string[] = []
const components: string[] = [] const components: string[] = []
const templateDir = pathJoin('../../public/template/component') const templateDir = path.join(templatePath, './component')
const componetDir = pathJoin('../../../../packages/vue/src') const componetDir = pathJoinFromCLI('../../packages/vue/src')
const { version } = fs.readJSONSync(pathJoin('../../../../packages/vue/package.json')) const { version } = fs.readJSONSync(pathJoinFromCLI('../../packages/vue/package.json'))
args.forEach((item) => { args.forEach((item) => {
if (item.indexOf('-') === 0) { if (item.indexOf('-') === 0) {

View File

@ -13,16 +13,16 @@ import {
capitalizeKebabCase, capitalizeKebabCase,
prettierFormat, prettierFormat,
logGreen, logGreen,
logRed logRed,
templatePath
} from '../../shared/utils' } from '../../shared/utils'
const svgRE = /\.svg$/ const svgRE = /\.svg$/
const svgDir = pathJoin('..', 'node_modules', '@opentiny', 'theme', 'svgs') const svgDir = pathJoin('..', 'node_modules', '@opentiny', 'theme', 'svgs')
const iconDir = pathJoin('..', 'packages', 'icon') const iconDir = pathJoin('..', 'packages', 'icon')
const packageJson = 'package.json' const packageJson = 'package.json'
const templatePath = pathJoin('..', 'template')
// 检查是否按照依赖包 // 检查是否安装依赖包
if (!fs.existsSync(svgDir)) { if (!fs.existsSync(svgDir)) {
logYellow('The @opentiny/vue-theme is not exist , please npm install @opentiny/vue-theme.') logYellow('The @opentiny/vue-theme is not exist , please npm install @opentiny/vue-theme.')
} }

View File

@ -11,7 +11,7 @@ import {
capitalizeKebabCase, capitalizeKebabCase,
kebabCase, kebabCase,
prettierFormat, prettierFormat,
pathJoin pathJoinFromCLI
} from './utils' } from './utils'
const require = createRequire(import.meta.url) const require = createRequire(import.meta.url)
@ -434,7 +434,7 @@ const createModuleMapping = (componentName, isMobile = false) => {
const moduleJson = quickSort({ sortData: moduleMap, returnType: 'object' }) const moduleJson = quickSort({ sortData: moduleMap, returnType: 'object' })
fs.writeJsonSync( fs.writeJsonSync(
pathJoin('..', 'modules.json'), pathJoinFromCLI('../../packages/modules.json'),
prettierFormat({ prettierFormat({
str: JSON.stringify(moduleJson), str: JSON.stringify(moduleJson),
options: { options: {

View File

@ -482,6 +482,23 @@ const filesFragmentReplace = (folderPath, regExpStr: Array<string | RegExp> | 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 { export {
assetsPath, assetsPath,
capitalize, capitalize,
@ -517,4 +534,7 @@ export {
getBuildTag, getBuildTag,
getVueVersion, getVueVersion,
pathFromWorkspaceRoot, pathFromWorkspaceRoot,
pathJoinFromCLI,
pathJoinFromCLIEntry,
templatePath,
} }

View File

@ -9,7 +9,10 @@ const shared: Options = {
target: 'node14', target: 'node14',
platform: 'node', platform: 'node',
dts: false, dts: false,
publicDir: true publicDir: true,
env: {
TINY_TSUP_ENV: 'production'
}
} }
export default defineConfig([ export default defineConfig([