/
index.js
50 lines (38 loc) · 1.28 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
const path = require('path')
const fs = require('fs')
function assertStr (str, name) {
if (typeof str !== 'string') {
throw new Error(`Expecting ${name} to be string`)
}
}
const licenses = fs
.readdirSync(__dirname + '/licenses')
.map((license) => license.replace(/\.[0-9a-z]+$/i, ''))
function createLicense (where, license, opts) {
license = typeof license === 'string' ? license.toLowerCase() : null
if (opts && typeof opts !== 'object' && !!opts.prototype) {
throw new Error(`Expecting opts to be object`)
}
assertStr(where, 'where')
if (licenses.indexOf(license) === -1) {
throw new Error('Unknown license type ' + license)
}
if (typeof opts.year !== 'string' && typeof opts.year !== 'number') {
throw new Error('Expecting opts.year to be number or string')
}
assertStr(opts.author, 'opts.author')
assertStr(opts.project, 'opts.project')
const license_file = __dirname + '/licenses/' + license + '.txt'
const data = fs.readFileSync(license_file, 'utf8')
const result = data
.replace(/\[year\]/g, opts.year)
.replace(/\[fullname\]/g, opts.author)
.replace(/\[project\]/g, opts.project)
fs.writeFileSync(
path.resolve(where, 'LICENSE'),
result,
'utf8'
)
}
module.exports = createLicense
module.exports.licenses = licenses