From 51db48acc901d8fb298d9b0f7f10b47ffe90df05 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 10 Dec 2014 22:14:27 +0000 Subject: [PATCH] Fix checking for non-empty folders in initcommand --- core/modules/commands/init.js | 2 +- core/modules/utils/filesystem.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/modules/commands/init.js b/core/modules/commands/init.js index 108124112..51b80f267 100644 --- a/core/modules/commands/init.js +++ b/core/modules/commands/init.js @@ -26,7 +26,7 @@ Command.prototype.execute = function() { var fs = require("fs"), path = require("path"); // Check that we don't already have a valid wiki folder - if($tw.boot.wikiTiddlersPath) { + if($tw.boot.wikiTiddlersPath || ($tw.utils.isDirectory($tw.boot.wikiPath) && !$tw.utils.isDirectoryEmpty($tw.boot.wikiPath))) { return "Wiki folder is not empty"; } // Loop through each of the specified editions diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index d0fe34645..d7d084221 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -139,4 +139,21 @@ exports.isDirectory = function(dirPath) { return fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory(); }; +/* +Check if a path identifies a directory that is empty +*/ +exports.isDirectoryEmpty = function(dirPath) { + if(!$tw.utils.isDirectory(dirPath)) { + return false; + } + var files = fs.readdirSync(dirPath), + empty = true; + $tw.utils.each(files,function(file,index) { + if(file.charAt(0) !== ".") { + empty = false; + } + }); + return empty; +}; + })();