generated from mirrors/plugin-sample-vite-svelte
Initial commit
This commit is contained in:
commit
04f54e248a
42 changed files with 4538 additions and 0 deletions
5
scripts/.gitignore
vendored
Normal file
5
scripts/.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
.venv
|
||||
build
|
||||
dist
|
||||
*.exe
|
||||
*.spec
|
24
scripts/elevate.ps1
Normal file
24
scripts/elevate.ps1
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Copyright (c) 2024 by frostime. All Rights Reserved.
|
||||
# @Author : frostime
|
||||
# @Date : 2024-09-06 19:15:53
|
||||
# @FilePath : /scripts/elevate.ps1
|
||||
# @LastEditTime : 2024-09-06 19:39:13
|
||||
# @Description : Force to elevate the script to admin privilege.
|
||||
|
||||
param (
|
||||
[string]$scriptPath
|
||||
)
|
||||
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
$projectDir = Split-Path -Parent $scriptDir
|
||||
|
||||
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
|
||||
$args = "-NoProfile -ExecutionPolicy Bypass -File `"" + $MyInvocation.MyCommand.Path + "`" -scriptPath `"" + $scriptPath + "`""
|
||||
Start-Process powershell.exe -Verb RunAs -ArgumentList $args -WorkingDirectory $projectDir
|
||||
exit
|
||||
}
|
||||
|
||||
Set-Location -Path $projectDir
|
||||
& node $scriptPath
|
||||
|
||||
pause
|
66
scripts/make_dev_link.js
Normal file
66
scripts/make_dev_link.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* 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 { log, error, getSiYuanDir, chooseTarget, getThisPluginName, makeSymbolicLink } from './utils.js';
|
||||
|
||||
let targetDir = '';
|
||||
|
||||
/**
|
||||
* 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....');
|
||||
let res = await getSiYuanDir();
|
||||
|
||||
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) {
|
||||
targetDir = env;
|
||||
log(`\tGot target directory from environment variable "SIYUAN_PLUGIN_DIR": ${targetDir}`);
|
||||
} else {
|
||||
error('\tCan not get SiYuan directory from environment variable "SIYUAN_PLUGIN_DIR", failed!');
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
targetDir = await chooseTarget(res);
|
||||
}
|
||||
|
||||
log(`>>> Successfully got target directory: ${targetDir}`);
|
||||
}
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
error(`Failed! Plugin directory not exists: "${targetDir}"`);
|
||||
error('Please set the plugin directory in scripts/make_dev_link.js');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. The dev directory, which contains the compiled plugin code
|
||||
*/
|
||||
const devDir = `${process.cwd()}/dev`;
|
||||
if (!fs.existsSync(devDir)) {
|
||||
fs.mkdirSync(devDir);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 3. The target directory to make symbolic link to dev directory
|
||||
*/
|
||||
const name = getThisPluginName();
|
||||
if (name === null) {
|
||||
process.exit(1);
|
||||
}
|
||||
const targetPath = `${targetDir}/${name}`;
|
||||
|
||||
/**
|
||||
* 4. Make symbolic link
|
||||
*/
|
||||
makeSymbolicLink(devDir, targetPath);
|
57
scripts/make_install.js
Normal file
57
scripts/make_install.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2024 by frostime. All Rights Reserved.
|
||||
* @Author : frostime
|
||||
* @Date : 2024-03-28 20:03:59
|
||||
* @FilePath : /scripts/make_install.js
|
||||
* @LastEditTime : 2024-09-06 18:08:19
|
||||
* @Description :
|
||||
*/
|
||||
// make_install.js
|
||||
import fs from 'fs';
|
||||
import { log, error, getSiYuanDir, chooseTarget, copyDirectory, getThisPluginName } from './utils.js';
|
||||
|
||||
let targetDir = '';
|
||||
|
||||
/**
|
||||
* 1. Get the parent directory to install the plugin
|
||||
*/
|
||||
log('>>> Try to visit constant "targetDir" in make_install.js...');
|
||||
if (targetDir === '') {
|
||||
log('>>> Constant "targetDir" is empty, try to get SiYuan directory automatically....');
|
||||
let res = await getSiYuanDir();
|
||||
|
||||
if (res === null || res === undefined || res.length === 0) {
|
||||
error('>>> Can not get SiYuan directory automatically');
|
||||
process.exit(1);
|
||||
} else {
|
||||
targetDir = await chooseTarget(res);
|
||||
}
|
||||
log(`>>> Successfully got target directory: ${targetDir}`);
|
||||
}
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
error(`Failed! Plugin directory not exists: "${targetDir}"`);
|
||||
error('Please set the plugin directory in scripts/make_install.js');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 2. The dist directory, which contains the compiled plugin code
|
||||
*/
|
||||
const distDir = `${process.cwd()}/dist`;
|
||||
if (!fs.existsSync(distDir)) {
|
||||
fs.mkdirSync(distDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 3. The target directory to install the plugin
|
||||
*/
|
||||
const name = getThisPluginName();
|
||||
if (name === null) {
|
||||
process.exit(1);
|
||||
}
|
||||
const targetPath = `${targetDir}/${name}`;
|
||||
|
||||
/**
|
||||
* 4. Copy the compiled plugin code to the target directory
|
||||
*/
|
||||
copyDirectory(distDir, targetPath);
|
141
scripts/update_version.js
Normal file
141
scripts/update_version.js
Normal file
|
@ -0,0 +1,141 @@
|
|||
// const fs = require('fs');
|
||||
// const path = require('path');
|
||||
// const readline = require('readline');
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import readline from 'node:readline';
|
||||
|
||||
// Utility to read JSON file
|
||||
function readJsonFile(filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||
if (err) return reject(err);
|
||||
try {
|
||||
const jsonData = JSON.parse(data);
|
||||
resolve(jsonData);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Utility to write JSON file
|
||||
function writeJsonFile(filePath, jsonData) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(filePath, JSON.stringify(jsonData, null, 2), 'utf8', (err) => {
|
||||
if (err) return reject(err);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Utility to prompt the user for input
|
||||
function promptUser(query) {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
return new Promise((resolve) => rl.question(query, (answer) => {
|
||||
rl.close();
|
||||
resolve(answer);
|
||||
}));
|
||||
}
|
||||
|
||||
// Function to parse the version string
|
||||
function parseVersion(version) {
|
||||
const [major, minor, patch] = version.split('.').map(Number);
|
||||
return { major, minor, patch };
|
||||
}
|
||||
|
||||
// Function to auto-increment version parts
|
||||
function incrementVersion(version, type) {
|
||||
let { major, minor, patch } = parseVersion(version);
|
||||
|
||||
switch (type) {
|
||||
case 'major':
|
||||
major++;
|
||||
minor = 0;
|
||||
patch = 0;
|
||||
break;
|
||||
case 'minor':
|
||||
minor++;
|
||||
patch = 0;
|
||||
break;
|
||||
case 'patch':
|
||||
patch++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return `${major}.${minor}.${patch}`;
|
||||
}
|
||||
|
||||
// Main script
|
||||
(async function () {
|
||||
try {
|
||||
const pluginJsonPath = path.join(process.cwd(), 'plugin.json');
|
||||
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
||||
|
||||
// Read both JSON files
|
||||
const pluginData = await readJsonFile(pluginJsonPath);
|
||||
const packageData = await readJsonFile(packageJsonPath);
|
||||
|
||||
// Get the current version from both files (assuming both have the same version)
|
||||
const currentVersion = pluginData.version || packageData.version;
|
||||
console.log(`\n🌟 Current version: \x1b[36m${currentVersion}\x1b[0m\n`);
|
||||
|
||||
// Calculate potential new versions for auto-update
|
||||
const newPatchVersion = incrementVersion(currentVersion, 'patch');
|
||||
const newMinorVersion = incrementVersion(currentVersion, 'minor');
|
||||
const newMajorVersion = incrementVersion(currentVersion, 'major');
|
||||
|
||||
// Prompt the user with formatted options
|
||||
console.log('🔄 How would you like to update the version?\n');
|
||||
console.log(` 1️⃣ Auto update \x1b[33mpatch\x1b[0m version (new version: \x1b[32m${newPatchVersion}\x1b[0m)`);
|
||||
console.log(` 2️⃣ Auto update \x1b[33mminor\x1b[0m version (new version: \x1b[32m${newMinorVersion}\x1b[0m)`);
|
||||
console.log(` 3️⃣ Auto update \x1b[33mmajor\x1b[0m version (new version: \x1b[32m${newMajorVersion}\x1b[0m)`);
|
||||
console.log(` 4️⃣ Input version \x1b[33mmanually\x1b[0m`);
|
||||
// Press 0 to skip version update
|
||||
console.log(' 0️⃣ Quit without updating\n');
|
||||
|
||||
const updateChoice = await promptUser('👉 Please choose (1/2/3/4): ');
|
||||
|
||||
let newVersion;
|
||||
|
||||
switch (updateChoice.trim()) {
|
||||
case '1':
|
||||
newVersion = newPatchVersion;
|
||||
break;
|
||||
case '2':
|
||||
newVersion = newMinorVersion;
|
||||
break;
|
||||
case '3':
|
||||
newVersion = newMajorVersion;
|
||||
break;
|
||||
case '4':
|
||||
newVersion = await promptUser('✍️ Please enter the new version (in a.b.c format): ');
|
||||
break;
|
||||
case '0':
|
||||
console.log('\n🛑 Skipping version update.');
|
||||
return;
|
||||
default:
|
||||
console.log('\n❌ Invalid option, no version update.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the version in both plugin.json and package.json
|
||||
pluginData.version = newVersion;
|
||||
packageData.version = newVersion;
|
||||
|
||||
// Write the updated JSON back to files
|
||||
await writeJsonFile(pluginJsonPath, pluginData);
|
||||
await writeJsonFile(packageJsonPath, packageData);
|
||||
|
||||
console.log(`\n✅ Version successfully updated to: \x1b[32m${newVersion}\x1b[0m\n`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error:', error);
|
||||
}
|
||||
})();
|
182
scripts/utils.js
Normal file
182
scripts/utils.js
Normal file
|
@ -0,0 +1,182 @@
|
|||
/*
|
||||
* Copyright (c) 2024 by frostime. All Rights Reserved.
|
||||
* @Author : frostime
|
||||
* @Date : 2024-09-06 17:42:57
|
||||
* @FilePath : /scripts/utils.js
|
||||
* @LastEditTime : 2024-09-06 19:23:12
|
||||
* @Description :
|
||||
*/
|
||||
// common.js
|
||||
import fs from 'fs';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import readline from 'node:readline';
|
||||
|
||||
// Logging functions
|
||||
export const log = (info) => console.log(`\x1B[36m%s\x1B[0m`, info);
|
||||
export const error = (info) => console.log(`\x1B[31m%s\x1B[0m`, info);
|
||||
|
||||
// HTTP POST headers
|
||||
export const POST_HEADER = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
// Fetch function compatible with older Node.js versions
|
||||
export async function myfetch(url, options) {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch SiYuan workspaces from port 6806
|
||||
* @returns {Promise<Object | null>}
|
||||
*/
|
||||
export 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; // 保持原始返回值
|
||||
}
|
||||
|
||||
/**
|
||||
* Choose target workspace
|
||||
* @param {{path: string}[]} workspaces
|
||||
* @returns {string} The path of the selected workspace
|
||||
*/
|
||||
export async function chooseTarget(workspaces) {
|
||||
let count = workspaces.length;
|
||||
log(`>>> Got ${count} SiYuan ${count > 1 ? 'workspaces' : 'workspace'}`);
|
||||
workspaces.forEach((workspace, i) => {
|
||||
log(`\t[${i}] ${workspace.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) => {
|
||||
rl.question(`\tPlease select a workspace[0-${count - 1}]: `, (answer) => {
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
rl.close();
|
||||
return `${workspaces[index].path}/data/plugins`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two paths are the same
|
||||
* @param {string} path1
|
||||
* @param {string} path2
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function cmpPath(path1, path2) {
|
||||
path1 = path1.replace(/\\/g, '/');
|
||||
path2 = path2.replace(/\\/g, '/');
|
||||
if (path1[path1.length - 1] !== '/') {
|
||||
path1 += '/';
|
||||
}
|
||||
if (path2[path2.length - 1] !== '/') {
|
||||
path2 += '/';
|
||||
}
|
||||
return path1 === path2;
|
||||
}
|
||||
|
||||
export function getThisPluginName() {
|
||||
if (!fs.existsSync('./plugin.json')) {
|
||||
process.chdir('../');
|
||||
if (!fs.existsSync('./plugin.json')) {
|
||||
error('Failed! plugin.json not found');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const plugin = JSON.parse(fs.readFileSync('./plugin.json', 'utf8'));
|
||||
const name = plugin?.name;
|
||||
if (!name) {
|
||||
error('Failed! Please set plugin name in plugin.json');
|
||||
return null;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
export function copyDirectory(srcDir, dstDir) {
|
||||
if (!fs.existsSync(dstDir)) {
|
||||
fs.mkdirSync(dstDir);
|
||||
log(`Created directory ${dstDir}`);
|
||||
}
|
||||
|
||||
fs.readdirSync(srcDir, { withFileTypes: true }).forEach((file) => {
|
||||
const src = path.join(srcDir, file.name);
|
||||
const dst = path.join(dstDir, file.name);
|
||||
|
||||
if (file.isDirectory()) {
|
||||
copyDirectory(src, dst);
|
||||
} else {
|
||||
fs.copyFileSync(src, dst);
|
||||
log(`Copied file: ${src} --> ${dst}`);
|
||||
}
|
||||
});
|
||||
log(`All files copied!`);
|
||||
}
|
||||
|
||||
|
||||
export function makeSymbolicLink(srcPath, targetPath) {
|
||||
if (!fs.existsSync(targetPath)) {
|
||||
// fs.symlinkSync(srcPath, targetPath, 'junction');
|
||||
//Go 1.23 no longer supports junctions as symlinks
|
||||
//Please refer to https://github.com/siyuan-note/siyuan/issues/12399
|
||||
fs.symlinkSync(srcPath, targetPath, 'dir');
|
||||
log(`Done! Created symlink ${targetPath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
//Check the existed target path
|
||||
let isSymbol = fs.lstatSync(targetPath).isSymbolicLink();
|
||||
if (!isSymbol) {
|
||||
error(`Failed! ${targetPath} already exists and is not a symbolic link`);
|
||||
return;
|
||||
}
|
||||
let existedPath = fs.readlinkSync(targetPath);
|
||||
if (cmpPath(existedPath, srcPath)) {
|
||||
log(`Good! ${targetPath} is already linked to ${srcPath}`);
|
||||
} else {
|
||||
error(`Error! Already exists symbolic link ${targetPath}\nBut it links to ${existedPath}`);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue