From e3304bbadeb837fe11cb0e3ed79681b931abb49f Mon Sep 17 00:00:00 2001
From: frostime <frostime@foxmail.com>
Date: Sat, 3 Jun 2023 18:09:57 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=B7=B2=E7=BB=8F?=
 =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E6=96=87=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 scripts/make_dev_link.js | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js
index 8033048..398a20b 100644
--- a/scripts/make_dev_link.js
+++ b/scripts/make_dev_link.js
@@ -6,9 +6,9 @@ import readline  from 'node:readline';
 
 //Please write the "workspace/data/plugins" directory here
 //请在这里填写你的 "workspace/data/plugins" 目录
-let targetDir = '';
+// let targetDir = '';
 //Like this
-// const targetDir = `H:\\SiYuanDevSpace\\data\\plugins`;
+const targetDir = `H:\\临时文件夹\\SiYuanDevSpace\\data\\plugins`;
 //********************************************************************************************
 
 const log = (info) => console.log(`\x1B[36m%s\x1B[0m`, info);
@@ -17,7 +17,7 @@ const error = (info) => console.log(`\x1B[31m%s\x1B[0m`, info);
 async function getSiYuanDir() {
     let url = 'http://127.0.0.1:6806/api/system/getWorkspaces';
     let header = {
-        // "Authorization": `Token ${token}`,
+        // "Authorization": `Token 6j8dkekjqjwb5dqq`,
         "Content-Type": "application/json",
     }
     let conf = {};
@@ -104,19 +104,45 @@ if (!name || name === '') {
 }
 
 //dev directory
-const devDir = `./dev`;
+const devDir = `${process.cwd()}/dev`;
 //mkdir if not exists
 if (!fs.existsSync(devDir)) {
     fs.mkdirSync(devDir);
 }
 
+function cmpPath(path1, path2) {
+    path1 = path1.replace(/\\/g, '/');
+    path2 = path2.replace(/\\/g, '/');
+    // sepertor at tail
+    if (path1[path1.length - 1] !== '/') {
+        path1 += '/';
+    }
+    if (path2[path2.length - 1] !== '/') {
+        path2 += '/';
+    }
+    return path1 === path2;
+}
+
 const targetPath = `${targetDir}/${name}`;
 //如果已经存在,就退出
 if (fs.existsSync(targetPath)) {
-    error(`Failed! Target directory  ${targetPath} already exists`);
+    let isSymbol = fs.lstatSync(targetPath).isSymbolicLink();
+
+    if (isSymbol) {
+        let srcPath = fs.readlinkSync(targetPath);
+        
+        if (cmpPath(srcPath, devDir)) {
+            log(`Good! ${targetPath} is already linked to ${devDir}`);
+        } else {
+            error(`Error! Already exists symbolic link ${targetPath}\nBut it links to ${srcPath}`);
+        }
+    } else {
+        error(`Failed! ${targetPath} already exists and is not a symbolic link`);
+    }
+
 } else {
     //创建软链接
-    fs.symlinkSync(`${process.cwd()}/dev`, targetPath, 'junction');
+    fs.symlinkSync(devDir, targetPath, 'junction');
     log(`Done! Created symlink ${targetPath}`);
 }
 

From d365b88d097023fd39b852d0708852506f102f9a Mon Sep 17 00:00:00 2001
From: frostime <frostime@foxmail.com>
Date: Sat, 3 Jun 2023 18:11:57 +0800
Subject: [PATCH 2/3] header

---
 scripts/make_dev_link.js | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js
index 398a20b..64fc467 100644
--- a/scripts/make_dev_link.js
+++ b/scripts/make_dev_link.js
@@ -14,17 +14,18 @@ const targetDir = `H:\\临时文件夹\\SiYuanDevSpace\\data\\plugins`;
 const log = (info) => console.log(`\x1B[36m%s\x1B[0m`, info);
 const error = (info) => console.log(`\x1B[31m%s\x1B[0m`, info);
 
+let POST_HEADER = {
+    // "Authorization": `Token ${token}`,
+    "Content-Type": "application/json",
+}
+
 async function getSiYuanDir() {
     let url = 'http://127.0.0.1:6806/api/system/getWorkspaces';
-    let header = {
-        // "Authorization": `Token 6j8dkekjqjwb5dqq`,
-        "Content-Type": "application/json",
-    }
     let conf = {};
     try {
         let response = await fetch(url, {
             method: 'POST',
-            headers: header
+            headers: POST_HEADER
         });
         if (response.ok) {
             conf = await response.json();

From 3f160a5b1fd719438a98e6fa3b2cecf1447ba826 Mon Sep 17 00:00:00 2001
From: frostime <frostime@foxmail.com>
Date: Sat, 3 Jun 2023 18:18:25 +0800
Subject: [PATCH 3/3] targetDir

---
 scripts/make_dev_link.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js
index 64fc467..392f734 100644
--- a/scripts/make_dev_link.js
+++ b/scripts/make_dev_link.js
@@ -6,9 +6,9 @@ import readline  from 'node:readline';
 
 //Please write the "workspace/data/plugins" directory here
 //请在这里填写你的 "workspace/data/plugins" 目录
-// let targetDir = '';
+let targetDir = '';
 //Like this
-const targetDir = `H:\\临时文件夹\\SiYuanDevSpace\\data\\plugins`;
+// let targetDir = `H:\\SiYuanDevSpace\\data\\plugins`;
 //********************************************************************************************
 
 const log = (info) => console.log(`\x1B[36m%s\x1B[0m`, info);