app模板、应用模板、组件模板、widget模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

4 years ago
  1. const fs = require('fs-extra')
  2. const path = require('path');
  3. const child_process = require("child_process");
  4. const express = require('express');
  5. const {createProxyMiddleware} = require('http-proxy-middleware');
  6. const proxy = require('./config');
  7. const {getDefaultSettings} = require('http2');
  8. function getDirectories(path) {
  9. return fs.readdirSync(path).filter(function (file) {
  10. return fs.statSync(path + '/' + file).isDirectory();
  11. });
  12. }
  13. const ROOT_DIR = __dirname
  14. const SRC_DIR = path.join(ROOT_DIR, './src');
  15. const FROM_PORT = 7000
  16. // 获取所有src目录下的文件夹(文件夹名即appIdesignd)
  17. const APP_IDS = getDirectories(SRC_DIR)
  18. const LIST = APP_IDS.map((appId, i) => {
  19. const workspace = path.join(SRC_DIR, `./${appId}`);
  20. const devPort = FROM_PORT + i;
  21. const devCmd = `cd ${workspace} && npm run serve -- --port ${devPort}`;
  22. const buildCmd = `cd ${workspace} && npm run build`;
  23. const installCmd = `cd ${workspace} && npm install`;
  24. const manifest = require(path.join(workspace, './public/esplug.json'))
  25. const entry = `http://localhost:${devPort}`
  26. const icon = `http://localhost:${devPort}/icon.png`
  27. return {
  28. appId,
  29. manifest: {
  30. ...manifest,
  31. id: appId,
  32. name: manifest.name || '未命名',
  33. entry,
  34. icon,
  35. },
  36. workspace,
  37. devPort,
  38. devCmd,
  39. installCmd,
  40. buildCmd,
  41. }
  42. }).filter((item) => {
  43. return !item.manifest.disabled
  44. });
  45. // 安装依赖
  46. function install() {
  47. LIST.forEach((item) => {
  48. console.log(`[appId:${item.appId} name:${item.manifest.name}] 开始安装依赖:`)
  49. child_process.execSync(item.installCmd, {stdio: 'inherit'})
  50. });
  51. }
  52. // 打包构建
  53. function build() {
  54. LIST.forEach((item) => {
  55. console.log(`[appId:${item.appId} appName:${item.manifest.name}] 开始打包构建:`)
  56. child_process.execSync(item.buildCmd, {stdio: 'inherit'})
  57. fs.copySync(path.join(item.workspace, './dist'), path.join(ROOT_DIR, `./dist`)) //${item.appId}
  58. });
  59. console.log('全部打包构建完成')
  60. }
  61. // 开发模式运行
  62. function dev() {
  63. const apps_widgets = {
  64. apps: [],
  65. widgets: [],
  66. components: [],
  67. }
  68. LIST.forEach((item) => {
  69. if (item.manifest.type === 'widget') {
  70. apps_widgets.widgets.push(item.manifest)
  71. } else {
  72. //过滤出system为true的组件
  73. apps_widgets.apps.push(item.manifest);
  74. const cps = item.manifest.components || [];
  75. apps_widgets.components.push(...cps.filter(c => c.system));
  76. }
  77. })
  78. fs.writeFileSync(path.join(ROOT_DIR, './lib/platform/dist/TIS_PLATFORM/manifest/apps_widgets.json'), JSON.stringify(apps_widgets, null, 2))
  79. const cmd = LIST.reduce((acc, item) => {
  80. acc += ` "${item.devCmd}"`
  81. return acc;
  82. }, 'concurrently');
  83. child_process.execSync(cmd, {stdio: 'inherit'});
  84. }
  85. //运行框架依赖
  86. function startPlatform() {
  87. const app = express();
  88. app.use(express.static('./lib/platform/dist'));
  89. const port = 8001;
  90. for (const key in proxy) {
  91. app.use(createProxyMiddleware(key, proxy[key]))
  92. }
  93. app.listen(port, function (err) {
  94. if (err) {
  95. console.log(err);
  96. return;
  97. }
  98. console.log(`listening at http://localhost:${port}/TIS_PLATFORM/"\n`);
  99. })
  100. }
  101. //启动一站式开发平台
  102. function design() {
  103. const app = express();
  104. app.use(express.static(path.join(__dirname, './lib/tis_design/examples/')))
  105. app.listen(9988);
  106. console.log(`listening at http://localhost:9988/"\n`);
  107. }
  108. const arg = process.argv[2]
  109. switch (arg) {
  110. case 'install':
  111. install()
  112. break;
  113. case 'dev':
  114. dev();
  115. break;
  116. case 'build':
  117. build();
  118. break;
  119. case 'platform':
  120. startPlatform();
  121. break;
  122. case 'design':
  123. design();
  124. break;
  125. default:
  126. console.error(`未知命令 ${arg}`)
  127. }