Skip to content

Commit

Permalink
feat: ✨ 添加自动化脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
mdzz2048 committed Aug 19, 2023
1 parent 62efdb2 commit 690da3e
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 0 deletions.
214 changes: 214 additions & 0 deletions script/make-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// import fs from 'fs';
const fs = require('fs');
// import http from 'node:http';
const http = require('http');
// import readline from 'node:readline';
const readline = require('readline');


//************************************ Write you dir here ************************************

//Please write the "workspace/conf/appearance/themes" directory here
//请在这里填写你的 "workspace/conf/appearance/themes" 目录
let targetDir = 'H:\\临时文件夹\\SiYuanDevSpace\\conf\\appearance\\themes';
//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(`HTTP-Error: ${response.status}`);
return null;
}
} catch (e)
{
error("Error:", e);
error("Please 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(`[${i}] ${workspaces[i].path}`);
}

if (count == 1)
{
return `${workspaces[0].path}/conf/appearance/themes`;
} else
{
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let index = await new Promise((resolve, reject) =>
{
rl.question(`Please select a workspace[0-${count - 1}]: `, (answer) =>
{
resolve(answer);
});
});
rl.close();
return `${workspaces[index].path}/conf/appearance/themes`;
}
}

async function main()
{
if (targetDir === '')
{
log('"targetDir" is empty, try to get SiYuan directory automatically....')
let res = await getSiYuanDir();

if (!res)
{
log('Failed! You can set the theme directory in scripts/make_dev_link.js and try again');
process.exit(1);
}

targetDir = await chooseTarget(res);
log(`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-link.js`);
process.exit(1);
}


//check if plugin.json exists
if (!fs.existsSync('./theme.json'))
{
//change dir to parent
process.chdir('../');
if (!fs.existsSync('./theme.json'))
{
error('Failed! plugin.json not found');
process.exit(1);
}
}

//load theme.json
const theme = JSON.parse(fs.readFileSync('./theme.json', 'utf8'));
const name = theme?.name;
if (!name || name === '')
{
error('Failed! Please set plugin name in plugin.json');
process.exit(1);
}

//dev directory
const devDir = process.cwd();
//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))
{
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}`);
}

}

main();
7 changes: 7 additions & 0 deletions script/make-zip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# 搜寻 png md css json 文件
find . -type f \( -name "*.png" -o -name "*.md" -o -name "*.css" -o -name "*.json" \) -print > file-list.txt

# 打包所有文件为 package.zip
zip package.zip -@ < file-list.txt

0 comments on commit 690da3e

Please sign in to comment.