Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions packages/toolbars/generate-vue/src/generateCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const basePaths = {
blocks: 'src/components/',
utils: 'src/lowcode/utils.js',
dataSource: 'src/lowcode/dataSource.json',
router: 'src/lowcode/routes.js',
store: 'src/lowcode/stores.js'
router: 'src/router/index.js',
store: 'src/stores/'
}

const FILE_TYPES = {
Expand Down Expand Up @@ -82,14 +82,18 @@ function generateStores({ globalState }) {
}

const filePath = basePaths.store
let result = "import { defineStore } from 'pinia'\n\n"
const result = "import { defineStore } from 'pinia'\n\n"

const res = []
const storeIds = []

const getStoreFnStrs = (getters = {}) =>
Object.values(getters)
.map(({ value }) => value?.replace(/function /, ''))
.join(',\n')

globalState.forEach(({ id, state, getters, actions }) => {
storeIds.push(id)
const storeCode = `export const ${id} = defineStore({
id: '${id}',
state: () => (${JSON.stringify(state)}),
Expand All @@ -101,16 +105,20 @@ function generateStores({ globalState }) {
}
})\n`

result += storeCode
res.push({
filePath: `${filePath}${id}.js`,
fileType: FILE_TYPES.Store,
fileContent: formatScript(`${result}\n${storeCode}`)
})
})

return [
{
filePath,
fileType: FILE_TYPES.Store,
fileContent: formatScript(result)
}
]
res.push({
filePath: `${filePath}index.js`,
fileType: FILE_TYPES.Store,
fileContent: formatScript(storeIds.map((id) => `export { ${id} } from './${id}'`).join('\n'))
})

return res
}

function generatePageFiles(codeList, pagePath = '') {
Expand Down Expand Up @@ -335,9 +343,12 @@ export function generateRouter(pages) {
}

const routes = generateRoutes(pages)
const importRoutes = "import { createRouter, createWebHashHistory } from 'vue-router'\n"

const content = `
export const routes = [
${importRoutes}

const routes = [
${routes
.map(
({ fileName, path, redirect, filePath }) => `{
Expand All @@ -347,6 +358,11 @@ export function generateRouter(pages) {
)
.join(',')}
]

export default createRouter({
history: createWebHashHistory(),
routes
})
`
const codeStr = formatScript(content)

Expand Down