-
Notifications
You must be signed in to change notification settings - Fork 6
/
licensor
executable file
·226 lines (199 loc) · 5.45 KB
/
licensor
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
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env node
/*
Copyright 2016 Kyle E. Mitchell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var fs = require('fs')
var path = require('path')
var docopt = require('docopt')
var wrap = require('word-wrap')
var options = docopt.docopt(
[
'License an npm package.',
'',
'Usage:',
' licensor [<SPDX-Identifier>] [--width=COLUMNS] [--notice]',
' licensor -h | --help',
' licensor -v | --version',
'',
'Options:',
' -h, --help Show this screen.',
' -v, --version Show version.',
' -w COLUMNS, --width=COLUMNS Wrap license text.',
' -n, --notice Write an Apache-2.0 NOTICE file.'
].join('\n') + '\n',
{
version: require('./package.json').version,
help: true
}
)
var cwd = process.cwd()
var packageJSON = path.join(cwd, 'package.json')
var packageData
try {
packageData = fs.readFileSync(packageJSON)
} catch (error) {
process.stderr.write('Cannot read package.json')
process.exit(1)
}
var json
try {
json = JSON.parse(packageData)
} catch (error) {
process.stderr.write('Invalid package.json')
process.exit(1)
}
require('normalize-package-data')(json)
var patternLicenses = [
'0BSD',
'BSD-2-Clause',
'BSD-3-Clause',
'ISC',
'MIT',
'UPL-1.0',
'WTFPL'
]
var fixedLicenses = [
'Apache-2.0',
'BlueOak-1.0.0',
'CC0-1.0'
]
var license = options['<SPDX-Identifier>'] || json.license
if (!license || typeof license !== 'string') {
process.stderr.write('No license property')
process.exit(1)
}
var packagePrefix = false
if (patternLicenses.indexOf(license) > -1) {
packagePrefix = 'jslicense-'
} else if (fixedLicenses.indexOf(license) > -1) {
packagePrefix = ''
}
if (packagePrefix === false) {
process.stderr.write('Unsupported license')
process.exit(1)
}
if (options['<SPDX-Identifier>']) {
var preserveIndent = require('json-preserve-indent')
var revised = preserveIndent(packageData)
revised.set('license', license)
fs.writeFileSync(packageJSON, revised.format())
}
var licensePath = path.join(cwd, 'LICENSE')
var wrapText = options['--width']
? function (x) {
return wrap(x, {
width: parseInt(options['--width']),
indent: '',
trim: true,
newline: '\n'
})
}
: function (x) { return x }
fs.writeFileSync(
licensePath,
licenseText(license.toLowerCase())
)
if (license === 'Apache-2.0') {
var year = fieldFromPackage('year', json)
var owners = fieldFromPackage('owners', json)
if (options['--notice']) {
var url = fieldFromPackage('url', json)
fs.writeFileSync(
path.join(cwd, 'NOTICE'),
fieldFromPackage('name', json) + '\n' +
(url ? url + '\n' : '') +
'Copyright (c) ' + year + ' ' + owners + '\n'
)
}
var header = []
.concat('/*')
.concat(
require('apache-2.0-header')
.trim()
.replace('[yyyy]', year)
.replace('[name of copyright owner]', owners)
.split('\n')
)
.concat('*/')
var licenseURL = 'http://www.apache.org/licenses/LICENSE-2.0'
var probablyHasHeader = function (fileContent) {
return (
fileContent.indexOf('Copyright') > -1 &&
fileContent.indexOf(licenseURL) > -1
)
}
var startsWithShebang = function (fileContent) {
return fileContent.indexOf('#!') === 0
}
require('glob')
.sync('**/*.{js,mjs}', {ignore: 'node_modules/**/*'})
.forEach(function (jsFile) {
var content = fs.readFileSync(jsFile).toString()
if (!probablyHasHeader(content)) {
var lineEnding = content.indexOf('\r\n') === -1 ? '\n' : '\r\n'
var newContent
if (startsWithShebang(content)) {
var lines = content.split(/\r?\n/)
newContent = []
.concat(lines[0])
.concat(header)
.concat(lines.slice(1))
.join(lineEnding)
} else {
newContent = header.join(lineEnding) + lineEnding + content
}
fs.writeFileSync(jsFile, newContent)
}
})
}
function licenseText (id) {
if (packagePrefix === 'jslicense-') {
return (
wrapText(
require(packagePrefix + license.toLowerCase())
.map(function (lineArray) {
return lineArray.reduce(function (line, lineItem) {
if (typeof lineItem === 'string') {
return line + lineItem
} else {
return (
line +
fieldFromPackage(
lineItem.field,
json,
lineItem.default
)
)
}
}, '')
})
.join('\n\n')
) +
'\n'
)
} else {
return require(id)
}
}
function fieldFromPackage (field, pkg/*, defaultValue */) {
if (field === 'url') {
return pkg.homepage
} else if (field === 'name') {
return pkg.name
} else if (field === 'owners') {
return pkg.author.name
} else if (field === 'year') {
return new Date()
.getFullYear()
.toString()
}
}