124 lines
3.7 KiB
TypeScript
124 lines
3.7 KiB
TypeScript
import { program as app, Command } from '@/program.ts';
|
|
import { glob } from 'glob';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import FormData from 'form-data';
|
|
import { baseURL, getBaseURL } from '@/module/query.ts';
|
|
import { getConfig } from '@/module/index.ts';
|
|
import inquirer from 'inquirer';
|
|
|
|
const command = new Command('deploy')
|
|
.description('把前端文件传到服务器')
|
|
.argument('<filePath>', 'Path to the file to be uploaded') // 定义文件路径参数
|
|
.option('-v, --version <version>', 'verbose')
|
|
.option('-k, --key <key>', 'key')
|
|
.option('-y, --yes <yes>', 'yes')
|
|
.action(async (filePath, options) => {
|
|
try {
|
|
let { version, key, yes } = 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/**/*'], nodir: true });
|
|
const _relativeFiles = files.map((file) => file.replace(directory + '/', ''));
|
|
console.log('upload Files', _relativeFiles);
|
|
console.log('upload Files Key', key, version);
|
|
if (!yes) {
|
|
// 确认是否上传
|
|
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,
|
|
filepath: file,
|
|
});
|
|
}
|
|
form.append('appKey', key);
|
|
form.append('version', version);
|
|
return new Promise((resolve) => {
|
|
const _baseURL = getBaseURL();
|
|
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);
|