Skip to content

Commit f79f762

Browse files
committed
🔧 ci: make-link script
1 parent 4603e05 commit f79f762

5 files changed

Lines changed: 272 additions & 319 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "GPL-3.0-only",
1010
"scripts": {
1111
"make-link": "node --no-warnings ./scripts/make_dev_link.js",
12+
"make-link-win": "powershell.exe -NoProfile -ExecutionPolicy Bypass -File ./scripts/elevate.ps1 -scriptPath ./scripts/make_dev_link.js",
1213
"dev": "vite build --watch",
1314
"dev-srcmap": "vite build --watch --sourcemap=inline",
1415
"build": "vite build",

‎scripts/elevate.ps1‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2024 by frostime. All Rights Reserved.
2+
# @Author : frostime
3+
# @Date : 2024-09-06 19:15:53
4+
# @FilePath : /scripts/elevate.ps1
5+
# @LastEditTime : 2024-09-06 19:39:13
6+
# @Description : Force to elevate the script to admin privilege.
7+
8+
param (
9+
[string]$scriptPath
10+
)
11+
12+
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
13+
$projectDir = Split-Path -Parent $scriptDir
14+
15+
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
16+
$args = "-NoProfile -ExecutionPolicy Bypass -File `"" + $MyInvocation.MyCommand.Path + "`" -scriptPath `"" + $scriptPath + "`""
17+
Start-Process powershell.exe -Verb RunAs -ArgumentList $args -WorkingDirectory $projectDir
18+
exit
19+
}
20+
21+
Set-Location -Path $projectDir
22+
& node $scriptPath
23+
24+
pause

‎scripts/make_dev_link.js‎

Lines changed: 34 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,29 @@
1+
/*
2+
* Copyright (c) 2024 by frostime. All Rights Reserved.
3+
* @Author : frostime
4+
* @Date : 2023-07-15 15:31:31
5+
* @FilePath : /scripts/make_dev_link.js
6+
* @LastEditTime : 2024-09-06 19:54:22
7+
* @Description :
8+
*/
9+
// make_dev_link.js
110
import fs from 'fs';
2-
import http from 'node:http';
3-
import readline from 'node:readline';
11+
import { log, error, getSiYuanDir, chooseTarget, getThisPluginName, makeSymbolicLink } from './utils.js';
412

5-
6-
//************************************ Write you dir here ************************************
7-
8-
//Please write the "workspace/data/plugins" directory here
9-
//请在这里填写你的 "workspace/data/plugins" 目录
1013
let targetDir = '';
11-
//Like this
12-
// let targetDir = `H:\\SiYuanDevSpace\\data\\plugins`;
13-
//********************************************************************************************
14-
15-
const log = (info) => console.log(`\x1B[36m%s\x1B[0m`, info);
16-
const error = (info) => console.log(`\x1B[31m%s\x1B[0m`, info);
17-
18-
let POST_HEADER = {
19-
// "Authorization": `Token ${token}`,
20-
"Content-Type": "application/json",
21-
}
22-
23-
async function myfetch(url, options) {
24-
//使用 http 模块,从而兼容那些不支持 fetch 的 nodejs 版本
25-
return new Promise((resolve, reject) => {
26-
let req = http.request(url, options, (res) => {
27-
let data = '';
28-
res.on('data', (chunk) => {
29-
data += chunk;
30-
});
31-
res.on('end', () => {
32-
resolve({
33-
ok: true,
34-
status: res.statusCode,
35-
json: () => JSON.parse(data)
36-
});
37-
});
38-
});
39-
req.on('error', (e) => {
40-
reject(e);
41-
});
42-
req.end();
43-
});
44-
}
45-
46-
async function getSiYuanDir() {
47-
let url = 'http://127.0.0.1:6806/api/system/getWorkspaces';
48-
let conf = {};
49-
try {
50-
let response = await myfetch(url, {
51-
method: 'POST',
52-
headers: POST_HEADER
53-
});
54-
if (response.ok) {
55-
conf = await response.json();
56-
} else {
57-
error(`\tHTTP-Error: ${response.status}`);
58-
return null;
59-
}
60-
} catch (e) {
61-
error(`\tError: ${e}`);
62-
error("\tPlease make sure SiYuan is running!!!");
63-
return null;
64-
}
65-
return conf.data;
66-
}
67-
68-
async function chooseTarget(workspaces) {
69-
let count = workspaces.length;
70-
log(`>>> Got ${count} SiYuan ${count > 1 ? 'workspaces' : 'workspace'}`)
71-
for (let i = 0; i < workspaces.length; i++) {
72-
log(`\t[${i}] ${workspaces[i].path}`);
73-
}
74-
75-
if (count == 1) {
76-
return `${workspaces[0].path}/data/plugins`;
77-
} else {
78-
const rl = readline.createInterface({
79-
input: process.stdin,
80-
output: process.stdout
81-
});
82-
let index = await new Promise((resolve, reject) => {
83-
rl.question(`\tPlease select a workspace[0-${count-1}]: `, (answer) => {
84-
resolve(answer);
85-
});
86-
});
87-
rl.close();
88-
return `${workspaces[index].path}/data/plugins`;
89-
}
90-
}
91-
92-
log('>>> Try to visit constant "targetDir" in make_dev_link.js...')
9314

