Skip to content

Commit

Permalink
feat: engine-cli support create platform with params (#626)
Browse files Browse the repository at this point in the history
* feat: engine-cli support create platform with params

* fix: add choices for theme

* fix: del unnecessary empty options
  • Loading branch information
chilingling authored Jul 9, 2024
1 parent 87169b4 commit c715a5a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 27 deletions.
7 changes: 7 additions & 0 deletions designer-demo/engine.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
}
9 changes: 2 additions & 7 deletions designer-demo/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
AppService,
GenerateCodeService
} from '@opentiny/tiny-engine'
import engineConfig from './engine.config'

export default {
root: {
Expand All @@ -54,13 +55,7 @@ export default {
GenerateCodeService
]
},
config: {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
},
config: engineConfig,
layout: Layout,
themes: [
{
Expand Down
3 changes: 2 additions & 1 deletion packages/common/js/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const open = (params = {}) => {
const paramsMap = new URLSearchParams(location.search)
params.app = paramsMap.get('id')
params.tenant = paramsMap.get('tenant')
const { scripts, styles } = useMaterial().resState.thirdPartyDeps

const { scripts, styles } = useMaterial().materialState.thirdPartyDeps
params.scripts = {}
scripts
.filter((item) => item.script)
Expand Down
42 changes: 35 additions & 7 deletions packages/engine-cli/src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,41 @@ import { cwd } from 'node:process'
import path from 'node:path'
import fs from 'fs-extra'
import chalk from 'chalk'
import { generateConfig, generatePackageJson } from './generateConfig'

export function createPlatform(name) {
const sourcePath = path.join(__dirname, '../template/designer/')
const logger = console

const defaultOptions = {
theme: 'light',
platformId: 918,
material: ['/mock/bundle.json'],
scripts: [],
styles: []
}

export function createPlatform(name, options = {}) {
if (fs.pathExistsSync(path.join(cwd(), name))) {
logger.log(chalk.red(`create failed, because the ${name} folder already exists. 创建失败,${name} 文件夹已存在。`))
return
}

const mergedOptions = {
...defaultOptions,
...options
}

const templatePath = path.join(__dirname, '../template/designer/')
const destPath = path.join(cwd(), name)
fs.copySync(sourcePath, destPath)
// eslint-disable-next-line no-console
console.log(

fs.copySync(templatePath, destPath)

const configContent = generateConfig(mergedOptions)
const pkgContent = generatePackageJson(name, mergedOptions, templatePath)

fs.outputFileSync(path.resolve(destPath, 'engine.config.js'), configContent)
fs.outputJSONSync(path.resolve(destPath, 'package.json'), pkgContent)

logger.log(
chalk.green(`create finish, run the follow command to start project: \ncd ${name} && npm install && npm run dev`)
)
}
Expand All @@ -29,8 +57,8 @@ export function createPlugin(name) {
const sourcePath = path.join(__dirname, '../template/plugin/')
const destPath = path.join(cwd(), name)
fs.copySync(sourcePath, destPath)
// eslint-disable-next-line no-console
console.log(

logger.log(
chalk.green(`create finish, run the follow command to start project: \ncd ${name} && npm install && npm run dev`)
)
}
37 changes: 37 additions & 0 deletions packages/engine-cli/src/commands/generateConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs-extra'
import path from 'path'

// 根据参数生成 config 文件内容
export const generateConfig = (options = {}) => {
const { theme, platformId, material, scripts = [], styles = [] } = options

const configContent = `
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || '${theme}',
material: ${JSON.stringify(material)},
scripts: ${JSON.stringify(scripts)},
styles: ${JSON.stringify(styles)},
platformId: ${platformId}
}
`

return configContent
}

// 根据参数修改 package.json
export const generatePackageJson = (name, options, templatePath) => {
const templatePackageJson = fs.readJSONSync(path.resolve(templatePath, 'package.json'))

templatePackageJson.name = name
templatePackageJson.scripts['serve:frontend'] = templatePackageJson.scripts['serve:frontend'].replace(
/VITE_THEME=[^\s]+/,
`VITE_THEME=${options.theme}`
)
templatePackageJson.scripts.build = templatePackageJson.scripts.build.replace(
/VITE_THEME=[^\s]+/,
`VITE_THEME=${options.theme}`
)

return templatePackageJson
}
11 changes: 8 additions & 3 deletions packages/engine-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { Command } from 'commander'
import { Command, Option } from 'commander'
import { input, select } from '@inquirer/prompts'
import { createPlatform, createPlugin } from './commands/create.js'

Expand All @@ -18,8 +18,13 @@ const program = new Command()
program
.command('create-platform <name>')
.description('create a new tiny-engine platform 创建一个新的tiny-engine低代码平台')
.action((name) => {
createPlatform(name)
.addOption(new Option('-t, --theme <theme>', 'platform theme 平台主题', 'light').choices(['light', 'dark']))
.option('-pid, --platformId <platformId>', 'platform id 平台主题', 918)
.option('-m, --material [material...]', 'material address 物料地址', ['/mock/bundle.json'])
.option('--scripts [script...]', '物料 script', [])
.option('--styles [styles...]', '物料 styles', [])
.action((name, options) => {
createPlatform(name, options)
})

program
Expand Down
7 changes: 7 additions & 0 deletions packages/engine-cli/template/designer/engine.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
}
13 changes: 4 additions & 9 deletions packages/engine-cli/template/designer/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,16 @@ import {
Canvas,
EditorInfoService,
AppService,
generateCodeService
GenerateCodeService
} from '@opentiny/tiny-engine'
import engineConfig from './engine.config'

export default {
root: {
id: 'engine.root',
metas: [EditorInfoService, AppService, generateCodeService]
},
config: {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
metas: [EditorInfoService, AppService, GenerateCodeService]
},
config: engineConfig,
layout: Layout,
themes: [
{
Expand Down

0 comments on commit c715a5a

Please sign in to comment.