Skip to content

Commit 08870c9

Browse files
committed
chore: add update template script
1 parent 2d47b79 commit 08870c9

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"pub:prerelease": "pnpm run build:plugin && pnpm run build:alpha && pnpm lerna version prerelease --preid beta --no-push --yes && lerna publish from-package --pre-dist-tag beta --yes",
2020
"setup": "node ./scripts/setup.js",
2121
"splitMaterials": "node ./scripts/splitMaterials.mjs",
22-
"buildMaterials": "node ./scripts/buildMaterials.mjs"
22+
"buildMaterials": "node ./scripts/buildMaterials.mjs",
23+
"updateTemplate": "node ./scripts/updateTemplate.mjs"
2324
},
2425
"devDependencies": {
2526
"@babel/eslint-parser": "^7.21.3",

packages/engine-cli/template/designer/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "designer-demo-template",
2+
"name": "designer-demo",
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
77
"dev": "concurrently 'pnpm:serve:mock' 'pnpm:serve:frontend'",
8-
"serve:frontend": "cross-env VITE_THEME=light vite",
9-
"serve:mock": "node node_modules/@opentiny/tiny-engine-mock/dist/app.js",
108
"build:alpha": "cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode alpha",
11-
"build": "cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode prod"
9+
"build": "cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode prod",
10+
"serve:frontend": "cross-env VITE_THEME=light vite",
11+
"serve:mock": "node node_modules/@opentiny/tiny-engine-mock/dist/app.js"
1212
},
1313
"dependencies": {
1414
"@opentiny/tiny-engine": "^2.1.0",
@@ -29,7 +29,7 @@
2929
"@opentiny/tiny-engine-vite-config": "^2.1.0",
3030
"@vitejs/plugin-vue": "^5.1.2",
3131
"cross-env": "^7.0.3",
32-
"concurrently": "^8.2.0",
33-
"vite": "^5.4.2"
32+
"vite": "^5.4.2",
33+
"concurrently": "^8.2.0"
3434
}
3535
}

packages/engine-cli/template/designer/registry.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,20 @@ export default {
101101
Lang,
102102
ViewSetting
103103
],
104-
plugins: [Materials, Tree, Page, Block, Datasource, Bridge, I18n, Script, State, Schema, Help, Robot],
104+
plugins: [
105+
Materials,
106+
Tree,
107+
Page,
108+
[Block, { options: { ...Block.options, mergeCategoriesAndGroups: true } }],
109+
Datasource,
110+
Bridge,
111+
I18n,
112+
Script,
113+
State,
114+
Schema,
115+
Help,
116+
Robot
117+
],
105118
dsls: [{ id: 'engine.dsls.dslvue' }],
106119
settings: [Props, Styles, Events],
107120
canvas: Canvas

packages/engine-cli/template/designer/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig((configEnv) => {
77
viteConfigEnv: configEnv,
88
root: __dirname,
99
iconDirs: [path.resolve(__dirname, './node_modules/@opentiny/tiny-engine/assets/')],
10+
useSourceAlias: false,
1011
envDir: './env'
1112
})
1213

scripts/updateTemplate.mjs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import fs from 'fs-extra'
2+
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import Logger from './logger.mjs'
5+
import pkg from '../packages/design-core/package.json' assert { type: 'json' }
6+
7+
const logger = new Logger('updateTemplate')
8+
9+
const __filename = fileURLToPath(import.meta.url)
10+
const __dirname = path.dirname(__filename)
11+
12+
const templateSrcPath = path.resolve(__dirname, '../designer-demo')
13+
const templateDistPath = path.resolve(__dirname, '../packages/engine-cli/template/designer')
14+
15+
const ignoreFolder = ['node_modules', 'dist', 'temp', 'tmp']
16+
17+
const filter = (src, _dest) => {
18+
if (ignoreFolder.some((item) => src.includes(item))) {
19+
return false
20+
}
21+
return true
22+
}
23+
24+
async function copyTemplate() {
25+
const templateBackupPath = path.resolve(templateDistPath, '../designer_backup')
26+
if (await fs.pathExists(templateBackupPath)) {
27+
await fs.remove(templateBackupPath)
28+
}
29+
// 先备份原目录
30+
if (await fs.pathExists(templateDistPath)) {
31+
await fs.move(templateDistPath, templateBackupPath)
32+
}
33+
try {
34+
// 删除cli template内容
35+
if (await fs.pathExists(templateDistPath)) {
36+
await fs.remove(templateDistPath)
37+
}
38+
// 复制designer-demo
39+
await fs.copy(templateSrcPath, templateDistPath, { filter })
40+
await fs.remove(templateBackupPath)
41+
} catch (error) {
42+
logger.error(error)
43+
// 复制错误时恢复
44+
if (await fs.pathExists(templateBackupPath)) {
45+
await fs.move(templateBackupPath, templateDistPath)
46+
}
47+
} finally {
48+
if (await fs.pathExists(templateBackupPath)) {
49+
await fs.remove(templateBackupPath)
50+
}
51+
}
52+
}
53+
54+
async function updatePkgJson() {
55+
const { version } = pkg
56+
const pkgJsonPath = path.resolve(templateDistPath, 'package.json')
57+
if ((await fs.pathExists(pkgJsonPath)) === false) {
58+
return
59+
}
60+
const pkgData = await fs.readJSON(pkgJsonPath)
61+
pkgData.version = '0.0.0'
62+
63+
const defaultScripts = {
64+
dev: "concurrently 'pnpm:serve:mock' 'pnpm:serve:frontend'",
65+
'serve:frontend': 'cross-env VITE_THEME=light vite',
66+
'serve:mock': 'node node_modules/@opentiny/tiny-engine-mock/dist/app.js',
67+
'build:alpha': 'cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode alpha',
68+
build: 'cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode prod'
69+
}
70+
71+
Object.entries(defaultScripts).forEach(([name, value]) => {
72+
pkgData.scripts[name] = value
73+
})
74+
75+
pkgData.devDependencies['concurrently'] = '^8.2.0'
76+
77+
Object.keys(pkgData.dependencies)
78+
.filter((name) => name.includes('@opentiny/tiny-engine'))
79+
.forEach((name) => (pkgData.dependencies[name] = version))
80+
Object.keys(pkgData.devDependencies)
81+
.filter((name) => name.includes('@opentiny/tiny-engine'))
82+
.forEach((name) => (pkgData.devDependencies[name] = version))
83+
84+
await fs.writeJSON(pkgJsonPath, pkgData, { spaces: 2 })
85+
}
86+
87+
async function modifyViteConfig() {
88+
const viteConfigPath = path.resolve(templateDistPath, 'vite.config.js')
89+
if (await fs.exists(viteConfigPath)) {
90+
const fileContent = await fs.readFile(viteConfigPath, { encoding: 'utf-8' })
91+
const aliasRegexp = /useSourceAlias: *true/
92+
if (aliasRegexp.test(fileContent)) {
93+
const newFileContent = fileContent.replace(aliasRegexp, 'useSourceAlias: false')
94+
await fs.writeFile(viteConfigPath, newFileContent)
95+
}
96+
}
97+
}
98+
99+
async function main() {
100+
await copyTemplate()
101+
await updatePkgJson()
102+
await modifyViteConfig()
103+
}
104+
105+
main()

0 commit comments

Comments
 (0)