feat: add pack-deploy module

This commit is contained in:
熊潇 2025-02-18 13:52:29 +08:00
parent 169827b211
commit 67e5f52ff0
2 changed files with 61 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/envision-cli",
"version": "0.0.15",
"version": "0.0.16",
"description": "envision command tools",
"main": "dist/index.js",
"type": "module",

View File

@ -3,10 +3,11 @@ import path from 'path';
import * as tar from 'tar';
import glob from 'fast-glob';
import { program, Command } from '@/program.ts';
import { getBaseURL, getConfig } from '@/module/index.ts';
import { getBaseURL, getConfig, query } from '@/module/index.ts';
import { fileIsExist } from '@/uitls/file.ts';
import ignore from 'ignore';
import FormData from 'form-data';
import { chalk } from '@/module/chalk.ts';
// 查找文件(忽略大小写)
async function findFileInsensitive(targetFile: string): Promise<string | null> {
@ -289,19 +290,75 @@ const uploadFiles = async (filePath: string, collection: any): Promise<any> => {
);
});
};
const deployLoadFn = async (id: string, fileKey: string, force = false) => {
if (!id) {
console.error(chalk.red('id is required'));
return;
}
// pkg: {
// name: 'mark',
// version: '0.0.2',
// description: '',
// main: 'dist/app.mjs',
// app: [Object],
// files: [Array],
// scripts: [Object],
// keywords: [Array],
// author: 'abearxiong <xiongxiao@xiongxiao.me>',
// license: 'MIT',
// type: 'module',
// devDependencies: [Object],
// dependencies: [Object]
// },
const res = await query.post({
path: 'micro-app',
key: 'deploy',
data: {
id: id,
key: fileKey,
force: force,
},
});
if (res.code === 200) {
console.log('deploy-load success. current version:', res.data?.pkg?.version);
} else {
console.error('deploy-load failed', res.message);
}
return res;
};
const packCommand = new Command('pack')
.description('打包应用, 默认使用 package.json 中的 files 字段')
.option('-i, --ignore', '使用 .npmignore 文件模式去忽略文件进行打包, 不需要package.json中的files字段')
.option('-p, --publish', '打包并发布')
.option('-u, --update', 'show command for deploy to server')
.action(async (opts) => {
let value: { collection: Record<string, any>; outputFilePath: string } = await packLib(opts.ignore);
if (opts.publish && value?.collection) {
console.log('\n\npublish');
const { collection, outputFilePath } = value;
await uploadFiles(outputFilePath, collection);
try {
const res = await uploadFiles(outputFilePath, collection);
if (res.code === 200 && opts?.update) {
const id = res.data.id;
if (opts?.update) {
console.log(chalk.blue('example: '), 'envision pack-deploy ', id, '<key>');
console.log('envision pack-deploy ', id);
}
}
} catch (error) {
console.error('Error uploading file:', error);
}
}
//
});
const packDeployCommand = new Command('pack-deploy')
.argument('<id>', 'id')
.argument('<fileKey>', 'fileKey, 服务器的部署文件夹的列表')
.option('-f --force', 'force')
.action(async (id, fileKey, opts) => {
const { force } = opts || {};
const res = await deployLoadFn(id, fileKey, force);
});
program.addCommand(packDeployCommand);
program.addCommand(publishCommand);
program.addCommand(packCommand);