From f3930ad69c343d24343a594c7de876a046584923 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sun, 23 Feb 2014 22:58:17 +0000 Subject: [PATCH] Add init command to initialise a wiki folder Fixes #362 --- boot/boot.js | 1 + core/modules/commands/init.js | 47 ++++++++ core/modules/utils/filesystem.js | 111 ++++++++++++++++++ .../tw5.com/tiddlers/commands/InitCommand.tid | 21 ++++ .../Notes for upgrading to 5.0.8-beta.tid | 4 +- 5 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 core/modules/commands/init.js create mode 100644 core/modules/utils/filesystem.js create mode 100644 editions/tw5.com/tiddlers/commands/InitCommand.tid diff --git a/boot/boot.js b/boot/boot.js index ae026fc46..eaad53776 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -1456,6 +1456,7 @@ $tw.boot.startup = function(options) { pluginsPath: "../plugins/", themesPath: "../themes/", languagesPath: "../languages/", + editionsPath: "../editions/", wikiInfo: "./tiddlywiki.info", wikiPluginsSubDir: "./plugins", wikiThemesSubDir: "./themes", diff --git a/core/modules/commands/init.js b/core/modules/commands/init.js new file mode 100644 index 000000000..5eda044bc --- /dev/null +++ b/core/modules/commands/init.js @@ -0,0 +1,47 @@ +/*\ +title: $:/core/modules/commands/init.js +type: application/javascript +module-type: command + +Command to initialise an empty wiki folder + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "init", + synchronous: true +}; + +var Command = function(params,commander) { + this.params = params; + this.commander = commander; +}; + +Command.prototype.execute = function() { + var path = require("path"), + editionName = this.params[0] || "empty"; + // Check that we don't already have a valid wiki folder + if($tw.boot.wikiTiddlersPath) { + return "Wiki folder is not empty"; + } + // Check the edition exists + var editionPath = path.resolve($tw.boot.corePath,$tw.config.editionsPath) + "/" + editionName; + if(!$tw.utils.isDirectory(editionPath)) { + return "Edition '" + editionName + "' not found"; + } + // Copy the edition content + var err = $tw.utils.copyDirectory(editionPath,$tw.boot.wikiPath); + if(!err) { + this.commander.streams.output.write("Copied edition '" + editionName + "' to " + $tw.boot.wikiPath + "\n"); + } + return err; +}; + +exports.Command = Command; + +})(); diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js new file mode 100644 index 000000000..82b816ff9 --- /dev/null +++ b/core/modules/utils/filesystem.js @@ -0,0 +1,111 @@ +/*\ +title: $:/core/modules/utils/filesystem.js +type: application/javascript +module-type: utils-node + +File system utilities + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var fs = require("fs"), + path = require("path"); + +/* +Recursively (and synchronously) copy a directory and all its content +*/ +exports.copyDirectory = function(srcPath,dstPath) { + // Remove any trailing path separators + srcPath = $tw.utils.removeTrailingSeparator(srcPath); + dstPath = $tw.utils.removeTrailingSeparator(dstPath); + // Create the destination directory + var err = $tw.utils.createDirectory(dstPath); + if(err) { + return err; + } + // Function to copy a folder full of files + var copy = function(srcPath,dstPath) { + var srcStats = fs.lstatSync(srcPath), + dstExists = fs.existsSync(dstPath); + if(srcStats.isFile()) { + $tw.utils.copyFile(srcPath,dstPath); + } else if(srcStats.isDirectory()) { + var items = fs.readdirSync(srcPath); + for(var t=0; t 0) { + bytesRead = fs.readSync(srcFile,fileBuffer,0,FILE_BUFFER_LENGTH,pos); + fs.writeSync(dstFile,fileBuffer,0,bytesRead); + pos += bytesRead; + } + fs.closeSync(srcFile); + fs.closeSync(dstFile); + return null; +} + +/* +Remove trailing path separator +*/ +exports.removeTrailingSeparator = function(dirPath) { + var len = dirPath.length; + if(dirPath.charAt(len-1) === path.sep) { + dirPath = dirPath.substr(0,len-1); + } + return dirPath; +}; + +/* +Recursively create a directory +*/ +exports.createDirectory = function(dirPath) { + var parts = dirPath.split(path.sep); + for(var component=0; component +``` + +The "edition" defaults to ''empty''. + +For example: + +``` +tiddlywiki ./MyWikiFolder --init empty +``` + +Note that the init command will fail if the wiki folder does not exist, or is not empty. diff --git a/editions/tw5.com/tiddlers/howtos/Notes for upgrading to 5.0.8-beta.tid b/editions/tw5.com/tiddlers/howtos/Notes for upgrading to 5.0.8-beta.tid index 8d5713661..08843b7c9 100644 --- a/editions/tw5.com/tiddlers/howtos/Notes for upgrading to 5.0.8-beta.tid +++ b/editions/tw5.com/tiddlers/howtos/Notes for upgrading to 5.0.8-beta.tid @@ -1,5 +1,5 @@ created: 20140223183404938 -modified: 20140223192031783 +modified: 20140223195514667 tags: howto title: Notes for upgrading to 5.0.8-beta type: text/vnd.tiddlywiki @@ -54,4 +54,4 @@ And a paragraph of text. ! Changed commands for [[TiddlyWiki on Node.js]] -The handling of wiki folders has changed. Previously, if the `tiddlywiki` command was run against a wiki folder that didn't have the necessary `tiddlywiki.info` file then it would be automatically created. Now, the wiki folder must be initialised with the CreateWikiCommand. +The handling of wiki folders has changed. Previously, if the `tiddlywiki` command was run against a wiki folder that didn't have the necessary `tiddlywiki.info` file then it would be automatically created. Now, the wiki folder must be initialised with the InitCommand.