From 831ba76c7ca170d03b20bdca80553ca457dbf91a Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 21:34:17 +0800 Subject: [PATCH 1/6] dev --- scripts/make_dev_link.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js index 893d9f9..003edef 100644 --- a/scripts/make_dev_link.js +++ b/scripts/make_dev_link.js @@ -11,7 +11,28 @@ const targetDir = ''; //******************************************************************************************** +async function getSiYuanDir() { + let url = 'http://127.0.0.1:6806/api/system/getConf'; + let header = { + // "Authorization": `Token ${token}`, + "Content-Type": "application/json", + } + try { + let conf = await fetch(url, { + method: 'POST', + headers: header + }); + console.log(conf); + } catch (e) { + console.log('Failed! Please make sure SiYuan is running'); + process.exit(1); + } +} +if (targetDir === '') { + await getSiYuanDir(); + process.exit(0); +} //Check if (!fs.existsSync(targetDir)) { From c29ea972404957ec24b76e50585be0915ab17018 Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 21:34:17 +0800 Subject: [PATCH 2/6] commit --- scripts/make_dev_link.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js index 003edef..85131a2 100644 --- a/scripts/make_dev_link.js +++ b/scripts/make_dev_link.js @@ -11,10 +11,10 @@ const targetDir = ''; //******************************************************************************************** -async function getSiYuanDir() { +async function getSiYuanDir(token) { let url = 'http://127.0.0.1:6806/api/system/getConf'; let header = { - // "Authorization": `Token ${token}`, + "Authorization": `Token ${token}`, "Content-Type": "application/json", } try { @@ -24,13 +24,13 @@ async function getSiYuanDir() { }); console.log(conf); } catch (e) { - console.log('Failed! Please make sure SiYuan is running'); + console.log(e); process.exit(1); } } if (targetDir === '') { - await getSiYuanDir(); + await getSiYuanDir('es7zy9zewgibc6dt'); process.exit(0); } From 9007a64178c1fca7b7c334c8a4ff11286f43a209 Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 22:20:40 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=AF=BB=E5=8F=96=20work?= =?UTF-8?q?space=20=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/make_dev_link.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js index 85131a2..c813a56 100644 --- a/scripts/make_dev_link.js +++ b/scripts/make_dev_link.js @@ -10,34 +10,43 @@ const targetDir = ''; // const targetDir = `H:\\SiYuanDevSpace\\data\\plugins`; //******************************************************************************************** +const log = console.log; -async function getSiYuanDir(token) { - let url = 'http://127.0.0.1:6806/api/system/getConf'; +async function getSiYuanDir() { + let url = 'http://127.0.0.1:6806/api/system/getWorkspaces'; let header = { - "Authorization": `Token ${token}`, + // "Authorization": `Token ${token}`, "Content-Type": "application/json", } + let conf = {}; try { - let conf = await fetch(url, { + let response = await fetch(url, { method: 'POST', headers: header }); - console.log(conf); + if (response.ok) { + conf = await response.json(); + } else { + log(`HTTP-Error: ${response.status}`); + process.exit(1); + } } catch (e) { - console.log(e); + log("Error:", e); process.exit(1); } + return conf.data; } if (targetDir === '') { - await getSiYuanDir('es7zy9zewgibc6dt'); + let res = await getSiYuanDir(); + log(res); process.exit(0); } //Check if (!fs.existsSync(targetDir)) { - console.log(`Failed! plugin directory not exists: "${targetDir}"`); - console.log(`Please set the plugin directory in scripts/make_dev_link.js`); + log(`Failed! plugin directory not exists: "${targetDir}"`); + log(`Please set the plugin directory in scripts/make_dev_link.js`); process.exit(1); } @@ -52,7 +61,7 @@ if (!fs.existsSync('./plugin.json')) { const plugin = JSON.parse(fs.readFileSync('./plugin.json', 'utf8')); const name = plugin?.name; if (!name || name === '') { - console.log('Failed! Please set plugin name in plugin.json'); + log('Failed! Please set plugin name in plugin.json'); process.exit(1); } @@ -66,11 +75,11 @@ if (!fs.existsSync(devDir)) { const targetPath = `${targetDir}/${name}`; //如果已经存在,就退出 if (fs.existsSync(targetPath)) { - console.log('Failed! Target directory already exists'); + log('Failed! Target directory already exists'); process.exit(1); } //创建软链接 fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction'); -console.log(`Done! Created symlink ${targetPath}`); +log(`Done! Created symlink ${targetPath}`); From c99faadc2ae73c5f4fdd3ab7d847f90326793138 Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 22:39:14 +0800 Subject: [PATCH 4/6] auto detect workspace --- package.json | 2 +- scripts/make_dev_link.js | 53 +++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 2f98a3b..faf7e2d 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "author": "", "license": "GPL-3.0", "scripts": { - "make-link": "node ./scripts/make_dev_link.js", + "make-link": "node --no-warnings ./scripts/make_dev_link.js", "dev": "vite build --watch", "build": "vite build" }, diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js index c813a56..5c8ba19 100644 --- a/scripts/make_dev_link.js +++ b/scripts/make_dev_link.js @@ -1,11 +1,12 @@ import fs from 'fs'; +import readline from 'node:readline'; //************************************ Write you dir here ************************************ //Please write the "workspace/data/plugins" directory here //请在这里填写你的 "workspace/data/plugins" 目录 -const targetDir = ''; +let targetDir = ''; //Like this // const targetDir = `H:\\SiYuanDevSpace\\data\\plugins`; //******************************************************************************************** @@ -28,19 +29,50 @@ async function getSiYuanDir() { conf = await response.json(); } else { log(`HTTP-Error: ${response.status}`); - process.exit(1); + return null; } } catch (e) { log("Error:", e); - process.exit(1); + return null; } return conf.data; } +async function chooseTarget(workspaces) { + let count = workspaces.length; + log(`Got ${count} SiYuan ${count > 1 ? 'workspaces' : 'workspace'}`) + for (let i = 0; i < workspaces.length; i++) { + log(`[${i}] ${workspaces[i].path}`); + } + + if (count == 1) { + return `${workspaces[0].path}/data/plugins`; + } else { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + let index = await new Promise((resolve, reject) => { + rl.question(`Please select a workspace[0-${count-1}]: `, (answer) => { + resolve(answer); + }); + }); + rl.close(); + return `${workspaces[index].path}/data/plugins`; + } +} + if (targetDir === '') { + log('"targetDir" is empty, try to get SiYuan directory automatically....') let res = await getSiYuanDir(); - log(res); - process.exit(0); + + if (res === null) { + log('Failed! Please set the plugin directory in scripts/make_dev_link.js'); + process.exit(1); + } + + targetDir = await chooseTarget(res); + log(`Got target directory: ${targetDir}`); } //Check @@ -75,11 +107,10 @@ if (!fs.existsSync(devDir)) { const targetPath = `${targetDir}/${name}`; //如果已经存在,就退出 if (fs.existsSync(targetPath)) { - log('Failed! Target directory already exists'); - process.exit(1); + log(`Failed! Target directory ${targetPath} already exists`); +} else { + //创建软链接 + fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction'); + log(`Done! Created symlink ${targetPath}`); } -//创建软链接 -fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction'); -log(`Done! Created symlink ${targetPath}`); - From b3088200b1c3baa951e7ae694049a40b9b0ec6c3 Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 22:50:03 +0800 Subject: [PATCH 5/6] update readme --- README.md | 20 ++++++++++++++++++-- README_zh_CN.md | 18 +++++++++++++++++- package.json | 2 +- plugin.json | 2 +- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d3edd63..9fcfcaa 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,27 @@ - Notice: we **don't recommand** you to place the folder under your `{workspace}/data/plugins/` folder. 3. Install NodeJS and pnpm, then run pnpm i in the command line under your repo folder -4. Create development symbolic links +4. **Auto create development symbolic links** + - Make sure that SiYuan is running + - Run `pnpm run make-link`, the script will detec all the siyuan workspace, please select + ```bash + >>> pnpm run make-link + > plugin-sample-vite-svelte@0.0.3 make-link H:\SrcCode\开源项目\plugin-sample-vite-svelte + > node --no-warnings ./scripts/make_dev_link.js + + "targetDir" is empty, try to get SiYuan directory automatically.... + Got 2 SiYuan workspaces + [0] H:\Media\SiYuan + [1] H:\临时文件夹\SiYuanDevSpace + Please select a workspace[0-1]: 0 + Got target directory: H:\Media\SiYuan/data/plugins + Done! Created symlink H:\Media\SiYuan/data/plugins/plugin-sample-vite-svelte + ``` +4. **Manually create development symbolic links** - Open `./scripts/make_dev_link.js` file, set `targetDir` to your SiYuan plugin directory `/data/plugins` - Run `pnpm run make-link`, succeed if following message is shown: ```bash - ❯❯❯ pnpm run make-link + >>> pnpm run make-link > plugin-sample-vite-svelte@0.0.1 make-link H:\SrcCode\plugin-sample-vite-svelte > node ./scripts/make_dev_link.js diff --git a/README_zh_CN.md b/README_zh_CN.md index dbcf283..5e40813 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -13,7 +13,23 @@ 2. 将你的库克隆到本地开发文件夹中 * 注意: 同 `plugin-sample` 不同, 本样例并不推荐直接把代码下载到 `{workspace}/data/plugins/` 3. 安装 [NodeJS](https://nodejs.org/en/download) 和 [pnpm](https://pnpm.io/installation),然后在开发文件夹下执行 `pnpm i` 安装所需要的依赖 -4. 创建开发需要的符号链接 +4. **自动创建符号链接** + - 打开思源笔记, 确保思源内核正在运行 + - 运行 `pnpm run make-link`, 脚本会自动检测所有思源的工作空间, 请在命令行中手动输入序号以选择工作空间 + ```bash + >>> pnpm run make-link + > plugin-sample-vite-svelte@0.0.3 make-link H:\SrcCode\开源项目\plugin-sample-vite-svelte + > node --no-warnings ./scripts/make_dev_link.js + + "targetDir" is empty, try to get SiYuan directory automatically.... + Got 2 SiYuan workspaces + [0] H:\Media\SiYuan + [1] H:\临时文件夹\SiYuanDevSpace + Please select a workspace[0-1]: 0 + Got target directory: H:\Media\SiYuan/data/plugins + Done! Created symlink H:\Media\SiYuan/data/plugins/plugin-sample-vite-svelte + ``` +4. **手动创建符号链接** - 打开 `./scripts/make_dev_link.js` 文件,更改 `targetDir` 为思源的插件目录 `/data/plugins` - 运行 `pnpm run make-link` 命令, 如果看到类似以下的消息,说明创建成功: ```bash diff --git a/package.json b/package.json index faf7e2d..b3ff9ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plugin-sample-vite-svelte", - "version": "0.0.2", + "version": "0.0.3", "type": "module", "description": "", "repository": "", diff --git a/plugin.json b/plugin.json index 60a4ede..f625d9f 100644 --- a/plugin.json +++ b/plugin.json @@ -2,7 +2,7 @@ "name": "plugin-sample-vite-svelte", "author": "frostime", "url": "https://github.com/siyuan-note/plugin-sample-vite-svelte", - "version": "0.0.2", + "version": "0.0.3", "displayName": { "en_US": "Plugin sample with vite and svelte", "zh_CN": "插件样例 vite + svelte 版" From 3853269e8243250aa0862f29b51f61427ae7525a Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 22 May 2023 22:53:25 +0800 Subject: [PATCH 6/6] log --- scripts/make_dev_link.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js index 5c8ba19..f00a456 100644 --- a/scripts/make_dev_link.js +++ b/scripts/make_dev_link.js @@ -33,6 +33,7 @@ async function getSiYuanDir() { } } catch (e) { log("Error:", e); + log("Please make sure SiYuan is running!!!"); return null; } return conf.data; @@ -67,7 +68,7 @@ if (targetDir === '') { let res = await getSiYuanDir(); if (res === null) { - log('Failed! Please set the plugin directory in scripts/make_dev_link.js'); + log('Failed! You can set the plugin directory in scripts/make_dev_link.js and try again'); process.exit(1); }