diff --git a/package.json b/package.json index 74a5376..dd3c6a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plugin-sample-vite-solidjs", - "version": "0.3.5", + "version": "0.3.6", "type": "module", "description": "This is a sample plugin based on vite and solidjs for Siyuan (https://b3log.org/siyuan)", "repository": "", @@ -8,9 +8,9 @@ "author": "frostime", "license": "GPL-3.0-only", "scripts": { - "make-link": "node --no-warnings ./scripts/make_dev_link.js", - "make-link-win": "powershell.exe -NoProfile -ExecutionPolicy Bypass -File ./scripts/elevate.ps1 -scriptPath ./scripts/make_dev_link.js", - "update-version": "node --no-warnings ./scripts/update_version.js", + "make-link": "npx make-link", + "make-link-win": "npx make-lin-win", + "update-version": "npx update-version", "dev": "vite build --watch", "build": "vite build", "make-install": "vite build && node --no-warnings ./scripts/make_install.js" @@ -23,7 +23,8 @@ "minimist": "^1.2.8", "rollup-plugin-livereload": "^2.0.5", "sass": "^1.72.0", - "siyuan": "0.9.9", + "siyuan": "1.0.3", + "siyuan-plugin-cli": "^2.3.1", "solid-js": "^1.8.17", "solid-styled-jsx": "^0.27.1", "ts-node": "^10.9.2", @@ -33,4 +34,4 @@ "vite-plugin-static-copy": "^1.0.5", "vite-plugin-zip-pack": "^1.2.2" } -} +} \ No newline at end of file diff --git a/plugin.json b/plugin.json index 0b8d8a8..9677772 100644 --- a/plugin.json +++ b/plugin.json @@ -2,7 +2,7 @@ "name": "plugin-sample-vite-solidjs", "author": "frostime", "url": "https://github.com/frostime/plugin-sample-vite-solidjs", - "version": "0.3.5", + "version": "0.3.6", "minAppVersion": "3.0.12", "backends": [ "all" @@ -28,6 +28,9 @@ ] }, "keywords": [ - "plugin", "sample", "插件样例", "solidjs" + "plugin", + "sample", + "插件样例", + "solidjs" ] -} +} \ No newline at end of file diff --git a/scripts/.gitignore b/scripts/.gitignore deleted file mode 100644 index 82fa0dc..0000000 --- a/scripts/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.venv -build -dist -*.exe -*.spec diff --git a/scripts/elevate.ps1 b/scripts/elevate.ps1 deleted file mode 100644 index 151b8ba..0000000 --- a/scripts/elevate.ps1 +++ /dev/null @@ -1,24 +0,0 @@ -# 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 diff --git a/scripts/make_dev_link.js b/scripts/make_dev_link.js deleted file mode 100644 index 09a33aa..0000000 --- a/scripts/make_dev_link.js +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 19:54:22 - * @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); diff --git a/scripts/make_install.js b/scripts/make_install.js deleted file mode 100644 index cb2a4ac..0000000 --- a/scripts/make_install.js +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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); diff --git a/scripts/update_version.js b/scripts/update_version.js deleted file mode 100644 index 775c98a..0000000 --- a/scripts/update_version.js +++ /dev/null @@ -1,141 +0,0 @@ -// 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); - } -})(); diff --git a/scripts/utils.js b/scripts/utils.js deleted file mode 100644 index 210b6b1..0000000 --- a/scripts/utils.js +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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} - */ -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}`); - } -}