-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommentwrap.js
51 lines (43 loc) · 1.33 KB
/
commentwrap.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
var reLeadingDot = /^\./;
var slashStarComments = {
leading: '/*',
line: ' * ',
end: ' */ '
};
var fileComments = {
js: slashStarComments,
css: slashStarComments,
coffee: {
leading: '###',
line: '',
end: '###'
}
};
var reLineBreak = /\r?\n/;
// intialise line endings based on platform
var lineEnding = process.platform == 'win32' ? '\r\n' : '\n';
module.exports = function(opts, content) {
// split the content on line breaks
var lines = [''].concat((content || '').split(reLineBreak)).concat('');
var lastLineIdx = lines.length - 1;
var filetype = (opts.filetype || 'js').replace(reLeadingDot, '');
var comments;
// get the comments for the specific filetype
comments = fileComments[filetype] || slashStarComments;
// if we have a leader then add an additional line for a tidy output
if (opts.leader) {
lines.unshift('');
}
// iterate through the lines and add the comments
lines = lines.map(function(line, index) {
var comment = comments[index === 0 ? 'leading' : 'line'];
// if we are at the first line, then add the optional leader
if (index === 0) {
comment += (opts.leader || '');
}
// add the comment line to the content line
return comment + line;
}).concat(comments.end);
// wrap in the comments
return lines.join(lineEnding);
};