mirror of
				https://github.com/flynx/pWiki.git
				synced 2025-10-31 11:00:08 +00:00 
			
		
		
		
	added cleanup(..), still need backups...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									b984928e68
								
							
						
					
					
						commit
						c24af7eaee
					
				
							
								
								
									
										96
									
								
								pwiki2.js
									
									
									
									
									
								
							
							
						
						
									
										96
									
								
								pwiki2.js
									
									
									
									
									
								
							| @ -759,20 +759,35 @@ module.localStorageNestedStore = { | |||||||
| var fs = require('fs') | var fs = require('fs') | ||||||
| var glob = require('glob') | 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
 | //		-> true/false
 | ||||||
| //
 | //
 | ||||||
| //	exists(base, path[, options])
 | //	func(base, path[, options])
 | ||||||
| //		-> true/false
 | //		-> true/false
 | ||||||
| //
 | //
 | ||||||
|  | // XXX should these be store methods???
 | ||||||
|  | // XXX do we need error checking in these???
 | ||||||
| var exists = | var exists = | ||||||
| module.exists = | module.exists = | ||||||
| async function(base, sub, options={index: '.index'}){ | async function(base, sub, options){ | ||||||
| 	if(typeof(sub) != 'string'){ | 	if(typeof(sub) != 'string'){ | ||||||
| 		options = sub ?? options | 		options = sub ?? options | ||||||
| 		sub = base | 		sub = base | ||||||
| 		base = null } | 		base = null } | ||||||
| 	var {index} = options | 	var {index} = Object.assign({},  | ||||||
|  | 		FILESTORE_OPTIONS,  | ||||||
|  | 		options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	var target = base ? | 	var target = base ? | ||||||
| 		module.path.join(base, sub) | 		module.path.join(base, sub) | ||||||
| @ -785,12 +800,14 @@ async function(base, sub, options={index: '.index'}){ | |||||||
| 	return true } | 	return true } | ||||||
| var read = | var read = | ||||||
| module.read = | module.read = | ||||||
| async function(base, sub, options={index: '.index'}){ | async function(base, sub, options){ | ||||||
| 	if(typeof(sub) != 'string'){ | 	if(typeof(sub) != 'string'){ | ||||||
| 		options = sub ?? options | 		options = sub ?? options | ||||||
| 		sub = base | 		sub = base | ||||||
| 		base = null } | 		base = null } | ||||||
| 	var {index} = options | 	var {index} = Object.assign({},  | ||||||
|  | 		FILESTORE_OPTIONS,  | ||||||
|  | 		options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	var target = base ? | 	var target = base ? | ||||||
| 		module.path.join(base, sub) | 		module.path.join(base, sub) | ||||||
| @ -806,14 +823,26 @@ async function(base, sub, options={index: '.index'}){ | |||||||
| 	return target ? | 	return target ? | ||||||
| 		fs.promises.readFile(target, {encoding: 'utf-8'}) | 		fs.promises.readFile(target, {encoding: 'utf-8'}) | ||||||
| 		: undefined } | 		: undefined } | ||||||
|  | // XXX
 | ||||||
|  | var backup = | ||||||
|  | module.backup = | ||||||
|  | async function(base, sub, options){ | ||||||
|  | 	var {index, backup} = | ||||||
|  | 		Object.assign({},  | ||||||
|  | 			FILESTORE_OPTIONS,  | ||||||
|  | 			options ?? {}) | ||||||
|  | 	// XXX
 | ||||||
|  | } | ||||||
| var mkdir =  | var mkdir =  | ||||||
| module.mkdir = | module.mkdir = | ||||||
| async function(base, sub, options={index: '.index'}){ | async function(base, sub, options){ | ||||||
| 	if(typeof(sub) != 'string'){ | 	if(typeof(sub) != 'string'){ | ||||||
| 		options = sub ?? options | 		options = sub ?? options | ||||||
| 		sub = base  | 		sub = base  | ||||||
| 		base = null } | 		base = null } | ||||||
| 	var {index} = options | 	var {index} = Object.assign({},  | ||||||
|  | 		FILESTORE_OPTIONS,  | ||||||
|  | 		options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	var levels = module.path.split(sub) | 	var levels = module.path.split(sub) | ||||||
| 	for(var level of levels){ | 	for(var level of levels){ | ||||||
| @ -822,6 +851,8 @@ async function(base, sub, options={index: '.index'}){ | |||||||
| 			: module.path.join(base, level) | 			: module.path.join(base, level) | ||||||
| 		// nothing exists -- create dir and continue...
 | 		// nothing exists -- create dir and continue...
 | ||||||
| 		if(!fs.existsSync(base)){ | 		if(!fs.existsSync(base)){ | ||||||
|  | 			verbose  | ||||||
|  | 				&& console.log('mkdir(..): mkdir:', base) | ||||||
| 			await fs.promises.mkdir(base, {recursive: true})  | 			await fs.promises.mkdir(base, {recursive: true})  | ||||||
| 			continue } | 			continue } | ||||||
| 		// directory -- continue...
 | 		// directory -- continue...
 | ||||||
| @ -829,23 +860,24 @@ async function(base, sub, options={index: '.index'}){ | |||||||
| 		if(stat.isDirectory()){ | 		if(stat.isDirectory()){ | ||||||
| 			continue } | 			continue } | ||||||
| 		// file -- convert to dir...
 | 		// file -- convert to dir...
 | ||||||
|  | 		verbose  | ||||||
|  | 			&& console.log('mkdir(..): converting file to dir:', base) | ||||||
| 		await fs.promises.rename(base, base+'.pwiki-bak')  | 		await fs.promises.rename(base, base+'.pwiki-bak')  | ||||||
| 		await fs.promises.mkdir(base, {recursive: true})  | 		await fs.promises.mkdir(base, {recursive: true})  | ||||||
| 		await fs.promises.rename(base +'.pwiki-bak', base +'/'+ index) } | 		await fs.promises.rename(base +'.pwiki-bak', base +'/'+ index) } | ||||||
| 	return base } | 	return base } | ||||||
| // XXX error checking???
 |  | ||||||
| // XXX metadata???
 | // XXX metadata???
 | ||||||
| // XXX modes???
 |  | ||||||
| // XXX should this transform <dir>/.index into a file if nothing else exists in it???
 |  | ||||||
| var update =  | var update =  | ||||||
| module.update = | module.update = | ||||||
| async function(base, sub, data, options={index: '.index'}){ | async function(base, sub, data, options){ | ||||||
| 	if(typeof(data) != 'string'){ | 	if(typeof(data) != 'string'){ | ||||||
| 		options = data ?? options | 		options = data ?? options | ||||||
| 		data = sub | 		data = sub | ||||||
| 		sub = base | 		sub = base | ||||||
| 		base = null } | 		base = null } | ||||||
| 	var {index} = options | 	var {index} = Object.assign({},  | ||||||
|  | 		FILESTORE_OPTIONS,  | ||||||
|  | 		options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	var target = base ? | 	var target = base ? | ||||||
| 		module.path.join(base, sub) | 		module.path.join(base, sub) | ||||||
| @ -873,20 +905,16 @@ async function(base, sub, data, options={index: '.index'}){ | |||||||
| 	await f.writeFile(data) | 	await f.writeFile(data) | ||||||
| 	f.close() | 	f.close() | ||||||
| 	return target } | 	return target } | ||||||
| var backup = |  | ||||||
| module.backup = |  | ||||||
| async function(base, sub, options={index: '.index', backup:'/.backup'}){ |  | ||||||
| 	var {index, backup} = options |  | ||||||
| 	// XXX
 |  | ||||||
| } |  | ||||||
| var clear =  | var clear =  | ||||||
| module.clear = | module.clear = | ||||||
| async function(base, sub, options={index: '.index'}){ | async function(base, sub, options){ | ||||||
| 	if(typeof(sub) != 'string'){ | 	if(typeof(sub) != 'string'){ | ||||||
| 		options = sub ?? options | 		options = sub ?? options | ||||||
| 		sub = base | 		sub = base | ||||||
| 		base = '' } | 		base = '' } | ||||||
| 	var {index} = options | 	var {index} = Object.assign({},  | ||||||
|  | 		FILESTORE_OPTIONS,  | ||||||
|  | 		options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	// remove leaf...
 | 	// remove leaf...
 | ||||||
| 	var target = base == '' ? | 	var target = base == '' ? | ||||||
| @ -924,13 +952,13 @@ async function(base, sub, options={index: '.index'}){ | |||||||
| 				fs.promises.rmdir(cur) } } | 				fs.promises.rmdir(cur) } } | ||||||
| 		levels.pop() }  | 		levels.pop() }  | ||||||
| 	return target } | 	return target } | ||||||
| // XXX cleanup all sub-paths...
 |  | ||||||
| // 		- remove empty leaf dirs
 |  | ||||||
| // 		- dir -> file ???
 |  | ||||||
| var cleanup = | var cleanup = | ||||||
| module.cleanup = | module.cleanup = | ||||||
| async function(base, options={index: '.index'}){ | async function(base, options){ | ||||||
| 	var {index} = options | 	var {index, clearEmptyDir, dirToFile, verbose} =  | ||||||
|  | 		Object.assign({},  | ||||||
|  | 			FILESTORE_OPTIONS,  | ||||||
|  | 			options ?? {}) | ||||||
| 
 | 
 | ||||||
| 	glob(module.path.join(base, '**/*')) | 	glob(module.path.join(base, '**/*')) | ||||||
| 		.on('end', async function(paths){ | 		.on('end', async function(paths){ | ||||||
| @ -938,17 +966,25 @@ async function(base, options={index: '.index'}){ | |||||||
| 				.sort(function(a, b){ | 				.sort(function(a, b){ | ||||||
| 					return b.length - a.length }) | 					return b.length - a.length }) | ||||||
| 			for(var path of paths){ | 			for(var path of paths){ | ||||||
| 				var stat = await fs.promises.stat(base) | 				var stat = await fs.promises.stat(path) | ||||||
| 				if(stat.isDirectory()){ | 				if(stat.isDirectory()){ | ||||||
| 					var children = await fs.promises.readdir(path) | 					var children = await fs.promises.readdir(path) | ||||||
| 					// empty -> remove...
 | 					// empty -> remove...
 | ||||||
| 					if((children.length == 0){ | 					if(clearEmptyDir  | ||||||
|  | 							&& children.length == 0){ | ||||||
|  | 						verbose  | ||||||
|  | 							&& console.log('cleanup(..): removing dir:', path) | ||||||
| 						fs.promises.rmdir(path) | 						fs.promises.rmdir(path) | ||||||
| 						continue } | 						continue } | ||||||
| 					// dir -> file...
 | 					// dir -> file...
 | ||||||
| 					if(children.length == 1  | 					if(dirToFile | ||||||
|  | 							&& children.length == 1  | ||||||
| 							&& children[0] == index){ | 							&& children[0] == index){ | ||||||
| 						// XXX
 | 						verbose  | ||||||
|  | 							&& console.log('cleanup(..): converting dir to file:', path) | ||||||
|  | 						await fs.promises.rename(path +'/'+ index, path+'.pwiki-bak')  | ||||||
|  | 						await fs.promises.rmdir(path)  | ||||||
|  | 						await fs.promises.rename(path +'.pwiki-bak', path) | ||||||
| 						continue } | 						continue } | ||||||
| 				} } }) } | 				} } }) } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user