15+
/**
16+
* 1. Get the parent directory to install the plugin
17+
*/
18+
log('>>> Try to visit constant "targetDir" in make_dev_link.js...');
9419
if (targetDir === '') {
95-
log('>>> Constant "targetDir" is empty, try to get SiYuan directory automatically....')
20+
log('>>> Constant "targetDir" is empty, try to get SiYuan directory automatically....');
9621
let res = await getSiYuanDir();
97-
98-
if (res === null || res === undefined || res.length === 0) {
99-
log('>>> Can not get SiYuan directory automatically, try to visit environment variable "SIYUAN_PLUGIN_DIR"....');
10022

101-
// console.log(process.env)
23+
if (!res || res.length === 0) {
24+
log('>>> Can not get SiYuan directory automatically, try to visit environment variable "SIYUAN_PLUGIN_DIR"....');
10225
let env = process.env?.SIYUAN_PLUGIN_DIR;
103-
if (env !== undefined && env !== null && env !== '') {
26+
if (env) {
10427
targetDir = env;
10528
log(`\tGot target directory from environment variable "SIYUAN_PLUGIN_DIR": ${targetDir}`);
10629
} else {
@@ -111,76 +34,33 @@ if (targetDir === '') {
11134
targetDir = await chooseTarget(res);
11235
}
11336

114-
11537
log(`>>> Successfully got target directory: ${targetDir}`);
11638
}
117-
118-
//Check
11939
if (!fs.existsSync(targetDir)) {
120-
error(`Failed! plugin directory not exists: "${targetDir}"`);
121-
error(`Please set the plugin directory in scripts/make_dev_link.js`);
40+
error(`Failed! Plugin directory not exists: "${targetDir}"`);
41+
error('Please set the plugin directory in scripts/make_dev_link.js');
12242
process.exit(1);
12343
}
12444

125-
126-
//check if plugin.json exists
127-
if (!fs.existsSync('./plugin.json')) {
128-
//change dir to parent
129-
process.chdir('../');
130-
if (!fs.existsSync('./plugin.json')) {
131-
error('Failed! plugin.json not found');
132-
process.exit(1);
133-
}
134-
}
135-
136-
//load plugin.json
137-
const plugin = JSON.parse(fs.readFileSync('./plugin.json', 'utf8'));
138-
const name = plugin?.name;
139-
if (!name || name === '') {
140-
error('Failed! Please set plugin name in plugin.json');
141-
process.exit(1);
142-
}
143-
144-
//dev directory
45+
/**
46+
* 2. The dev directory, which contains the compiled plugin code
47+
*/
14548
const devDir = `${process.cwd()}/dev`;
146-
//mkdir if not exists
14749
if (!fs.existsSync(devDir)) {
14850
fs.mkdirSync(devDir);
14951
}
15052

151-
function cmpPath(path1, path2) {
152-
path1 = path1.replace(/\\/g, '/');
153-
path2 = path2.replace(/\\/g, '/');
154-
// sepertor at tail
155-
if (path1[path1.length - 1] !== '/') {
156-
path1 += '/';
157-
}
158-
if (path2[path2.length - 1] !== '/') {
159-
path2 += '/';
160-
}
161-
return path1 === path2;
162-
}
163-
164-
const targetPath = `${targetDir}/${name}`;
165-
//如果已经存在,就退出
166-
if (fs.existsSync(targetPath)) {
167-
let isSymbol = fs.lstatSync(targetPath).isSymbolicLink();
168-
169-
if (isSymbol) {
170-
let srcPath = fs.readlinkSync(targetPath);
171-
172-
if (cmpPath(srcPath, devDir)) {
173-
log(`Good! ${targetPath} is already linked to ${devDir}`);
174-
} else {
175-
error(`Error! Already exists symbolic link ${targetPath}\nBut it links to ${srcPath}`);
176-
}
177-
} else {
178-
error(`Failed! ${targetPath} already exists and is not a symbolic link`);
179-
}
18053

181-
} else {
182-
//创建软链接
183-
fs.symlinkSync(devDir, targetPath, 'junction');
184-
log(`Done! Created symlink ${targetPath}`);
54+
/**
55+
* 3. The target directory to make symbolic link to dev directory
56+
*/
57+
const name = getThisPluginName();
58+
if (name === null) {
59+
process.exit(1);
18560
}
61+
const targetPath = `${targetDir}/${name}`;
18662

63+
/**
64+
* 4. Make symbolic link
65+
*/
66+
makeSymbolicLink(devDir, targetPath);

0 commit comments

Comments
 (0)