function fileWatcher() {
	let	channels = []
	let repoPaths = []
	let username;

	return storage.getAsync('user')
		.then(info => {
			username = info.currentUser
			return storage.getAsync('channels') 
		})
		.then(cachedChannels => {
			for (var channel in cachedChannels[username]) {
				if (cachedChannels[username][channel]) {
					channels.push(channel)
					repoPaths.push(cachedChannels[ username ][channel])
				}
			}
			channels.forEach((channel, index) => {
				let repoPath = repoPaths[index]
				gaze('**/*', {
					//Don't watch npm packages in a repo—it's prohibitively expensive and they shouldn't be
					//doing anything in there anyway.
					cwd: repoPath,
					ignore: ['**/node_modules/**', 'node_modules/**']
				}, function(err, watcher) {
					var watched = this.watched()
					//'All' will fire the callback if any files are changed, added, or deleted.
					this.on('all', function (event, filepath) {
						let payload = { event, channel, username }
						//Check to see if the gaze trigger was a file being added.
						//This can cut down on some logic downstream, because
						//other people couldn't be watching a file that was just created.

						let pathRegex = new RegExp( repoPath + '(\\S*)', 'g')
						let revisedPath = pathRegex.exec(filepath)[1]
						payload.filepath = revisedPath

						git(repoPath)
							.branch( (err, result) => {
								if (err) {
									console.log(err)
									payload.branch = {
									}
								} else {
									payload.branch = result
								}

								//adds a 'branch' property to the payload object,
								//with the value:
								//	{
								//		detached: BOOL,
								//		current: STRING indicating current branch,
								//		all: ARRAY of branch names (as STRINGS)
								//		branches: OBJ with a key for each branch,
								//			each with the format:
								//			{
								//				current: BOOL,
								//				name: STRING,
								//				commit: STRING,
								//				label: STRING (most recent commit msg)
								//			}
								//	}
							})
							.log(['--oneline'], (err, logReadout) => {
								if (err) {
									console.log(err)
								} else {
									payload.log = logParser(logReadout)
								}

								//adds a 'log' property to the payload object,
								//with the value:
								//	[ [hash for commit, commit msg], [hash for commit, commit msg], etc.]
							})
							.diff((err, diffReadout) => {
								if (err) {
									console.log(err)
									payload.diff ={
									}
								} else {
									payload.diff = diffParser(diffReadout)
								}

								//adds a 'diff' property to the payload object,
								//with the value:
								//	{
								//		(first filename): ARR of ARRS, each with format:
								//			[INT for line # where changes start, INT for line where changes end],
								//		(second filename): ARR of ARRS, etc.
								//	}
							})
							.then(() => {
								socket.emit('fileChanges', payload)
								return
							})
					})
				})
			})
		})
		.catch(err => console.error)
}
//Iterate through the user's repos and set up a watcher for each one.
Iterate through the user's repos and set up a watcher for each one.
Iterate through the user ' s repos and set up a watcher for each one .
function fileWatcher() {
	let	channels = []
	let repoPaths = []
	let username;
	return storage.getAsync('user')
		.then(info => {
			username = info.currentUser
			return storage.getAsync('channels') 
		})
		.then(cachedChannels => {
			for (var channel in cachedChannels[username]) {
				if (cachedChannels[username][channel]) {
					channels.push(channel)
					repoPaths.push(cachedChannels[ username ][channel])
				}
			}
			channels.forEach((channel, index) => {
				let repoPath = repoPaths[index]
				gaze('**/*', {
					cwd: repoPath,
					ignore: ['**/node_modules/**', 'node_modules/**']
				}, function(err, watcher) {
					var watched = this.watched()
					this.on('all', function (event, filepath) {
						let payload = { event, channel, username }
						let pathRegex = new RegExp( repoPath + '(\\S*)', 'g')
						let revisedPath = pathRegex.exec(filepath)[1]
						payload.filepath = revisedPath
						git(repoPath)
							.branch( (err, result) => {
								if (err) {
									console.log(err)
									payload.branch = {
									}
								} else {
									payload.branch = result
								}
							})
							.log(['--oneline'], (err, logReadout) => {
								if (err) {
									console.log(err)
								} else {
									payload.log = logParser(logReadout)
								}
							})
							.diff((err, diffReadout) => {
								if (err) {
									console.log(err)
									payload.diff ={
									}
								} else {
									payload.diff = diffParser(diffReadout)
								}
							})
							.then(() => {
								socket.emit('fileChanges', payload)
								return
							})
					})
				})
			})
		})
		.catch(err => console.error)
}
function fileWatcher ( ) { let channels = [ ] let repoPaths = [ ] let username ; return storage . getAsync ( 'user' ) . then ( info => { username = info . currentUser return storage . getAsync ( 'channels' ) } ) . then ( cachedChannels => { for ( var channel in cachedChannels [ username ] ) { if ( cachedChannels [ username ] [ channel ] ) { channels . push ( channel ) repoPaths . push ( cachedChannels [ username ] [ channel ] ) } } channels . forEach ( ( channel , index ) => { let repoPath = repoPaths [ index ] gaze ( '**/*' , { cwd : repoPath , ignore : [ '**/node_modules/**' , 'node_modules/**' ] } , function ( err , watcher ) { var watched = this . watched ( ) this . on ( 'all' , function ( event , filepath ) { let payload = { event , channel , username } let pathRegex = new RegExp ( repoPath + '(\\S*)' , 'g' ) let revisedPath = pathRegex . exec ( filepath ) [ 1 ] payload . filepath = revisedPath git ( repoPath ) . branch ( ( err , result ) => { if ( err ) { console . log ( err ) payload . branch = { } } else { payload . branch = result } } ) . log ( [ '--oneline' ] , ( err , logReadout ) => { if ( err ) { console . log ( err ) } else { payload . log = logParser ( logReadout ) } } ) . diff ( ( err , diffReadout ) => { if ( err ) { console . log ( err ) payload . diff = { } } else { payload . diff = diffParser ( diffReadout ) } } ) . then ( ( ) => { socket . emit ( 'fileChanges' , payload ) return } ) } ) } ) } ) } ) . catch ( err => console . error ) }
Iterate through the user's repos and set up a watcher for each one.
Iterate through the user ' s repos and set up a watcher for each one .
//Don't watch npm packages in a repo—it's prohibitively expensive and they shouldn't be //doing anything in there anyway. //'All' will fire the callback if any files are changed, added, or deleted. //Check to see if the gaze trigger was a file being added. //This can cut down on some logic downstream, because //other people couldn't be watching a file that was just created. //adds a 'branch' property to the payload object, //with the value: //	{ //		detached: BOOL, //		current: STRING indicating current branch, //		all: ARRAY of branch names (as STRINGS) //		branches: OBJ with a key for each branch, //			each with the format: //			{ //				current: BOOL, //				name: STRING, //				commit: STRING, //				label: STRING (most recent commit msg) //			} //	} //adds a 'log' property to the payload object, //with the value: //	[ [hash for commit, commit msg], [hash for commit, commit msg], etc.] //adds a 'diff' property to the payload object, //with the value: //	{ //		(first filename): ARR of ARRS, each with format: //			[INT for line # where changes start, INT for line where changes end], //		(second filename): ARR of ARRS, etc. //	}