publish 功能
This commit is contained in:
+12
-8
@@ -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
|
||||
)
|
||||
}
|
||||
|
||||
+91
-63
@@ -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("发布成功")
|
||||
}
|
||||
Reference in New Issue
Block a user