Blocking While Dynamically Adding Project Directories/Files


#1

Hello,

As the title states, I am having issues understanding how to “block” while loading new directories into my project on startup. I am using a node module called “simple-git” to add a submodule to my project providing it does not exist. However, it is also the point where things start breaking. Here is the code I have so far:

Meteor.startup(() => {
	// If dir does not exist, add it as a submodule
	if (!dirExists()) {
		console.log('Directory does not exist!');
		console.log('Adding submodule...');

		require('simple-git')('../../../../../')
		.submoduleAdd('...gitStuff.git', 'newSubmodule/', function(err) {
			if (err) throw err;
			populateDB();
		});
	}
	// Otherwise, update submodule
	else {
		console.log('Directory exists!');
		console.log('Updating submodule...');
		updateSubmodule();
	}
});

function dirExists() {
	try {
		fs.statSync('../../../../../newSubmodule');
	} catch(e) {
		return false;
	}
	return true;
}

function populateDB() {
	// Check if file can be accessed
	fs.stat(PERMISSIONS_PATH, function(err, stat) {
		if (!err) {
			var Fiber = Npm.require('fibers');
			Fiber(function () {
				var count = Blah.find().count();

				// If DB is empty, just populate it
				if (count === 0) {
					console.log('Collection is empty!');
					console.log('Populating database...');
				}
			}).run();
		} else if (err.code == 'ENOENT') {
			console.log('File does not exist!');
		} else {
			console.log('File could not be accessed!');
		}
	});
}

When I start my project, nothing goes as I would expect to. Here is the log from the above code:

I20160602-09:13:19.237(-7)? Directory does not exist!
I20160602-09:13:19.238(-7)? Adding metasearch submodule...
=> Meteor server restarted
I20160602-09:13:22.304(-7)? Directory exists! 
I20160602-09:13:22.304(-7)? Updating submodule...
I20160602-09:13:22.314(-7)? Collection is empty!
I20160602-09:13:22.315(-7)? Populating database...
=> Meteor server restarted
I20160602-09:13:31.733(-7)? Directory exists! 
I20160602-09:13:31.733(-7)? Updating submodule...
I20160602-09:13:31.742(-7)? Collection is empty!
I20160602-09:13:31.742(-7)? Populating database...
=> Meteor server restarted

Why does the server need to restart multiple times? Obviously, I am doing something wrong and if anyone could point me in the right direction that would be great.

Additionally, if anyone can tell me how to access my DB in the startup function properly that would be awesome! As can be seen above, I start a new fiber to read from my DB because otherwise it got upset.

Thanks so much!