init for deploy files
This commit is contained in:
118
src/command/deploy.ts
Normal file
118
src/command/deploy.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { app, Command } from '@/app.ts';
|
||||
import { glob } from 'glob';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import FormData from 'form-data';
|
||||
import { baseURL } from '@/module/query.ts';
|
||||
import { getConfig } from '@/module/index.ts';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
const command = new Command('deploy')
|
||||
.description('deploy to server')
|
||||
.argument('<filePath>', 'Path to the file to be uploaded') // 定义文件路径参数
|
||||
.option('-v, --version <version>', 'verbose')
|
||||
.option('-k, --key <key>', 'key')
|
||||
.action(async (filePath, options) => {
|
||||
try {
|
||||
let { version, key } = options;
|
||||
if (!version || !key) {
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'version',
|
||||
message: 'Enter your version:',
|
||||
when: () => !version,
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'key',
|
||||
message: 'Enter your key:',
|
||||
when: () => !key,
|
||||
},
|
||||
]);
|
||||
version = answers.version || version;
|
||||
key = answers.key || key;
|
||||
}
|
||||
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/**/*'] });
|
||||
const _relativeFiles = files.map((file) => file.replace(directory + '/', ''));
|
||||
console.log('upload Files', _relativeFiles);
|
||||
console.log('upload Files Key', key, version);
|
||||
// 确认是否上传
|
||||
const confirm = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
message: 'Do you want to upload these files?',
|
||||
},
|
||||
]);
|
||||
if (!confirm.confirm) {
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await uploadFiles(_relativeFiles, directory, { key, version });
|
||||
if (res?.code === 200) {
|
||||
console.log('File uploaded successfully!');
|
||||
res.data?.data?.files?.map?.((d) => {
|
||||
console.log('uploaded file', d?.name, d?.path);
|
||||
});
|
||||
} else {
|
||||
console.error('File upload failed', res?.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('error', error);
|
||||
}
|
||||
});
|
||||
|
||||
const uploadFiles = async (files: string[], directory: string, { key, version }: { key: string; version: string }): Promise<any> => {
|
||||
const config = await getConfig();
|
||||
const form = new FormData();
|
||||
for (const file of files) {
|
||||
const filePath = path.join(directory, file);
|
||||
form.append('file', fs.createReadStream(filePath), {
|
||||
filename: file,
|
||||
});
|
||||
}
|
||||
form.append('appKey', key);
|
||||
form.append('version', version);
|
||||
return new Promise((resolve) => {
|
||||
const url = new URL(baseURL);
|
||||
console.log('upload url', url.hostname, url.protocol, url.port);
|
||||
form.submit(
|
||||
{
|
||||
path: '/api/app/upload',
|
||||
host: url.hostname,
|
||||
protocol: url.protocol as any,
|
||||
port: url.port,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + config.token,
|
||||
...form.getHeaders(),
|
||||
},
|
||||
},
|
||||
(err, res) => {
|
||||
if (err) {
|
||||
console.error('Error uploading file:', err.message);
|
||||
return;
|
||||
}
|
||||
// 处理服务器响应
|
||||
let body = '';
|
||||
res.on('data', (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const res = JSON.parse(body);
|
||||
resolve(res);
|
||||
} catch (e) {
|
||||
resolve({ code: 500, message: body });
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
app.addCommand(command);
|
||||
Reference in New Issue
Block a user