feat:add aliyun oss deploy

This commit is contained in:
zyronon
2025-08-16 01:17:53 +08:00
parent 5be67976ab
commit 18e22f4733
3 changed files with 1 additions and 1 deletions

86
scripts/deploy-oss.js Normal file
View File

@@ -0,0 +1,86 @@
import OSS from 'ali-oss'
import fs from 'fs'
import path from 'path'
import Core from '@alicloud/pop-core'
const {
OSS_REGION,
OSS_KEY_ID,
OSS_KEY_SECRET,
OSS_BUCKET,
CDN_DOMAIN
} = process.env
if (!OSS_REGION || !OSS_KEY_ID || !OSS_KEY_SECRET || !OSS_BUCKET || !CDN_DOMAIN) {
console.error('❌ 缺少必要的环境变量,请检查 GitHub Secrets 配置')
process.exit(1)
}
const client = new OSS({
region: OSS_REGION,
accessKeyId: OSS_KEY_ID,
accessKeySecret: OSS_KEY_SECRET,
bucket: OSS_BUCKET
})
const cdnClient = new Core({
accessKeyId: OSS_KEY_ID,
accessKeySecret: OSS_KEY_SECRET,
endpoint: 'https://cdn.aliyuncs.com',
apiVersion: '2018-05-10'
})
// 遍历 dist 目录,统计文件
function getAllFiles(dir, fileList = []) {
const files = fs.readdirSync(dir)
for (const file of files) {
const filePath = path.join(dir, file)
const stat = fs.statSync(filePath)
if (stat.isDirectory()) {
getAllFiles(filePath, fileList)
} else {
fileList.push(filePath)
}
}
return fileList
}
// 上传文件,显示进度
async function uploadFiles(files, localBase = './dist') {
const total = files.length
let count = 0
for (const file of files) {
const relativePath = path.relative(localBase, file)
const remotePath = relativePath.split(path.sep).join('/') // 转 POSIX 路径
await client.put(remotePath, file)
count++
const percent = ((count / total) * 100).toFixed(1)
process.stdout.write(`\r📤 上传进度: ${count}/${total} (${percent}%) ${remotePath} `)
}
console.log('\n✅ 文件全部上传完成')
}
// 刷新 CDN
async function refreshCDN() {
console.log('🔄 刷新 CDN 缓存...')
const params = {
ObjectPath: `https://${CDN_DOMAIN}/*`,
ObjectType: 'File'
}
const requestOption = { method: 'POST' }
const result = await cdnClient.request('RefreshObjectCaches', params, requestOption)
console.log('✅ CDN 刷新完成:', result)
}
async function main() {
const files = getAllFiles('./dist')
console.log(`📁 共找到 ${files.length} 个文件,开始上传...`)
await uploadFiles(files)
await refreshCDN()
}
main().catch(err => {
console.error('❌ 部署失败:', err)
process.exit(1)
})

60
scripts/gulpfile.esm.js Normal file
View File

@@ -0,0 +1,60 @@
import { src, dest } from 'gulp';
import through from 'through2';
import * as XLSX from 'xlsx';
import * as path from 'path';
function excel2json() {
let json = {};
const stream = through.obj(function(file, encode, cb) {
if (!file.isBuffer()) {
return cb(null, file);
}
const workbook = XLSX.read(file.contents);
// const excelData = XLSX.utils.sheet_to_json(
// workbook.Sheets[workbook.SheetNames[0]],
// );
const excelData = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1']);
json = excelData.reduce((result, current) => {
let newCurrent = {};
for (var key in current) {
var letterPattern = /[a-zA-Z]+/g;
var matches = key.match(letterPattern);
if (matches) {
var string = matches[0].toLocaleLowerCase();
newCurrent[string] = current[key].replace(/@{/g, '{');
}
}
result[newCurrent.key] = {};
result[newCurrent.key]['en'] = newCurrent.en || '';
result[newCurrent.key]['zh'] = newCurrent.zh || '';
result[newCurrent.key]['id'] = newCurrent.id || '';
result[newCurrent.key]['tw'] = newCurrent.tw || '';
result[newCurrent.key]['th'] = newCurrent.th || '';
result[newCurrent.key]['ru'] = newCurrent.ru || '';
result[newCurrent.key]['vi'] = newCurrent.vi || '';
result[newCurrent.key]['es'] = newCurrent.es || '';
result[newCurrent.key]['pt'] = newCurrent.pt || '';
result[newCurrent.key]['ja'] = newCurrent.ja || '';
result[newCurrent.key]['uk'] = newCurrent.uk || '';
result[newCurrent.key]['ko'] = newCurrent.ko || '';
result[newCurrent.key]['de'] = newCurrent.de || '';
result[newCurrent.key]['fr'] = newCurrent.fr || '';
return result;
}, json);
file.contents = Buffer.from(JSON.stringify(json, null, '\t'));
file.path = path.join(file.base, 'i18n.json');
cb(null, file);
});
return stream;
}
// 将翻译好的excel写入json
function i18nwrite() {
return src(['./src/locales/i18n.xlsx'])
.pipe(excel2json())
.pipe(dest('src/locales'));
}
export { i18nwrite };