This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
73 lines (62 loc) · 1.97 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var _ = require('lodash'),
Bluebird = require('bluebird'),
path = require('path'),
debug = require('debug')('configurated-sample-generator'),
fs = Bluebird.promisifyAll(require('fs')),
packer = require('./packer'),
util = require('util');
function PackerConfigurer(options) {
options = options || {};
this.organization = options.organization;
this.types = {
noop: _.identity
};
this.infoLoader = _.identity;
}
PackerConfigurer.prototype.addTypeProcesor = function(name, fn) {
this.types[name] = fn;
return this;
}
PackerConfigurer.prototype.setInformationLoader = function(fn) {
this.infoLoader = fn;
return this;
}
PackerConfigurer.prototype.getPacker = function() {
return _.partial(packer, this);
}
PackerConfigurer.fileWriter = function(fn) {
return function fileWriter(context) {
var fileDesc = fn(context);
return fs.writeFileAsync(path.join(context.configurationFilePath || context.examplePath, fileDesc.name), fileDesc.content, {flags: 'w'}).then(function() {
debug("File created");
return context;
});
};
};
PackerConfigurer.envFileCreator = function(fn) {
return PackerConfigurer.fileWriter(function(context) {
var env = _.map(fn(context), function(value, key) {
return util.format('%s=%s', key, value);
}).join(' \n');
return {
name: '.env',
content: env + '\n'
};
});
};
PackerConfigurer.fileReplacer = function(fn) {
return function searchAndReplaceConfig(context) {
return fs.readFileAsync(context.configurationFilePath)
.then(function(contents) {
debug("Reading content of existing file to replace");
var finalContent = fn(context, contents.toString());
debug("Content replaced");
return fs.writeFileAsync(context.configurationFilePath, finalContent, {flags: 'w'})
.then(function() {
debug("New content written");
return context;
});
});
};
}
module.exports = PackerConfigurer;