feat: 添加publish

This commit is contained in:
2024-12-05 17:31:32 +08:00
parent da6211299b
commit a117281b9e
7 changed files with 141 additions and 39 deletions

View File

@@ -4,7 +4,8 @@ import path from 'path';
import fs from 'fs';
import { chalk } from '@/module/chalk.ts';
import inquirer from 'inquirer';
import { spawn, spawnSync } from 'child_process';
import { spawn } from 'child_process';
import { installDeps } from '@/uitls/npm.ts';
const command = new Command('init').description('初始化应用').action((optison) => {
console.log('init');
@@ -17,6 +18,7 @@ const setMainAppConfig = async (mainAppPath: string) => {
config.mainAppPath = mainAppPath;
writeConfig(config);
};
const initMain = async () => {
const mainAppPath = path.join(envisionPath, 'main-app');
const pkgPath = path.join(mainAppPath, 'package.json');
@@ -26,34 +28,29 @@ const initMain = async () => {
name: 'main-app',
version: '1.0.0',
type: 'module',
main: 'dist/app.mjs',
scripts: {
start: 'node dist/app.mjs',
},
dependencies: {
'@kevisual/router': 'latest',
'@kevisual/local-app-manager': 'latest',
'@kevisual/use-config': 'latest',
},
};
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
console.log(chalk.green('初始化 main-app 成功'));
}
// 在对应的路径,安装依赖
const needDeps = ['@kevisual/router', '@kevisual/local-app-manager', '@kevisual/use-config'];
let hasPnpm = false;
try {
spawnSync('pnpm', ['--version']);
hasPnpm = true;
} catch (e) {
hasPnpm = false;
}
for (let dep of needDeps) {
console.log(chalk.green('安装依赖', dep));
const npm = hasPnpm ? 'pnpm' : 'npm';
spawnSync(npm, ['install', dep], { cwd: mainAppPath, stdio: 'inherit' });
}
installDeps({ appPath: mainAppPath, sync: true });
// 创建 dist/app.mjs
const code = `import { App } from '@kevisual/router';
import { app as LocalApp } from '@kevisual/local-app-manager';
import { useConfig } from '@kevisual/use-config';
const config = useConfig();
const app = new App();
app.importApp(LocalApp);
const host = config.host || '127.0.0.1';
app.listen(config.port, host, () => {
console.log(\`app is running on http://\${host}:\${config.port}\`);
LocalApp.listen(config.port, host, () => {
console.log(\`LocalApp is running on http://\${host}:\${config.port}\`);
});
`;
const codeDistPath = path.join(mainAppPath, 'dist');
@@ -92,6 +89,7 @@ app.listen(config.port, host, () => {
//
console.log(chalk.green('创建 app.config.json5 成功'));
setMainAppConfig(mainAppPath);
writeConfig({ ...getConfig(), appsPath: answers.appsPath });
};
const checkPid = (pid: number) => {
try {
@@ -124,6 +122,8 @@ const mainApp = new Command('main')
if (options.init) {
await initMain();
return;
} else {
console.warn(chalk.yellow('请使用 --init 初始化 main 应用'));
}
const runChild = () => {
const logDir = path.join(envisionPath, 'log');
@@ -173,7 +173,7 @@ const mainApp = new Command('main')
}
}
runChild();
} else if (options.stop) {
} else if (options.exit) {
if (config.mainAppPid) {
// 检查是否有进程
if (checkPid(config.mainAppPid)) {