puglatizer.js
function(callback) {

			var fileLoop = function(currentDir,templateDirectories,cb) {
				var callback_has_been_called = false
				fs.readdir(currentDir,function(err,files) {
					if (err) {
						return console.log('Unable to find files in path',currentDir)
					}
					var num = files.length

					var finishFile = function(i) {
						if (!(--num)) {
							if (!callback_has_been_called) {
								callback_has_been_called = true
								return cb()
							}
						} else {
							buildFileData(i+1)
						}
					}

					var buildFileData = function(i) {
						var file = files[i]
						var filepath = currentDir + '/' + file

						fs.stat(filepath,function(err2,stats) {
							if (err2) {
								return console.log('Unable to find file',filepath)
							}

							if (stats && stats.isDirectory()) {
								var pugatizerPath = '    puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = {}\r\n'
								pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
								fs.appendFileSync(outputFile,pugatizerPath)
								fileLoop(filepath,templateDirectories,function() {
									finishFile(i)
								})
							} else if (stats && stats.isFile()) {
								var ext = path.extname(filepath)
								if (ext == '.pug') {
									var pugatizerPath = '    puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = '
									pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
									buildTemplateFromFile(filepath,options,function(err,template) {
										pugatizerPath += minify.js(template.toString()) + ';\r\n\r\n'
										fs.appendFileSync(outputFile,pugatizerPath)
										finishFile(i)
									})
								} else {
									finishFile(i)
								}
							} else {
								finishFile(i)
							}
						})
					}

					if (!num) {
						return console.log('Unable to find files in path',currentDir)
					} else {
						buildFileData(0)
					}

				})

			}
			
			fileLoop(templateDirectories,templateDirectories,function() {
				return callback()
			})
		}
function(callback) {

			var fileLoop = function(currentDir,templateDirectories,cb) {
				var callback_has_been_called = false
				fs.readdir(currentDir,function(err,files) {
					if (err) {
						return console.log('Unable to find files in path',currentDir)
					}
					var num = files.length

					var finishFile = function(i) {
						if (!(--num)) {
							if (!callback_has_been_called) {
								callback_has_been_called = true
								return cb()
							}
						} else {
							buildFileData(i+1)
						}
					}

					var buildFileData = function(i) {
						var file = files[i]
						var filepath = currentDir + '/' + file

						fs.stat(filepath,function(err2,stats) {
							if (err2) {
								return console.log('Unable to find file',filepath)
							}

							if (stats && stats.isDirectory()) {
								var pugatizerPath = '    puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = {}\r\n'
								pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
								fs.appendFileSync(outputFile,pugatizerPath)
								fileLoop(filepath,templateDirectories,function() {
									finishFile(i)
								})
							} else if (stats && stats.isFile()) {
								var ext = path.extname(filepath)
								if (ext == '.pug') {
									var pugatizerPath = '    puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = '
									pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
									buildTemplateFromFile(filepath,options,function(err,template) {
										pugatizerPath += minify.js(template.toString()) + ';\r\n\r\n'
										fs.appendFileSync(outputFile,pugatizerPath)
										finishFile(i)
									})
								} else {
									finishFile(i)
								}
							} else {
								finishFile(i)
							}
						})
					}

					if (!num) {
						return console.log('Unable to find files in path',currentDir)
					} else {
						buildFileData(0)
					}

				})

			}
			
			fileLoop(templateDirectories,templateDirectories,function() {
				return callback()
			})
		}
function ( callback ) { var fileLoop = function ( currentDir , templateDirectories , cb ) { var callback_has_been_called = false fs . readdir ( currentDir , function ( err , files ) { if ( err ) { return console . log ( 'Unable to find files in path' , currentDir ) } var num = files . length var finishFile = function ( i ) { if ( ! ( -- num ) ) { if ( ! callback_has_been_called ) { callback_has_been_called = true return cb ( ) } } else { buildFileData ( i + 1 ) } } var buildFileData = function ( i ) { var file = files [ i ] var filepath = currentDir + '/' + file fs . stat ( filepath , function ( err2 , stats ) { if ( err2 ) { return console . log ( 'Unable to find file' , filepath ) } if ( stats && stats . isDirectory ( ) ) { var pugatizerPath = '    puglatizer' + currentDir . replace ( templateDirectories , '' ) . replace ( / \/ / g , '"]["' ) + '"]["' + file . replace ( '.pug' , '' ) + '"] = {}\r\n' pugatizerPath = pugatizerPath . replace ( 'puglatizer"]' , 'puglatizer' ) fs . appendFileSync ( outputFile , pugatizerPath ) fileLoop ( filepath , templateDirectories , function ( ) { finishFile ( i ) } ) } else if ( stats && stats . isFile ( ) ) { var ext = path . extname ( filepath ) if ( ext == '.pug' ) { var pugatizerPath = '    puglatizer' + currentDir . replace ( templateDirectories , '' ) . replace ( / \/ / g , '"]["' ) + '"]["' + file . replace ( '.pug' , '' ) + '"] = ' pugatizerPath = pugatizerPath . replace ( 'puglatizer"]' , 'puglatizer' ) buildTemplateFromFile ( filepath , options , function ( err , template ) { pugatizerPath += minify . js ( template . toString ( ) ) + ';\r\n\r\n' fs . appendFileSync ( outputFile , pugatizerPath ) finishFile ( i ) } ) } else { finishFile ( i ) } } else { finishFile ( i ) } } ) } if ( ! num ) { return console . log ( 'Unable to find files in path' , currentDir ) } else { buildFileData ( 0 ) } } ) } fileLoop ( templateDirectories , templateDirectories , function ( ) { return callback ( ) } ) }
Here we build all the pug functions
Here we build all the pug functions