Thought JS developers that are using the ‘tiddlywiki’ module in node.js apps would find this script useful. I use TiddlyWiki as a database quite a bit - using filters as a query language is awesome. But miss access to $tw
like when in browser dev tools.
This script brings up node.js REPL with access to $tw
. At the least is handy to list all the functions for any $tw
object - type $tw.utils.
and press tab twice. (note the period at the end), lists all $tw.utils.
functions and exported variables.
/*
* Author: @poc2go at talk.tiddlywiki.org
* License: MIT
* Description: Runs a node.js REPL with '$tw' installed.
*
* Usage:
mkdir twrepl && cd twrepl
npm install tiddlywiki
* In an editor, Copy/Paste this script, save as 'twrepl.js', then
node ./twrepl
*
* Enjoy ;)
*/
// Terminal colours
const colour = {
log: (txt='', fg=255, bg=0, efg=255, ebg=0) => process.stdout.write(
`\x1b[38;5;${fg};48;5;${bg}m${txt}\x1b[38;5;${efg};48;5;${ebg}m`),
txt: (txt='', fg=255, bg=0, efg=255, ebg=0) =>
`\x1b[38;5;${fg};48;5;${bg}m${txt}\x1b[38;5;${efg};48;5;${ebg}m`,
}
// -------------------
// Initialize
// Boot tiddlywiki module
const $tw = require('tiddlywiki').TiddlyWiki();
$tw.preloadTiddlers = [ { title: 'preloaded', text: 'This tiddler is loaded on TW bootup'} ];
$tw.boot.argv = ['output']; // TW output path
$tw.boot.boot(() => {});
// Who are we?
function intro() {
colour.log( '$tw-repl: ',75); colour.log('v1.0.0\n',130,0,110);
}
// -------------------
// Node'js REPL
// Place $tw in REPL context so can be referenced
function resetContext() {
runtime.context.$tw = $tw;
}
// REPL runtime
var runtime;
function startRepl() {
runtime = require('node:repl').start({
prompt: colour.txt('$tw-repl> ',33,0,7,0), useColors: true,
ignoreUndefined: true, /*completer: completer*/
});
// If REPL is reset (.clear) - context needs resetting
runtime.on('reset', () => resetContext());
// Initial context settings
resetContext();
}
// -------------------
// -------------------
// Startup
// Show our name and version
intro();
startRepl();
runtime._ttyWrite('$tw.version\n');
runtime._ttyWrite(`$tw.wiki.getTiddler('preloaded').fields\n`);
Displays this:
$tw-repl: v1.0.0
$tw-repl> $tw.version
'5.3.1'
$tw-repl> $tw.wiki.getTiddler('preloaded').fields
[Object: null prototype] {
title: 'preloaded',
text: 'This tiddler is loaded on TW bootup'
}
$tw-repl>
Hope find useful or at least parts - colours, tiddlywiki boot, or REPL for your own explorations!