Skip to content

Commit

Permalink
feat: 兼容挂件版
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 14, 2023
1 parent dc34341 commit e6754fa
Show file tree
Hide file tree
Showing 16 changed files with 554 additions and 21 deletions.
8 changes: 6 additions & 2 deletions libs/publisher-bridge/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "publisher-bridge",
"name": "@terwer/publisher-bridge",
"version": "0.0.1",
"type": "module",
"description": "a bridge between publisher plugin and widget",
Expand All @@ -18,13 +18,17 @@
"lib"
],
"scripts": {
"dev": "vite",
"serve": "vite",
"dev": "vite build --watch",
"build": "vite build",
"start": "vite preview",
"test": "vitest --watch"
},
"devDependencies": {
"@terwer/eslint-config-custom": "^1.2.0",
"@terwer/vite-config-custom": "^0.2.0"
},
"dependencies": {
"@terwer/publisher-hook": "workspace:*"
}
}
6 changes: 2 additions & 4 deletions libs/publisher-bridge/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const init = () => {
return "ok"
}
import PublishBridge from "./publish-bridge"

export default init
export default PublishBridge
104 changes: 104 additions & 0 deletions libs/publisher-bridge/src/publish-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import PublishHook from "@terwer/publisher-hook"
import { Env } from "zhi-env"
import { LogFactory, LogLevelEnum, DefaultLogger } from "zhi-log"
import { SiyuanDevice } from "zhi-device"

/**
* 挂件桥接器
*
* @author terwer
* @version 1.0.0
* @since 0.6.8
*/
class PublishBridge {
private readonly REPO_HASH_0_8_0 = "06148220cfc6344e18225d82604a193cc173f511"
private logger: DefaultLogger
private publishHook: PublishHook
private siyuanDevice

constructor() {
const env = new Env(import.meta.env)
this.logger = LogFactory.customLogFactory(LogLevelEnum.LOG_LEVEL_INFO, "publish-bridge", env).getLogger()
this.siyuanDevice = SiyuanDevice
this.publishHook = new PublishHook()
}

public async init(): Promise<void> {
this.logger.info("Initiating sy-post-publisher from publish bridge ...")
// 统一的初始化入口
try {
const dataDir = this.siyuanDevice.siyuanDataPath()
const sypFolder = `${dataDir}/widgets/sy-post-publisher`
const fs = this.siyuanDevice.requireLib("fs")
this.logger.info("Widget sy-post-publisher folder=>", sypFolder)
if (!fs.existsSync(sypFolder)) {
this.logger.info("Widget sy-post-publisher not exist, downloading...")
// 下载插件并解压
await this.doDownload()
this.logger.info("Widget sy-post-publisher downloaded")
}

// 下载完成,初始化
this.publishHook.doInit({
isInitLocalStorage: true,
// 桥接班禁用菜单插槽
isInitSlot: false,
isInitThemeAdaptor: true,
isInitPublishHelper: true,
isInitPicgoExtension: true,
isInitCmder: true,
})
} catch (e) {
this.logger.error("Failed to init sy-post-publisher,it may not work in some case.Error=>", e)
}
}

private async doDownload() {
this.logger.warn("Downloading sy-post-publisher from bazaar...")
const url = "/api/bazaar/installBazaarWidget"
const data = {
repoURL: "https://github.com/terwer/sy-post-publisher",
packageName: "sy-post-publisher",
repoHash: this.REPO_HASH_0_8_0,
mode: 0,
}
const fetchOps = {
body: JSON.stringify(data),
method: "POST",
}
const res = await fetch(url, fetchOps)
const resJson = await res.json()
if (resJson.code == 0) {
this.logger.info("Download sy-post-publisher from bazaar success")
} else {
throw new Error("Download sy-post-publisher error, this plugin will not work!")
}
}
}

export default PublishBridge
21 changes: 20 additions & 1 deletion libs/publisher-bridge/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@

import { resolve } from "path"
import { defineConfig } from "vite"
import minimist from "minimist"

const args = minimist(process.argv.slice(2))
const isWatch = args.watch || args.w
const devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/publish-tool/lib/bridge"
const distDir = isWatch ? devDistDir : "./dist"

export default defineConfig({
plugins: [],

build: {
// 输出路径
outDir: distDir,

lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, "src/index.ts"),
// the proper extensions will be added
fileName: "index",
formats: ["cjs"],
formats: ["es"],
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: [],
},
commonjsOptions: {
include: [],
},
},
optimizeDeps: {
esbuildOptions: {
platform: "node",
},
},

test: {
Expand Down
9 changes: 7 additions & 2 deletions libs/publisher-hook/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "publisher-hook",
"name": "@terwer/publisher-hook",
"version": "0.0.1",
"type": "module",
"description": "siyuanhook for sy-post-publisher widget",
Expand All @@ -18,13 +18,18 @@
"lib"
],
"scripts": {
"dev": "vite",
"serve": "vite",
"dev": "vite build --watch",
"build": "vite build",
"start": "vite preview",
"test": "vitest --watch"
},
"devDependencies": {
"@terwer/eslint-config-custom": "^1.2.0",
"@terwer/vite-config-custom": "^0.2.0"
},
"dependencies": {
"zhi-device": "^0.5.0",
"zhi-log": "^1.13.0"
}
}
7 changes: 2 additions & 5 deletions libs/publisher-hook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
const init = () => {
return "ok"
}

export default init
import PublishHook from "./publish-hook"
export default PublishHook
Loading

0 comments on commit e6754fa

Please sign in to comment.