diff --git a/app/service/app-center/generate.ts b/app/service/app-center/generate.ts index 8262e7de3a01c78770e80723be55da341efb09fd..43a9bd723d8adf68b9d7a2675d6558f71630e843 100644 --- a/app/service/app-center/generate.ts +++ b/app/service/app-center/generate.ts @@ -24,7 +24,8 @@ const WORDS = { saveCode: 'save the code locally', generateCode: 'Generate app preview code', dslParse: 'dsl parsing', - generateDependencies: 'generate dependencies' + generateDependencies: 'generate dependencies', + generateGlobalState: 'generate global state' }; const COMPONENT_NAME = { @@ -77,7 +78,8 @@ class Generate extends DataServcice { this.generateBlock(), this.generateUtils(), this.generateRouter(), - this.generateDependencies() + this.generateDependencies(), + this.generateGlobalState() ]; // 执行生成代码任务,任何一个 reject 立即结束,并抛出错误,由调用方 catch @@ -116,7 +118,7 @@ class Generate extends DataServcice { dataSource: { dataHandler, list: originList } } = this.schema; const fileName = 'dataSource.json'; - const filepath = path.resolve(this.generatePath, 'src', fileName); + const filepath = path.resolve(this.generatePath, 'src/lowcode', fileName); const list = originList.map(({ id, name, data }) => ({ id, name, ...data })); const code = { dataHandler, list }; @@ -131,7 +133,7 @@ class Generate extends DataServcice { if (utils?.length) { const utilStr = this.generateExport(utils); const fileName = 'utils.js'; - const filepath = path.resolve(this.generatePath, 'src', fileName); + const filepath = path.resolve(this.generatePath, 'src/lowcode', fileName); const content = this.formatCode(utilStr, { ...prettierCommon, parser: 'typescript' }, fileName); return this.saveCode(content, filepath, fileName); @@ -603,6 +605,85 @@ export default createRouter({ throw new Error(message); } } + + private async generateGlobalState () { + const { global_state } = this.schema.meta; + let globalState: { + actions?: { + [key:string]: { + type: string; + value: string + } + }; + getters?: { + [key:string]: { + type: string; + value: string; + } + }; + id: string; + state: { [key:string]: any } + }[] = [] + + if (Array.isArray(global_state)) { + globalState = [...global_state] + } + + const baseDir = `${this.generatePath}/src/stores` + // write global state + const res:any = [] + const ids:any[] = [] + + const getStoreFnStrs = (getters: Record = {}) => + Object.values(getters) + .map(({ value }) => value?.replace(/function /, '')) + .join(',\n'); + + for (const stateItem of globalState) { + let importStatement = "import { defineStore } from 'pinia'" + const { id, state, getters, actions } = stateItem + + ids.push(id) + + const storeFiles = ` + ${importStatement} + export const ${id} = defineStore({ + id: '${id}', + state: () => (${JSON.stringify(state)}), + getters: { + ${getStoreFnStrs(getters)} + }, + actions: { + ${getStoreFnStrs(actions)} + } + }) + ` + const fileName = `${id}.js` + + res.push({ + fileName, + fileContent: this.formatCode(storeFiles, { ...prettierCommon, parser: 'typescript' }, fileName) + }) + } + + res.push({ + fileName: 'index.js', + fileContent: ids.map((item) => `export { ${item} } from './${item}'`).join('\n') + }) + + try { + for (const { fileName, fileContent } of res) { + await this.saveCode(fileContent, path.resolve(baseDir, fileName)) + } + + } catch (error) { + const message = `failed to ${WORDS.generateGlobalState}: ${this.getMessage(error)}`; + this.logger.error(message); + + throw new Error(message); + } + + } } export default Generate; diff --git a/app/validate/app-center/app.ts b/app/validate/app-center/app.ts index 92dac61c07bbff7819d6e9ea55261284883c856c..5828ee3f01b722243463d49a9e4f48caab70ecb4 100644 --- a/app/validate/app-center/app.ts +++ b/app/validate/app-center/app.ts @@ -55,7 +55,7 @@ export const updateAppRule = { export const publishAppRule = { id: 'id', - commitMsg: 'commit', + commitMsg: 'string', branch: 'string', canCreateNewBranch: 'boolean', allGenerate: 'boolean' diff --git a/config/config.default.ts b/config/config.default.ts index b18bb29632fcc0a36f59be8f030a9cac5c601f09..c115bc008c8eaec66a4c2d1e2c6493312c78e1ab 100644 --- a/config/config.default.ts +++ b/config/config.default.ts @@ -85,22 +85,22 @@ export default (appInfo) => { config.previewTemplate = { default: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' }, common: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' }, bigScreen: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' }, priceCalculator: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' }, mobile: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' }, taihu: { - vue: '@opentiny/tiny-engine-generate-preview' + vue: '@opentiny/tiny-engine-preview-vue' } };