Skip to content

Commit 2dc290d

Browse files
authored
Merge pull request #6 from siyuan-note/dev-auto-link
Dev auto link
2 parents aa19c76 + 3853269 commit 2dc290d

File tree

5 files changed

+110
-16
lines changed

5 files changed

+110
-16
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@
1717
- Notice: we **don't recommand** you to place the folder under your `{workspace}/data/plugins/` folder.
1818

1919
3. Install NodeJS and pnpm, then run pnpm i in the command line under your repo folder
20-
4. Create development symbolic links
20+
4. **Auto create development symbolic links**
21+
- Make sure that SiYuan is running
22+
- Run `pnpm run make-link`, the script will detec all the siyuan workspace, please select
23+
```bash
24+
>>> pnpm run make-link
25+
> [email protected] make-link H:\SrcCode\开源项目\plugin-sample-vite-svelte
26+
> node --no-warnings ./scripts/make_dev_link.js
27+
28+
"targetDir" is empty, try to get SiYuan directory automatically....
29+
Got 2 SiYuan workspaces
30+
[0] H:\Media\SiYuan
31+
[1] H:\临时文件夹\SiYuanDevSpace
32+
Please select a workspace[0-1]: 0
33+
Got target directory: H:\Media\SiYuan/data/plugins
34+
Done! Created symlink H:\Media\SiYuan/data/plugins/plugin-sample-vite-svelte
35+
```
36+
4. **Manually create development symbolic links**
2137
- Open `./scripts/make_dev_link.js` file, set `targetDir` to your SiYuan plugin directory `<siyuan workspace>/data/plugins`
2238
- Run `pnpm run make-link`, succeed if following message is shown:
2339
```bash
24-
❯❯❯ pnpm run make-link
40+
>>> pnpm run make-link
2541
> [email protected] make-link H:\SrcCode\plugin-sample-vite-svelte
2642
> node ./scripts/make_dev_link.js
2743

README_zh_CN.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@
1313
2. 将你的库克隆到本地开发文件夹中
1414
* 注意: 同 `plugin-sample` 不同, 本样例并不推荐直接把代码下载到 `{workspace}/data/plugins/`
1515
3. 安装 [NodeJS](https://nodejs.org/en/download)[pnpm](https://pnpm.io/installation),然后在开发文件夹下执行 `pnpm i` 安装所需要的依赖
16-
4. 创建开发需要的符号链接
16+
4. **自动创建符号链接**
17+
- 打开思源笔记, 确保思源内核正在运行
18+
- 运行 `pnpm run make-link`, 脚本会自动检测所有思源的工作空间, 请在命令行中手动输入序号以选择工作空间
19+
```bash
20+
>>> pnpm run make-link
21+
> [email protected] make-link H:\SrcCode\开源项目\plugin-sample-vite-svelte
22+
> node --no-warnings ./scripts/make_dev_link.js
23+
24+
"targetDir" is empty, try to get SiYuan directory automatically....
25+
Got 2 SiYuan workspaces
26+
[0] H:\Media\SiYuan
27+
[1] H:\临时文件夹\SiYuanDevSpace
28+
Please select a workspace[0-1]: 0
29+
Got target directory: H:\Media\SiYuan/data/plugins
30+
Done! Created symlink H:\Media\SiYuan/data/plugins/plugin-sample-vite-svelte
31+
```
32+
4. **手动创建符号链接**
1733
- 打开 `./scripts/make_dev_link.js` 文件,更改 `targetDir` 为思源的插件目录 `<siyuan workspace>/data/plugins`
1834
- 运行 `pnpm run make-link` 命令, 如果看到类似以下的消息,说明创建成功:
1935
```bash

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "plugin-sample-vite-svelte",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"type": "module",
55
"description": "",
66
"repository": "",
77
"homepage": "",
88
"author": "",
99
"license": "GPL-3.0",
1010
"scripts": {
11-
"make-link": "node ./scripts/make_dev_link.js",
11+
"make-link": "node --no-warnings ./scripts/make_dev_link.js",
1212
"dev": "vite build --watch",
1313
"build": "vite build"
1414
},

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "plugin-sample-vite-svelte",
33
"author": "frostime",
44
"url": "https://github.com/siyuan-note/plugin-sample-vite-svelte",
5-
"version": "0.0.2",
5+
"version": "0.0.3",
66
"displayName": {
77
"en_US": "Plugin sample with vite and svelte",
88
"zh_CN": "插件样例 vite + svelte 版"

scripts/make_dev_link.js

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,85 @@
11
import fs from 'fs';
2+
import readline from 'node:readline';
23

34

45
//************************************ Write you dir here ************************************
56

67
//Please write the "workspace/data/plugins" directory here
78
//请在这里填写你的 "workspace/data/plugins" 目录
8-
const targetDir = '';
9+
let targetDir = '';
910
//Like this
1011
// const targetDir = `H:\\SiYuanDevSpace\\data\\plugins`;
1112
//********************************************************************************************
1213

14+
const log = console.log;
1315

16+
async function getSiYuanDir() {
17+
let url = 'http://127.0.0.1:6806/api/system/getWorkspaces';
18+
let header = {
19+
// "Authorization": `Token ${token}`,
20+
"Content-Type": "application/json",
21+
}
22+
let conf = {};
23+
try {
24+
let response = await fetch(url, {
25+
method: 'POST',
26+
headers: header
27+
});
28+
if (response.ok) {
29+
conf = await response.json();
30+
} else {
31+
log(`HTTP-Error: ${response.status}`);
32+
return null;
33+
}
34+
} catch (e) {
35+
log("Error:", e);
36+
log("Please make sure SiYuan is running!!!");
37+
return null;
38+
}
39+
return conf.data;
40+
}
41+
42+
async function chooseTarget(workspaces) {
43+
let count = workspaces.length;
44+
log(`Got ${count} SiYuan ${count > 1 ? 'workspaces' : 'workspace'}`)
45+
for (let i = 0; i < workspaces.length; i++) {
46+
log(`[${i}] ${workspaces[i].path}`);
47+
}
48+
49+
if (count == 1) {
50+
return `${workspaces[0].path}/data/plugins`;
51+
} else {
52+
const rl = readline.createInterface({
53+
input: process.stdin,
54+
output: process.stdout
55+
});
56+
let index = await new Promise((resolve, reject) => {
57+
rl.question(`Please select a workspace[0-${count-1}]: `, (answer) => {
58+
resolve(answer);
59+
});
60+
});
61+
rl.close();
62+
return `${workspaces[index].path}/data/plugins`;
63+
}
64+
}
1465

66+
if (targetDir === '') {
67+
log('"targetDir" is empty, try to get SiYuan directory automatically....')
68+
let res = await getSiYuanDir();
69+
70+
if (res === null) {
71+
log('Failed! You can set the plugin directory in scripts/make_dev_link.js and try again');
72+
process.exit(1);
73+
}
74+
75+
targetDir = await chooseTarget(res);
76+
log(`Got target directory: ${targetDir}`);
77+
}
1578

1679
//Check
1780
if (!fs.existsSync(targetDir)) {
18-
console.log(`Failed! plugin directory not exists: "${targetDir}"`);
19-
console.log(`Please set the plugin directory in scripts/make_dev_link.js`);
81+
log(`Failed! plugin directory not exists: "${targetDir}"`);
82+
log(`Please set the plugin directory in scripts/make_dev_link.js`);
2083
process.exit(1);
2184
}
2285

@@ -31,7 +94,7 @@ if (!fs.existsSync('./plugin.json')) {
3194
const plugin = JSON.parse(fs.readFileSync('./plugin.json', 'utf8'));
3295
const name = plugin?.name;
3396
if (!name || name === '') {
34-
console.log('Failed! Please set plugin name in plugin.json');
97+
log('Failed! Please set plugin name in plugin.json');
3598
process.exit(1);
3699
}
37100

@@ -45,11 +108,10 @@ if (!fs.existsSync(devDir)) {
45108
const targetPath = `${targetDir}/${name}`;
46109
//如果已经存在,就退出
47110
if (fs.existsSync(targetPath)) {
48-
console.log('Failed! Target directory already exists');
49-
process.exit(1);
111+
log(`Failed! Target directory ${targetPath} already exists`);
112+
} else {
113+
//创建软链接
114+
fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction');
115+
log(`Done! Created symlink ${targetPath}`);
50116
}
51117

52-
//创建软链接
53-
fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction');
54-
console.log(`Done! Created symlink ${targetPath}`);
55-

0 commit comments

Comments
 (0)