diff --git a/pwiki2.js b/pwiki2.js index 41351c9..737d613 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -759,20 +759,35 @@ module.localStorageNestedStore = { var fs = require('fs') var glob = require('glob') -// exists(base[, options]) + +var FILESTORE_OPTIONS = { + index: '.index', + backup: '/.backup', + + clearEmptyDir: true, + dirToFile: true, + + verbose: true, +} + +// func(base[, options]) // -> true/false // -// exists(base, path[, options]) +// func(base, path[, options]) // -> true/false // +// XXX should these be store methods??? +// XXX do we need error checking in these??? var exists = module.exists = -async function(base, sub, options={index: '.index'}){ +async function(base, sub, options){ if(typeof(sub) != 'string'){ options = sub ?? options sub = base base = null } - var {index} = options + var {index} = Object.assign({}, + FILESTORE_OPTIONS, + options ?? {}) var target = base ? module.path.join(base, sub) @@ -785,12 +800,14 @@ async function(base, sub, options={index: '.index'}){ return true } var read = module.read = -async function(base, sub, options={index: '.index'}){ +async function(base, sub, options){ if(typeof(sub) != 'string'){ options = sub ?? options sub = base base = null } - var {index} = options + var {index} = Object.assign({}, + FILESTORE_OPTIONS, + options ?? {}) var target = base ? module.path.join(base, sub) @@ -806,14 +823,26 @@ async function(base, sub, options={index: '.index'}){ return target ? fs.promises.readFile(target, {encoding: 'utf-8'}) : undefined } +// XXX +var backup = +module.backup = +async function(base, sub, options){ + var {index, backup} = + Object.assign({}, + FILESTORE_OPTIONS, + options ?? {}) + // XXX +} var mkdir = module.mkdir = -async function(base, sub, options={index: '.index'}){ +async function(base, sub, options){ if(typeof(sub) != 'string'){ options = sub ?? options sub = base base = null } - var {index} = options + var {index} = Object.assign({}, + FILESTORE_OPTIONS, + options ?? {}) var levels = module.path.split(sub) for(var level of levels){ @@ -822,6 +851,8 @@ async function(base, sub, options={index: '.index'}){ : module.path.join(base, level) // nothing exists -- create dir and continue... if(!fs.existsSync(base)){ + verbose + && console.log('mkdir(..): mkdir:', base) await fs.promises.mkdir(base, {recursive: true}) continue } // directory -- continue... @@ -829,23 +860,24 @@ async function(base, sub, options={index: '.index'}){ if(stat.isDirectory()){ continue } // file -- convert to dir... + verbose + && console.log('mkdir(..): converting file to dir:', base) await fs.promises.rename(base, base+'.pwiki-bak') await fs.promises.mkdir(base, {recursive: true}) await fs.promises.rename(base +'.pwiki-bak', base +'/'+ index) } return base } -// XXX error checking??? // XXX metadata??? -// XXX modes??? -// XXX should this transform