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
99 lines
2.6 KiB
const fs = require('fs-extra')
|
|
const path = require('path');
|
|
const child_process = require("child_process");
|
|
|
|
function getDirectories(path) {
|
|
return fs.readdirSync(path).filter(function (file) {
|
|
return fs.statSync(path + '/' + file).isDirectory();
|
|
});
|
|
}
|
|
|
|
const ROOT_DIR = __dirname
|
|
const SRC_DIR = path.join(ROOT_DIR, './src');
|
|
const FROM_PORT = 7000
|
|
|
|
// 获取所有src目录下的文件夹(文件夹名即appId)
|
|
const APP_IDS = getDirectories(SRC_DIR)
|
|
|
|
const LIST = APP_IDS.map((appId, i) => {
|
|
const workspace = path.join(SRC_DIR, `./${appId}`);
|
|
const devPort = FROM_PORT + i;
|
|
const devCmd = `cd ${workspace} && npm run serve -- --port ${devPort}`;
|
|
const buildCmd = `cd ${workspace} && npm run build`;
|
|
const installCmd = `cd ${workspace} && npm install`;
|
|
const manifest = require(path.join(workspace, './manifest.json'))
|
|
const entry = `http://localhost:${devPort}`
|
|
const icon = `http://localhost:${devPort}/icon.png`
|
|
return {
|
|
appId,
|
|
manifest: {
|
|
...manifest,
|
|
id: appId,
|
|
name: manifest.name || '未命名',
|
|
entry,
|
|
icon,
|
|
},
|
|
workspace,
|
|
devPort,
|
|
devCmd,
|
|
installCmd,
|
|
buildCmd,
|
|
}
|
|
}).filter((item) => {
|
|
return !item.manifest.disabled
|
|
});
|
|
|
|
// 安装依赖
|
|
function install() {
|
|
LIST.forEach((item) => {
|
|
console.log(`[appId:${item.appId} name:${item.manifest.name}] 开始安装依赖:`)
|
|
child_process.execSync(item.installCmd, { stdio: 'inherit' })
|
|
});
|
|
}
|
|
|
|
// 打包构建
|
|
function build() {
|
|
LIST.forEach((item) => {
|
|
console.log(`[appId:${item.appId} appName:${item.manifest.name}] 开始打包构建:`)
|
|
child_process.execSync(item.buildCmd, { stdio: 'inherit' })
|
|
fs.copySync(path.join(item.workspace, './dist'), path.join(ROOT_DIR, `./dist/${item.appId}`))
|
|
});
|
|
console.log('全部打包构建完成')
|
|
}
|
|
|
|
// 开发模式运行
|
|
function dev() {
|
|
const apps_widgets = {
|
|
apps: [],
|
|
widgets: []
|
|
}
|
|
LIST.forEach((item) => {
|
|
if (item.manifest.type === 'widget') {
|
|
apps_widgets.widgets.push(item.manifest)
|
|
} else {
|
|
apps_widgets.apps.push(item.manifest)
|
|
}
|
|
})
|
|
fs.writeFileSync(path.join(ROOT_DIR, './lib/platform/dist/apps_widgets.json'), JSON.stringify(apps_widgets, null, 2))
|
|
|
|
const cmd = LIST.reduce((acc, item) => {
|
|
acc += ` "${item.devCmd}"`
|
|
return acc;
|
|
}, 'npx concurrently');
|
|
child_process.execSync(cmd, { stdio: 'inherit' });
|
|
}
|
|
|
|
const arg = process.argv[2]
|
|
switch (arg) {
|
|
case 'install':
|
|
install()
|
|
break;
|
|
case 'dev':
|
|
dev();
|
|
break;
|
|
case 'build':
|
|
build();
|
|
break;
|
|
default:
|
|
console.error(`未知命令 ${arg}`)
|
|
}
|