-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var packageFile = require('./package.json'),
fs = require('fs'),
_ = require('underscore'),
ejs = require('ejs');
var versionFile = module.exports = function(configObject){
var currentTime = (new Date()),
name = packageFile.name,
version = packageFile.version,
fileContent;
var defaultConfig = {
pathToOutputFile: __dirname + '/version.txt',
pathToTemplate: 'version.ejs',
templateString: '',
extras: {}
};
/**
* Renders the template and writes the version file to the file system.
* @param templateContent
*/
var writeFile = function(templateContent){
fileContent = ejs.render(templateContent, {'name': name, 'version': version, 'time': currentTime, extras: configObject.extras});
fs.writeFileSync(configObject.pathToOutputFile, fileContent, {flag: 'w'});
}
//set default config data
configObject = configObject || {};
configObject = _.defaults(configObject, defaultConfig);
// If we are given a template string in the config, then use it directly.
// But if we get a file path, fetch the content then use it.
if(configObject.templateString){
writeFile(configObject.templateString);
}else{
var content = fs.readFileSync(configObject.pathToTemplate, {encoding: 'utf8'});
writeFile(content);
}
};