This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (116 loc) · 3.23 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
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
var ejs = require('ejs');
var findRoot = require('find-root');
var flatten = require('flatten');
var fs = require('fs');
var globby = require('globby');
var path = require('path');
var stripBom = require('strip-bom');
/**
* Whether or not the path is absolute.
*
* @private
* @param {String} p
* @return {Boolean}
*/
function isAbsolute(p) {
return path.isAbsolute ? path.isAbsolute(p) : path.resolve(p) === p;
}
/**
* Normalize an array of patterns.
*
* @private
* @param {String|Array} patterns
* @return {Array}
*/
function normalize(patterns) {
if (!Array.isArray(patterns)) patterns = [patterns];
return patterns.filter(function (value) {
return typeof value === 'string' && /\S/.test(value);
}).map(function (pattern) {
return pattern.trim();
});
}
/**
* Attempts to render a banner template file.
*
* @private
* @param {String} filename
* @param {Object} context
* @throws {Error} If banner is missing or empty.
* @return {String}
*/
function render(filename, context) {
var banner = ejs.render(
fs.readFileSync(filename, {encoding: 'utf8'}),
context
).trim();
if (!banner) {
throw new Error('Empty banner: ' + filename);
}
return banner;
}
/**
* Gets a full path for a filename, respecting absolute paths.
*
* If given an absolute path, will return that filename directly. Otherwise,
* it will prepend the cwd.
*
* @private
* @param {String} fn
* @param {String} [cwd=process.cwd()]
* @return {String}
*/
function getPath(fn, cwd) {
cwd = cwd || process.cwd();
return isAbsolute(fn) ? fn : path.join(cwd, fn);
}
/**
* Gets linebreak.
*
* @private
* @param {String} lineBreaks
* @return {String}
*/
function getLinebreak(lineBreaks) {
if (lineBreaks && lineBreaks.toLowerCase() === 'crlf') {
return '\r\n';
} else {
return '\n';
}
}
/**
* Prepend a banner/license comment to a file.
*
* @function bannerize
* @param {String|Array} patterns
* A string or array of glob pattern(s) to which to apply the banner.
* @param {Object} [options]
* An object containing optional values.
* @param {String} [options.banner="banner.ejs"]
* A path to a custom banner template file.
* @param {String} [options.cwd=process.cwd()]
* Customize where relative patterns should match from.
* @param {String} [options.lineBreak="LF"]
* Set linebreaks ("LF" or "CRLF")
* @return {Promise}
* A promise which resolves with an array of all the files that were
* modified.
*/
module.exports = function bannerize(patterns, options) {
options = options || {};
var date = new Date();
var cwd = getPath(options.cwd || process.cwd());
var pkg = require(getPath('package.json', findRoot(cwd)));
var banner = getPath(options.banner || 'banner.ejs', cwd);
return globby(normalize(patterns), {cwd: cwd}).then(function (files) {
banner = render(banner, {pkg: pkg, date: date});
return flatten(files).map(function (file) {
file = getPath(file, cwd);
fs.writeFileSync(
file,
[banner, stripBom(fs.readFileSync(file, {encoding: 'utf8'}))].
join(getLinebreak(options.lineBreaks || options.lineBreak)));
return file;
});
});
};