change-creator.js
5.89 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
/**
* Configuration Options:
* - write file location
* - read file location
*/
function checkProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
/**
* Generates a 'random' hex value.
* More 'random' than Math.random without depending on a GUID module.
*/
function generateRandomIdentifier() {
return crypto.randomBytes(4).toString('hex');
}
var CHANGES_DIR = path.join(process.cwd(), '.changes');
/**
* A map of valid change types.
* Can be referenced outside of this module.
*/
var VALID_TYPES = Object.create(null);
VALID_TYPES['bugfix'] = true;
VALID_TYPES['feature'] = true;
/**
* Handles creating a change log entry JSON file.
*/
function ChangeCreator(config) {
this._config = config || {};
this._type = '';
this._category = '';
this._description = '';
}
ChangeCreator.prototype = {
getChangeType: function getChangeType() {
return this._type;
},
setChangeType: function setChangeType(type) {
this._type = type;
},
getChangeCategory: function getChangeCategory() {
return this._category;
},
setChangeCategory: function setChangeCategory(category) {
this._category = category;
},
getChangeDescription: function getChangeDescription() {
return this._description;
},
setChangeDescription: function setChangeDescription(description) {
this._description = description;
},
/**
* Validates the entire change entry.
*/
validateChange: function validateChange() {
var type = this.getChangeType();
var category = this.getChangeCategory();
var description = this.getChangeDescription();
var missingFields = [];
this.validateChangeType(type);
this.validateChangeCategory(category);
this.validateChangeDescription(description);
return this;
},
/**
* Validates a change entry type.
*/
validateChangeType: function validateChangeType(type) {
var type = type || this._type;
if (!type) {
throw new Error('ValidationError: Missing `type` field.');
}
if (VALID_TYPES[type]) {
return this;
}
var validTypes = Object.keys(VALID_TYPES).join(',');
throw new Error('ValidationError: `type` set as "' + type + '" but must be one of [' + validTypes + '].');
},
/**
* Validates a change entry category.
*/
validateChangeCategory: function validateChangeCategory(category) {
var category = category || this._category;
if (!category) {
throw new Error('ValidationError: Missing `category` field.');
}
return this;
},
/**
* Validates a change entry description.
*/
validateChangeDescription: function validateChangeDescription(description) {
var description = description || this._description;
if (!description) {
throw new Error('ValidationError: Missing `description` field.');
}
return this;
},
/**
* Creates the output directory if it doesn't exist.
*/
createOutputDirectory: function createOutputDirectory(outPath) {
var pathObj = path.parse(outPath);
var sep = path.sep;
var directoryStructure = pathObj.dir.split(sep) || [];
for (var i = 0; i < directoryStructure.length; i++) {
var pathToCheck = directoryStructure.slice(0, i + 1).join(sep);
if (!pathToCheck) {
continue;
}
try {
var stats = fs.statSync(pathToCheck);
} catch (err) {
if (err.code === 'ENOENT') {
// Directory doesn't exist, so create it
fs.mkdirSync(pathToCheck);
} else {
throw err;
}
}
}
return this;
},
/**
* Returns a path to the future change entry file.
*/
determineWriteLocation: function determineWriteLocation() {
/* Order for determining write location:
1) Check configuration for `outFile` location.
2) Check configuration for `inFile` location.
3) Create a new file using default location.
*/
var config = this._config || {};
if (checkProperty(config, 'outFile') && config['outFile']) {
return config['outFile'];
}
if (checkProperty(config, 'inFile') && config['inFile']) {
return config['inFile'];
}
// Determine default location
var newFileName = this._type + '-' + this._category + '-' + generateRandomIdentifier() + '.json';
return path.join(process.cwd(), '.changes', 'next-release', newFileName);
},
/**
* Writes a change entry as a JSON file.
*/
writeChanges: function writeChanges(callback) {
var hasCallback = typeof callback === 'function';
var fileLocation = this.determineWriteLocation();
try {
// Will throw an error if the change is not valid
this.validateChange().createOutputDirectory(fileLocation);
var change = {
type: this.getChangeType(),
category: this.getChangeCategory(),
description: this.getChangeDescription()
}
fs.writeFileSync(fileLocation, JSON.stringify(change, null, 2));
var data = {
file: fileLocation
};
if (hasCallback) {
return callback(null, data);
} else {
return data;
}
} catch (err) {
if (hasCallback) {
return callback(err, null);
} else {
throw err;
}
}
}
}
module.exports = {
ChangeCreator: ChangeCreator,
VALID_TYPES: VALID_TYPES
};