Cricket, cricket, huh?
I’ve looked into this a ton, and I’ve concluded that TiddlyWiki absolutely could support the installation of plugins through npm, which would be a great boon to plugin developers working in a Node.js environment.
For instance, consider tw5-uglify. It’s fully capable of integrating into a build environment to optionally produce minified plugin files, but currently it requires developers to checkout the repository and connect it to their TIDDLYWIKI_PLUGIN_PATH environment variable.
Barf.
Instead, if it were installable via npm, one would only need add the following to their package.json file:
"devDependencies":
"tiddlywiki": "^5.3.0",
"tw5-uglify": "github:flibbles/tw5-uglify"
}
(or maybe a version like "^v1.10.0" instead of a github reference if I ever put uglify into the npm repository.)
Then, they include the following in their tiddlywiki.info file:
"plugins": [
...
"flibbles/uglify"
]
And that’s it. When they execute npm install, npm will install not only a local tiddlywiki instance, but the required uglify plugin (and whatever other plugins they want).
…We could make this happen very easily.
The way a node_modules directory lays itself out is simple. Here’s an example with the dependencies tiddlywiki and gulp installed:
node_modules/
.bin/
tiddlywiki (symlink to node_modules/tiddlywiki/tiddlywiki.js)
gulp (symlink to node_modules/gulp/bin/gulp.js)
(Other symlinks of other packages. Always a flat directory)
tiddlywik/
LICENSE
package.json
tiddlywiki.js
(All other tiddlywiki files)
gulp/
LICENSE
package.json
bin/
gulp.js
(All other gulp files)
...
Pretty straight forward, right? It neatly sandboxes every dependency in its own directory, and the package.json files describe symlinks to add to .bin. This makes it all easy to remove later too.
What we can do: Those symlinks don’t have to point to script files. They can point to directories. So I experimented by configuring uglify to install like this:
node_modules/
.bin/
flibbles (symlink to node_modules/tw5-uglify/plugins/)
tw5-uglify/
LICENSE
package.json
plugins/
uglify/
plugin.info
readme.tid
(All other plugin files)
uglify-wizard/
plugin.info
(All other files for this supplemental plugin)
...
If Tiddlywiki were configured to look at the node_modules/.bin directory, then it will automatically discover ALL TW plugins installed in this manner. For instance, it will find uglify by following flibbles/uglify/plugin.info, which is the directory structure Tiddlywiki expects.
There are only a couple caveats to this.
- Tiddlywiki isn’t configured to look in
node_modules/.bin automatically, and I think this is a crying shame. When you run anything through npm, it configures the PATH to look in all node_modules/.bin directorys all the way up the directory chain, including in /usr/local/lib/node_modules/.bin where global installaitions go. Why can’t TiddlyWiki do the same?
- The current way TiddlyWiki looks for plugins in directories (i.e.
(author handle)/(plugin name)/plugin.info), means we can’t install two plugins from the same author. My flibbles symlink above needs to point into Uglify/plugins. It can’t also point into Relink/plugins. But if Tiddlywiki were tweaked such that it could also look for (author handle)_(plugin name)/plugin.info. Basically, use an underscore to merge the two nested directories into one. Then we could have the following layout.
node_modules/
.bin/
flibbles_uglify (symlink to node_modules/tw5-uglify/plugins/uglify)
flibbles_uglify-wizard (symlink to node_modules/tw5-uglify/plugins/uglify-wizard)
flibbles_relink (symlink to node_modules/tw5-relink/plugins/relink)
flibbles_relink-titles (symlink to node_modules/tw5-relink/plugins/relink-titles)
tw5-uglify/
LICENSE
package.json
plugins/
uglify/
plugin.info
...
uglify-wizard/
plugin.info
...
tw5-relink/
LICENSE
package.json
plugins/
relink/
plugin.info
...
relinik-titles/
plugin.info
...
And everything works perfectly.
If anyone knows about my plugins, I have incredibly simple repository layouts that don’t use fancy things like gulp or sass, and yet this npm system is so simple that my plugins (and anyone’s plugins) can fully support this npm structure without introducing any of that. In fact, I can have npm dependencies point to my plugins directly in github, and it works fine. I’ve actually started doing this for TW5-Graphs’s dependency on relink for tests. But this is currently imperfect. You’ll see those package scripts are having to manually add relink for tests with ++ notation.
So how about it, guys? Is this a direction TiddlyWiki would benefit from? Obviously it’d be great for uglify’s adoption, but it could be a huge boon for many other Node.js oriented plugins.
@linonetwo, I’d love your input especially, since you’re the one who encouraged me down this path. And you’re far more familiar with npm than I am.