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

@@ -1,11 +1,14 @@
import { program as app, Command } from '@/program.ts';
import { glob } from 'glob';
import glob from 'fast-glob';
import path from 'path';
import fs from 'fs';
import FormData from 'form-data';
import { baseURL, getBaseURL } from '@/module/query.ts';
import { getBaseURL, query } from '@/module/query.ts';
import { getConfig } from '@/module/index.ts';
import inquirer from 'inquirer';
import { packLib, unpackLib } from './publish.ts';
import chalk from 'chalk';
import { installDeps } from '@/uitls/npm.ts';
const command = new Command('deploy')
.description('把前端文件传到服务器')
@@ -37,8 +40,8 @@ const command = new Command('deploy')
const pwd = process.cwd();
const directory = path.join(pwd, filePath);
const gPath = path.join(directory, '**/*');
const files = await glob(gPath, { cwd: pwd, ignore: ['node_modules/**/*'], nodir: true });
const _relativeFiles = files.map((file) => file.replace(directory + '/', ''));
const files = await glob(gPath, { cwd: pwd, ignore: ['node_modules/**/*'], onlyFiles: true });
const _relativeFiles = files.map((file) => path.relative(directory, file));
console.log('upload Files', _relativeFiles);
console.log('upload Files Key', key, version);
if (!yes) {
@@ -124,8 +127,40 @@ app.addCommand(command);
const local = new Command('local')
.description('本地部署')
.option('-k, --key <key>', 'key')
.action(() => {
.argument('<key>', 'key')
.option('-i, --ignore', '使用 .npmignore 文件模式去忽略文件进行打包, 不需要package.json中的files字段')
.option('-u, --update', 'query查询 127.0.0.1:11015/api/router?path=local-apps&key=detect')
.action(async (key, opts) => {
console.log('local deploy');
const { outputFilePath } = await packLib(opts?.ignore);
const mainAppPath = getConfig().mainAppPath;
const appsPath = getConfig().appsPath || path.join(mainAppPath, 'apps');
if (!key) {
console.error(chalk.red('key is required'));
return;
}
const appPath = path.join(appsPath, key);
if (!fs.existsSync(appPath)) {
fs.mkdirSync(appPath, { recursive: true });
}
// 复制应用到apps目录下
if (outputFilePath) {
await unpackLib(outputFilePath, appPath);
fs.unlinkSync(outputFilePath);
installDeps({ appPath });
if (opts?.update) {
const res = await query.post(
{ path: 'local-apps', key: 'detect' },
{
url: `http://127.0.0.1:11015/api/router?path=local-apps&key=detect`,
},
);
if (res.code === 200) {
console.log('local deploy success');
} else {
console.error('local deploy failed', res.message);
}
}
}
});
app.addCommand(local);