🔨 refactor: 创建 dir 符号链接;并编写 ps 脚本用于获取 windows 管理员权限
https://github.com/siyuan-note/siyuan/issues/12399
This commit is contained in:
parent
da4c4ced8f
commit
e345e18613
7 changed files with 327 additions and 355 deletions
|
@ -1,106 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2024 by frostime. All Rights Reserved.
|
||||
* @Author : frostime
|
||||
* @Date : 2023-07-15 15:31:31
|
||||
* @FilePath : /scripts/make_dev_link.js
|
||||
* @LastEditTime : 2024-09-06 18:13:53
|
||||
* @Description :
|
||||
*/
|
||||
// make_dev_link.js
|
||||
import fs from 'fs';
|
||||
import http from 'node:http';
|
||||
import readline from 'node:readline';
|
||||
import { log, error, getSiYuanDir, chooseTarget, getThisPluginName, makeSymbolicLink } from './utils.js';
|
||||
|
||||
|
||||
//************************************ Write you dir here ************************************
|
||||
|
||||
//Please write the "workspace/data/plugins" directory here
|
||||
//请在这里填写你的 "workspace/data/plugins" 目录
|
||||
let targetDir = '';
|
||||
//Like this
|
||||
// let 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 myfetch(url, options) {
|
||||
//使用 http 模块,从而兼容那些不支持 fetch 的 nodejs 版本
|
||||
return new Promise((resolve, reject) => {
|
||||
let req = http.request(url, options, (res) => {
|
||||
let data = '';
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
ok: true,
|
||||
status: res.statusCode,
|
||||
json: () => JSON.parse(data)
|
||||
});
|
||||
});
|
||||
});
|
||||
req.on('error', (e) => {
|
||||
reject(e);
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function getSiYuanDir() {
|
||||
let url = 'http://127.0.0.1:6806/api/system/getWorkspaces';
|
||||
let conf = {};
|
||||
try {
|
||||
let response = await myfetch(url, {
|
||||
method: 'POST',
|
||||
headers: POST_HEADER
|
||||
});
|
||||
if (response.ok) {
|
||||
conf = await response.json();
|
||||
} else {
|
||||
error(`\tHTTP-Error: ${response.status}`);
|
||||
return null;
|
||||
}
|
||||
} catch (e) {
|
||||
error(`\tError: ${e}`);
|
||||
error("\tPlease make sure SiYuan is running!!!");
|
||||
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(`\t[${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(`\tPlease select a workspace[0-${count-1}]: `, (answer) => {
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
rl.close();
|
||||
return `${workspaces[index].path}/data/plugins`;
|
||||
}
|
||||
}
|
||||
|
||||
log('>>> Try to visit constant "targetDir" in make_dev_link.js...')
|
||||
|
||||
/**
|
||||
* 1. Get the parent directory to install the plugin
|
||||
*/
|
||||
log('>>> Try to visit constant "targetDir" in make_dev_link.js...');
|
||||
if (targetDir === '') {
|
||||
log('>>> Constant "targetDir" is empty, try to get SiYuan directory automatically....')
|
||||
log('>>> Constant "targetDir" is empty, try to get SiYuan directory automatically....');
|
||||
let res = await getSiYuanDir();
|
||||
|
||||
if (res === null || res === undefined || res.length === 0) {
|
||||
log('>>> Can not get SiYuan directory automatically, try to visit environment variable "SIYUAN_PLUGIN_DIR"....');
|
||||
|
||||
// console.log(process.env)
|
||||
if (!res || res.length === 0) {
|
||||
log('>>> Can not get SiYuan directory automatically, try to visit environment variable "SIYUAN_PLUGIN_DIR"....');
|
||||
let env = process.env?.SIYUAN_PLUGIN_DIR;
|
||||
if (env !== undefined && env !== null && env !== '') {
|
||||
if (env) {
|
||||
targetDir = env;
|
||||
log(`\tGot target directory from environment variable "SIYUAN_PLUGIN_DIR": ${targetDir}`);
|
||||
} else {
|
||||
|
@ -111,76 +34,33 @@ if (targetDir === '') {
|
|||
targetDir = await chooseTarget(res);
|
||||
}
|
||||
|
||||
|
||||
log(`>>> Successfully got target directory: ${targetDir}`);
|
||||
}
|
||||
|
||||
//Check
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
error(`Failed! plugin directory not exists: "${targetDir}"`);
|
||||
error(`Please set the plugin directory in scripts/make_dev_link.js`);
|
||||
error(`Failed! Plugin directory not exists: "${targetDir}"`);
|
||||
error('Please set the plugin directory in scripts/make_dev_link.js');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
//check if plugin.json exists
|
||||
if (!fs.existsSync('./plugin.json')) {
|
||||
//change dir to parent
|
||||
process.chdir('../');
|
||||
if (!fs.existsSync('./plugin.json')) {
|
||||
error('Failed! plugin.json not found');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
//load plugin.json
|
||||
const plugin = JSON.parse(fs.readFileSync('./plugin.json', 'utf8'));
|
||||
const name = plugin?.name;
|
||||
if (!name || name === '') {
|
||||
error('Failed! Please set plugin name in plugin.json');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
//dev directory
|
||||
/**
|
||||
* 2. The dev directory, which contains the compiled plugin code
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. The target directory to make symbolic link to dev directory
|
||||
*/
|
||||
const name = getThisPluginName();
|
||||
if (name === null) {
|
||||
process.exit(1);
|
||||
}
|
||||
const targetPath = `${targetDir}/${name}`;
|
||||
//如果已经存在,就退出
|
||||
if (fs.existsSync(targetPath)) {
|
||||
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(devDir, targetPath, 'junction');
|
||||
log(`Done! Created symlink ${targetPath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 4. Make symbolic link
|
||||
*/
|
||||
makeSymbolicLink(devDir, targetPath);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue