Tips & Tricks to use TiddlyWiki on node.js

Continuing the discussion from What is the advantage of Node js. over html file?:

Here’s a summary of several tips and tricks you can use to improve your experience with tiddlywiki on node.js. This post is a wiki, feel free to contribute to it!

1. (windows) Using a .bat file to automate the creation/launch of wikis

@ECHO off
IF NOT "%~1"=="" (
  ::if using shortcut
  set name=%~1
) ELSE (
  ::ask for the name
  set /p name=Name of the wiki:
)
::remove quotations
set name=%name:"=%
ECHO %name% will open shortly..
::search free port
set startPort=80
:SEARCHPORT
netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL
IF "%ERRORLEVEL%" equ "0" (
  set /a startPort +=1
  GOTO :SEARCHPORT
) ELSE (
  set freePort=%startPort%
  GOTO :FOUNDPORT
)
:FOUNDPORT
::launch tiddlywiki server
CALL tiddlywiki "%name%" --init server
::open the url in the default browser
START http://localhost:%freePort%
::create shortcuts files if missing 
IF NOT EXIST "%name%/start.bat" ECHO for %%%%d in ^(^.^) do cd.. ^& start.bat "%%%%~nxd" >"%name%/start.bat"
IF NOT EXIST "%name%/backup.bat" ECHO tiddlywiki --build index >"%name%/backup.bat"
::set title of the wiki if blank
IF NOT EXIST "%name%/tiddlers/$__SiteTitle.tid" (
  ECHO {"title":"$:/SiteTitle","text":"%name%"} >"%name%/title.json"
  CALL tiddlywiki "%name%" --load "%name%/title.json"
)
::listen to a free port
CALL tiddlywiki "%name%" --listen port=%freePort%

Download : https://github.com/Telumire/StartTWNodeJS/releases/download/v0.0.1/start.bat

To use :

  • Download the .bat or create a text file, copy/paste the above code, then save and change the extension to .bat.
  • If node.js is installed, double-clicking that file in a folder will open a command prompt asking for the name of the wiki to be created, then a folder will be created with that name and the wiki will launch in the default internet browser.
  • A batch file to start the wiki and another to create a single file backup of the wiki will be created inside the wiki’s folder, for later use.
  • You can then create a shortcut of this batch file and place it into “C:\ProgramData\Microsoft\Windows\Start Menu” to be able to pin the shortcut to the start menu, or put it anywhere else you like. It’s also possible to customize the icon of the shortcut, create a keyboard shortcut and schedule your wiki to be opened automatically on startup.

2. (windows) Compress your folder to save space

Thanks to @pmario for this tip !

Note : The startup time may be slightly slower with compression enabled

  • Left click on the folder, then press alt+enter (or click on the properties button in the Home tab of the file explorer, or use the right-click menu).

Screenshot of the properties button in the file explorer

  • In the properties window, in the General tab, click on the “Advanced…” button and check the “Compress contents to save disk space” option

In a test with 10k tiddlers containing each 2783 characters, the results were as follow :

  • Size: 28 964 499 bytes
  • Size on disk: 40 964 096 bytes
  • Space saved : (40964096 - 28964499)/40964096 ≈ 30%

3. Using a .cmd file to upgrade a single file wiki with node.js

Tip shared by @amreus here : Update a single-file TiddlyWiki from the command line

Batch file code :

copy "%1" "%1"-%RANDOM%
tiddlywiki --load "%1" --output . --render  "$:/core/save/all" "%1" "text/plain"

Using the batch file (named twup.cmd) :

twup mywiki.html

:warning: backup your wiki before performing this operation !


EDIT: upgraded the batch file to automate the creation/launch of multiple wikis and set the title of the wiki

3 Likes

If you would like to hide the command windows when batch file is running, you can use the vbs script

CreateObject("Wscript.Shell").Run "cmd /c tiddlywiki tiddlywiki --listen port=8080  --load tiddlywiki.bib ", 0, false

Similar as batch file,

  • create a text file and save the text with vbs extension along with tiddlywiki folder
* tiddlywiki
  * tiddlywiki.info
  * tiddlers
  * plugins
* start-tiddlywiki.vbs
  • Create a shortcut into in the Startup menu

  • tiddlywiki will run at background when Windows restarts every time.

  • Open browser to access through http://127.0.0.1:8080

1 Like

I use a PowerShell script to start tiddlywiki on a random port (seeks a free port)

<#
.SYNOPSIS
Generates a random port number in a (hopefully) open range.
.LINK
https://www.cymru.com/jtk/misc/ephemeralports.html
#>
Function Get-RandomPort
{
    return Get-Random -Max 32767 -Min 10001;
}

Function Test-PortInUse
{
    Param(
        [Parameter(Mandatory=$true)]
        [Int] $portToTest
    );
    $count = netstat -aon | find `":$portToTest `" /c;
    return [bool]($count -gt 0);
}

Function Get-RandomUsablePort
{
    Param(
        [Int] $maxTries = 100
    );
    $result = -1;
    $tries = 0;
    DO
    {
        $randomPort = Get-RandomPort;
        if (-Not (Test-PortInUse($randomPort)))
        {
            $result = $randomPort;
        }
        $tries += 1;
    } While (($result -lt 0) -and ($tries -lt $maxTries));
    return $result;
}

In another ps1 file I call this Get_RandomPort like

<#
This powershell script start ThirdFlow (TW+NOde.js) in dev mode
A port number can be passed, if not a random port is found
Rev 0.1 - 2021.10.23

Mohammad
Oct 24, 2021

#>

param(
 [int]$port
)

Set-StrictMode -Version 1
Import-Module "$PSScriptRoot\scripts\library\RandomizedPort.psm1"


if(-not $port) #if port has not been passed, use random port
 {
    $port = Get-RandomUsablePort #get a free random port
}
 
tiddlywiki --listen port=$port
2 Likes

I’ve found a post on stackoverflow explaining how to get a free port with a batch script, this might interest you :

@echo off

set freePort=

set startPort=80

:SEARCHPORT

netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL

if "%ERRORLEVEL%" equ "0" (

  echo "port unavailable %startPort%"

  set /a startPort +=1

  GOTO :SEARCHPORT

) ELSE (

  echo "port available %startPort%"

  set freePort=%startPort%

  GOTO :FOUNDPORT

)

:FOUNDPORT

start http://127.0.0.1:%freePort% & tiddlywiki --listen port=%freePort%
2 Likes