107 lines
4.3 KiB
JavaScript
107 lines
4.3 KiB
JavaScript
const path = require("path")
|
|
const fs = require("fs")
|
|
const JSON5 = require('json5')
|
|
const { pipeline } = require('stream/promises');
|
|
const objutil = require("../util/objutil")
|
|
const datautil = require("../util/datautil")
|
|
|
|
|
|
module.exports = async function publish(remote, localFile, remotePath) {
|
|
|
|
if( remotePath ) {
|
|
|
|
// 如果有 meta
|
|
|
|
// 如果没有 meta
|
|
$logger.info("远程路径没有 meta, 按照默认规则上传");
|
|
|
|
// 确认目录
|
|
let folder = path.posix.resolve("/" + remotePath + "/data/" + datautil.now() );
|
|
if ( ! await remote.client.exists( folder ) ) {
|
|
// 创建文件夹
|
|
await remote.client.createDirectory(folder, {
|
|
recursive: true
|
|
});
|
|
}
|
|
|
|
let filename = path.basename(localFile);
|
|
|
|
// 上传文件
|
|
await remote.wUpload( localFile , path.posix.resolve(folder , filename ), true);
|
|
|
|
} else {
|
|
|
|
let curPath = process.cwd();
|
|
const metaFilePath = path.resolve( curPath, "meta.json" );
|
|
if ( ! fs.existsSync( metaFilePath ) ) {
|
|
$logger.error("当前路径不包含 meta.json")
|
|
return
|
|
}
|
|
|
|
const metaFile = fs.readFileSync(metaFilePath);
|
|
|
|
const localMeta = JSON5.parse( metaFile );
|
|
|
|
// 更新版本
|
|
const date = new Date();
|
|
localMeta.version = `${localMeta.version}.${date.getFullYear()}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}${date.getHours().toString().padStart(2, '0')}${date.getMinutes().toString().padStart(2, '0')}${date.getSeconds().toString().padStart(2, '0')}`;
|
|
|
|
|
|
// 确认目录
|
|
// const directoryItems = await remote.getDirectoryContents("/modules");
|
|
// const modulesFolder = remote.getFileInfoByName(directoryItems, localMeta.id)
|
|
if ( ! await remote.client.exists("/modules/" + localMeta.id) ) {
|
|
// 创建文件夹
|
|
await remote.client.createDirectory("/modules/" + localMeta.id);
|
|
}
|
|
|
|
// 先更新文件
|
|
for (let dist of localMeta.dist ) {
|
|
|
|
const folder = "/modules/" + localMeta.id + "/" + localMeta.version + "/" + dist.os + "/" + dist.platform
|
|
await remote.wMkdir( folder );
|
|
|
|
$logger.info("upload dist", dist.path, fs.existsSync(dist.path))
|
|
|
|
await remote.wUpload( dist.path , folder + "/" + dist.filename );
|
|
|
|
}
|
|
|
|
// remote meta
|
|
let remoteMeta = {
|
|
"versions" : []
|
|
}
|
|
try {
|
|
const remoteMetaStr = await remote.client.getFileContents("/modules/" + localMeta.id + "/meta.json", { format: "text" });
|
|
remoteMeta = JSON5.parse( remoteMetaStr )
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
remoteMeta.id = localMeta.id;
|
|
remoteMeta.name = localMeta.name;
|
|
remoteMeta.desc = localMeta.desc;
|
|
remoteMeta.type = localMeta.type;
|
|
remoteMeta.vcs = localMeta.vcs;
|
|
// 废弃
|
|
// remoteMeta.platform = "cross" === localMeta.platform ? ["X86_64", "ARM64"] : localMeta.platform;
|
|
// remoteMeta.os = "cross" === localMeta.os ? ["Windows", "Linux", "Alpine"] : localMeta.os;
|
|
const dist = objutil.copy(localMeta.dist)
|
|
for (let d of dist) {
|
|
delete dist.path
|
|
}
|
|
remoteMeta.latest = {
|
|
version : localMeta.version,
|
|
dist
|
|
}
|
|
remoteMeta.versions.push(remoteMeta.latest)
|
|
if ( remoteMeta.versions.length >= 20 ) {
|
|
remoteMeta.versions = remoteMeta.versions.slice(0, 20);
|
|
}
|
|
|
|
await remote.client.putFileContents("/modules/" + localMeta.id + "/meta.json", JSON5.stringify(remoteMeta, null, 4) , { overwrite: true });
|
|
|
|
}
|
|
|
|
$logger.info("发布成功")
|
|
} |