I have a few common files like logger, stopwatch, metrics, etc. Now, I would like to add all of them in 1 common.coffee and place this files in a common folder under lib.
lib/common/logger.coffee
lib/common/metrics.coffee
lib/common/stopwatch.coffee
lib/common.coffee
Now, when I have to use these files. I just do a
require( 'lib/common' )
and should be able to call the logger class
like logger.info, etc in the lib files.
How to go about doing it ? Below is the common.coffee but it requires me to say “Common.logger” whenever I have to use it. I dont want the Common prefix
nconf = require('nconf')
environment = process.env.NODE_ENV || 'development'
nconf.file 'environment', "config/#{environment}.json"
nconf.file 'default', 'config/default.json'
module.exports = {
logger: require('lib/common/logger')
metrics: require('lib/common/metrics') nconf
stopwatch: require('lib/common/stop_watch')
}
Also, how can I make a module of the common folder so that I can just use npm to install it.
You could use destructuring assignments on your require call.
another way would be to use a build tool like grunt.js and call a concatination task before building the final deployment articfact.