feat: recreate bob's config before server start

https://github.com/OokTech/TW5-Bob/issues/139
This commit is contained in:
tiddlygit-test 2020-07-24 01:08:34 +08:00
parent 2d79953efa
commit d3616aa8ab
2 changed files with 26 additions and 11 deletions

1
.gitignore vendored
View file

@ -57,3 +57,4 @@ snapcraft_login
template/wiki/tiddlers/$__StoryList.tid template/wiki/tiddlers/$__StoryList.tid
template/wiki/public-dist/ template/wiki/public-dist/
template/wiki/output/ template/wiki/output/
template/wiki/settings/settings.json

View file

@ -3,26 +3,40 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const $tw = require('tiddlywiki').TiddlyWiki(); const $tw = require('tiddlywiki').TiddlyWiki();
function startNodeJSWiki() { async function startNodeJSWiki() {
const { homePath, userName, tiddlyWikiPort = 5112 } = workerData; const { homePath, userName, tiddlyWikiPort = 5112 } = workerData;
const bobServerConfigFolderPath = path.join(homePath, 'settings');
const bobServerConfigPath = path.join(bobServerConfigFolderPath, 'settings.json');
try { try {
const bobServerConfigPath = path.join(homePath, 'settings', 'settings.json'); if (
if (fs.existsSync(bobServerConfigPath)) { await fs.promises
fs.unlinkSync(bobServerConfigPath); .access(bobServerConfigPath, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
) {
await fs.promises.unlink(bobServerConfigPath);
await fs.promises.unlink(bobServerConfigFolderPath);
} }
await fs.promises.mkdir(bobServerConfigFolderPath);
} catch (error) {
parentPort.postMessage(`Configuring TW-Bob config folder failed with error ${error.message} ${error.stack}`);
}
try {
const bobServerConfig = { const bobServerConfig = {
serverName: 'TiddlyGit-Desktop', serverName: 'TiddlyGit-Desktop',
persistentUsernames: 'yes', persistentUsernames: 'yes',
heartbeat: { heartbeat: {
interval: 200, interval: 200,
timeout: 1000,
}, },
enableBobSaver: 'no', enableBobSaver: 'no',
enableSaver: 'no',
saver: { saver: {
disable: 'yes', disable: 'yes',
}, },
pluginsPath: `./${homePath}/plugins`, pluginsPath: './plugins',
themesPath: `./${homePath}/themes`, themesPath: './themes',
editionsPath: `./${homePath}/`, editionsPath: './',
wikiPathBase: homePath, wikiPathBase: homePath,
'ws-server': { 'ws-server': {
port: tiddlyWikiPort, port: tiddlyWikiPort,
@ -31,9 +45,9 @@ function startNodeJSWiki() {
rootTiddler: '$:/core/save/lazy-images', rootTiddler: '$:/core/save/lazy-images',
}, },
}; };
fs.writeFileSync(bobServerConfigPath, JSON.stringify(bobServerConfig, undefined, ' ')); await fs.promises.writeFile(bobServerConfigPath, JSON.stringify(bobServerConfig, undefined, ' '));
} catch (error) { } catch (error) {
parentPort.postMessage(`Configuring TW-Bob failed with error ${error.message} ${error.trace}`); parentPort.postMessage(`Configuring TW-Bob failed with error ${error.message} ${error.stack}`);
} }
try { try {
process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins'); process.env.TIDDLYWIKI_PLUGIN_PATH = path.resolve(homePath, 'plugins');
@ -42,11 +56,11 @@ function startNodeJSWiki() {
$tw.boot.argv = [ $tw.boot.argv = [
'+plugins/OokTech/Bob', '+plugins/OokTech/Bob',
homePath, homePath,
'--wsserver', // port config in Meme-of-LinOnetwo/settings/settings.json '--wsserver',
]; ];
$tw.boot.boot(() => parentPort.postMessage(`Tiddlywiki booted at http://localhost:${tiddlyWikiPort}`)); $tw.boot.boot(() => parentPort.postMessage(`Tiddlywiki booted at http://localhost:${tiddlyWikiPort}`));
} catch (error) { } catch (error) {
parentPort.postMessage(`Tiddlywiki booted failed with error ${error.message} ${error.trace}`); parentPort.postMessage(`Tiddlywiki booted failed with error ${error.message} ${error.stack}`);
} }
} }
module.exports = startNodeJSWiki; module.exports = startNodeJSWiki;