publish 功能

This commit is contained in:
Cheney 2025-03-13 11:27:11 +08:00
parent 5f5e73e1c9
commit 5ff621c05b
5 changed files with 152 additions and 76 deletions

View File

@ -82,7 +82,24 @@ async function main(){
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function () {
parser.addCmdLine("publish [localFile] [remotePath]", "发布到远程仓库;", async function (cli) {
const fs = require("fs");
let localFile = cli.getParamValue("localFile");
if ( ! fs.existsSync(localFile) ) {
$logger.error("本地文件不存在")
return
}
let remotePath = cli.getParamValue("remotePath");
if ( ! remotePath ) {
$logger.info("使用本地配置路径")
if ( ! fs.existsSync("meta.json") ) {
$logger.error("没有找到本地配置")
return
}
} else {
$logger.info("发布到远程路径" + remotePath)
}
let remote = await $context.get("remote")
if (!(await remote.check())) {
$logger.error("远程仓库不可用")
@ -90,7 +107,7 @@ async function main(){
} else {
$logger.info("远程仓库可用")
}
remote.publish()
await remote.publish(localFile, remotePath)
});
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli) {
let remote = await $context.get("remote")

View File

@ -9,7 +9,7 @@ const { pipeline, Transform} = require("stream/promises");
module.exports = class Remote {
constructor() {
this.init()
// this.__init()
this.func_publish = require("./publish")
this.func_install = require("./install")
}
@ -21,12 +21,14 @@ module.exports = class Remote {
const uConfig = await $context.get("uConfig");
if ( uConfig.get("remoteServer") ){
let server = uConfig.get("remoteServer");
$logger.info("server " + server)
if ( server.startsWith("webdav") ) {
server = "http" + server.substring(6)
}
if ( uConfig.get("remoteAuth") ) {
let up = uConfig.get("remoteAuth");
$logger.info("remoteAuth " + up)
up = up.split(":", 2)
this.client = this.webdav.createClient(
server,
@ -155,29 +157,31 @@ module.exports = class Remote {
return false;
}
async publish(){
return this.func_publish( this )
async publish(localFile, remotePath){
return await this.func_publish( this , localFile, remotePath )
}
async install(id){
return this.func_install( this, id )
}
async wUpload(from, to){
async wUpload(from, to, debug){
const totalSize = fs.statSync(from).size;
debug ? console.log("totalSize", totalSize) : null
const rs = fs.createReadStream( from );
let wSize = 0;
let rSize = 0;
rs.on("data", (r)=>{
rSize += r.length;
// console.log("r", rSize * 100 / totalSize)
debug ? console.log("r", rSize * 100 / totalSize + "%") : null
})
const ws = this.client.createWriteStream(to)
ws.on("data", (r)=>{
wSize += r.length;
// console.log("w", wSize * 100 / totalSize)
debug ? console.log("w ", wSize * 100 / totalSize + "%") : null
})
return pipeline( rs, ws);
await pipeline( rs, ws);
debug ? console.log("finish") : null
}
async wDownload(from, to){
@ -198,7 +202,7 @@ module.exports = class Remote {
wSize += r.length;
// console.log("w", wSize * 100 / totalSize)
})
return pipeline(
return await pipeline(
rs, ws
)
}

View File

@ -3,77 +3,105 @@ const fs = require("fs")
const JSON5 = require('json5')
const { pipeline } = require('stream/promises');
const objutil = require("../util/objutil")
module.exports = async function publish(remote, meta){
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 datautil = require("../util/datautil")
// 确认目录
// 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);
}
module.exports = async function publish(remote, localFile, remotePath) {
// 先更新文件
for (let dist of localMeta.dist ) {
if( remotePath ) {
const folder = "/modules/" + localMeta.id + "/" + localMeta.version + "/" + dist.os + "/" + dist.platform
await remote.wMkdir( folder );
// 如果有 meta
$logger.info("upload dist", dist.path, fs.existsSync(dist.path))
// 如果没有 meta
$logger.info("远程路径没有 meta, 按照默认规则上传");
await remote.wUpload( dist.path , folder + "/" + dist.filename );
// 确认目录
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 });
}
// 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("发布成功")
}

27
kit/src/util/datautil.js Normal file
View File

@ -0,0 +1,27 @@
module.exports = {
// 定义格式化日期的函数
formatDate : function (date){
// 获取年份
const year = date.getFullYear();
// 获取月份,注意月份是从 0 开始的,所以要加 1
const month = String(date.getMonth() + 1).padStart(2, '0');
// 获取日期
const day = String(date.getDate()).padStart(2, '0');
// 获取小时
const hours = String(date.getHours()).padStart(2, '0');
// 获取分钟
const minutes = String(date.getMinutes()).padStart(2, '0');
// 获取秒数
const seconds = String(date.getSeconds()).padStart(2, '0');
// 按照 yyyyMMddHHmmss 的格式拼接字符串
return `${year}${month}${day}${hours}${minutes}${seconds}`;
},
now : function (){
// 获取当前日期和时间
const currentDate = new Date();
// 调用格式化函数
const formattedDate = this.formatDate(currentDate);
return formattedDate
}
}

View File

@ -105,7 +105,7 @@ async function main(){
// }
// );
const client = webdav.createClient(
"http://172.16.18.45:5244/dav",
"http://baishe.fullstack.club:5244/dav",
{
username: "kit",
password: "kitkit"
@ -117,7 +117,7 @@ async function main(){
console.log(directoryItems)
// 下载测试
await wDownload(client, "/kit")
// await wDownload(client, "/kit")
// let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe")
// rs.on("data", (r)=>{
@ -136,7 +136,7 @@ async function main(){
// console.log("end")
// await wUpload( client, "./kit", "/kit" )
await wUpload( client, "D:\\fullstack\\TDevOps\\Readme.md", "/test/case1/data/20250313111818/Readme.md2" )
// await wUpload( client, "../dist/windows/kit.exe", "/modules/kit/0.1.20240227094429/Windows/X86_64/kit.exe" )
console.log("over")
}