tis-cli前端项目快速搭建命令行工具
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.

99 lines
2.6 KiB

  1. const fs = require('fs-extra')
  2. const path = require('path');
  3. const child_process = require("child_process");
  4. function getDirectories(path) {
  5. return fs.readdirSync(path).filter(function (file) {
  6. return fs.statSync(path + '/' + file).isDirectory();
  7. });
  8. }
  9. const ROOT_DIR = __dirname
  10. const SRC_DIR = path.join(ROOT_DIR, './src');
  11. const FROM_PORT = 7000
  12. // 获取所有src目录下的文件夹(文件夹名即appId)
  13. const APP_IDS = getDirectories(SRC_DIR)
  14. const LIST = APP_IDS.map((appId, i) => {
  15. const workspace = path.join(SRC_DIR, `./${appId}`);
  16. const devPort = FROM_PORT + i;
  17. const devCmd = `cd ${workspace} && npm run serve -- --port ${devPort}`;
  18. const buildCmd = `cd ${workspace} && npm run build`;
  19. const installCmd = `cd ${workspace} && npm install`;
  20. const manifest = require(path.join(workspace, './manifest.json'))
  21. const entry = `http://localhost:${devPort}`
  22. const icon = `http://localhost:${devPort}/icon.png`
  23. return {
  24. appId,
  25. manifest: {
  26. ...manifest,
  27. id: appId,
  28. name: manifest.name || '未命名',
  29. entry,
  30. icon,
  31. },
  32. workspace,
  33. devPort,
  34. devCmd,
  35. installCmd,
  36. buildCmd,
  37. }
  38. }).filter((item) => {
  39. return !item.manifest.disabled
  40. });
  41. // 安装依赖
  42. function install() {
  43. LIST.forEach((item) => {
  44. console.log(`[appId:${item.appId} name:${item.manifest.name}] 开始安装依赖:`)
  45. child_process.execSync(item.installCmd, { stdio: 'inherit' })
  46. });
  47. }
  48. // 打包构建
  49. function build() {
  50. LIST.forEach((item) => {
  51. console.log(`[appId:${item.appId} appName:${item.manifest.name}] 开始打包构建:`)
  52. child_process.execSync(item.buildCmd, { stdio: 'inherit' })
  53. fs.copySync(path.join(item.workspace, './dist'), path.join(ROOT_DIR, `./dist/${item.appId}`))
  54. });
  55. console.log('全部打包构建完成')
  56. }
  57. // 开发模式运行
  58. function dev() {
  59. const apps_widgets = {
  60. apps: [],
  61. widgets: []
  62. }
  63. LIST.forEach((item) => {
  64. if (item.manifest.type === 'widget') {
  65. apps_widgets.widgets.push(item.manifest)
  66. } else {
  67. apps_widgets.apps.push(item.manifest)
  68. }
  69. })
  70. fs.writeFileSync(path.join(ROOT_DIR, './lib/platform/dist/apps_widgets.json'), JSON.stringify(apps_widgets, null, 2))
  71. const cmd = LIST.reduce((acc, item) => {
  72. acc += ` "${item.devCmd}"`
  73. return acc;
  74. }, 'npx concurrently');
  75. child_process.execSync(cmd, { stdio: 'inherit' });
  76. }
  77. const arg = process.argv[2]
  78. switch (arg) {
  79. case 'install':
  80. install()
  81. break;
  82. case 'dev':
  83. dev();
  84. break;
  85. case 'build':
  86. build();
  87. break;
  88. default:
  89. console.error(`未知命令 ${arg}`)
  90. }