|
| 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 | + return true |
| 42 | + } catch (error) { |
| 43 | + logger.error(error) |
| 44 | + // 复制错误时恢复 |
| 45 | + if (await fs.pathExists(templateBackupPath)) { |
| 46 | + await fs.move(templateBackupPath, templateDistPath) |
| 47 | + } |
| 48 | + } finally { |
| 49 | + if (await fs.pathExists(templateBackupPath)) { |
| 50 | + await fs.remove(templateBackupPath) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function updatePkgJson() { |
| 56 | + const { version } = pkg |
| 57 | + const pkgJsonPath = path.resolve(templateDistPath, 'package.json') |
| 58 | + if ((await fs.pathExists(pkgJsonPath)) === false) { |
| 59 | + return |
| 60 | + } |
| 61 | + const pkgData = await fs.readJSON(pkgJsonPath) |
| 62 | + pkgData.version = '0.0.0' |
| 63 | + |
| 64 | + const defaultScripts = { |
| 65 | + dev: "concurrently 'pnpm:serve:mock' 'pnpm:serve:frontend'", |
| 66 | + 'serve:frontend': 'cross-env VITE_THEME=light vite', |
| 67 | + 'serve:mock': 'node node_modules/@opentiny/tiny-engine-mock/dist/app.js', |
| 68 | + 'build:alpha': 'cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode alpha', |
| 69 | + build: 'cross-env NODE_OPTIONS=--max-old-space-size=8192 VITE_THEME=light vite build --mode prod' |
| 70 | + } |
| 71 | + |
| 72 | + Object.entries(defaultScripts).forEach(([name, value]) => { |
| 73 | + pkgData.scripts[name] = value |
| 74 | + }) |
| 75 | + |
| 76 | + pkgData.devDependencies['concurrently'] = '^8.2.0' |
| 77 | + |
| 78 | + Object.keys(pkgData.dependencies) |
| 79 | + .filter((name) => name.includes('@opentiny/tiny-engine')) |
| 80 | + .forEach((name) => (pkgData.dependencies[name] = version)) |
| 81 | + Object.keys(pkgData.devDependencies) |
| 82 | + .filter((name) => name.includes('@opentiny/tiny-engine')) |
| 83 | + .forEach((name) => (pkgData.devDependencies[name] = version)) |
| 84 | + |
| 85 | + await fs.writeJSON(pkgJsonPath, pkgData, { spaces: 2 }) |
| 86 | +} |
| 87 | + |
| 88 | +async function modifyViteConfig() { |
| 89 | + const viteConfigPath = path.resolve(templateDistPath, 'vite.config.js') |
| 90 | + if (await fs.exists(viteConfigPath)) { |
| 91 | + const fileContent = await fs.readFile(viteConfigPath, { encoding: 'utf-8' }) |
| 92 | + const aliasRegexp = /useSourceAlias: *true/ |
| 93 | + if (aliasRegexp.test(fileContent)) { |
| 94 | + const newFileContent = fileContent.replace(aliasRegexp, 'useSourceAlias: false') |
| 95 | + await fs.writeFile(viteConfigPath, newFileContent) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function main() { |
| 101 | + await copyTemplate() |
| 102 | + await updatePkgJson() |
| 103 | + await modifyViteConfig() |
| 104 | +} |
| 105 | + |
| 106 | +main() |
0 commit comments