forked from zumwald/oss-attribution-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (271 loc) · 11.1 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env node
// usage
var yargs = require('yargs')
.usage('Calculate the npm and bower modules used in this project and generate a third-party attribution (credits) text.',
{
outputDir: {
alias: 'o',
default: './oss-attribution'
},
baseDir: {
alias: 'b',
default: process.cwd()
}
})
.example('$0 -o ./tpn', 'run the tool and output text and backing json to ${projectRoot}/tpn directory.')
.example('$0 -b ./some/path/to/projectDir', 'run the tool for Bower/NPM projects in another directory.')
.example('$0 -o tpn -b ./some/path/to/projectDir', 'run the tool in some other directory and dump the output in a directory called "tpn" there.');
if (yargs.argv.help) {
yargs.showHelp();
process.exit(1);
}
// dependencies
var bluebird = require('bluebird');
var _ = require('lodash');
var npmchecker = require('license-checker');
var bower = require('bower');
var path = require('path');
var jetpack = require('fs-jetpack');
var cp = require('child_process');
var os = require('os');
// const
var licenseCheckerCustomFormat = {
name: '',
version: '',
description: '',
repository: '',
publisher: '',
email: '',
url: '',
licenses: '',
licenseFile: '',
licenseModified: false
}
/**
* Helpers
*/
function getAttributionForAuthor(a) {
return _.isString(a) ? a : a.name + ((a.email || a.homepage || a.url) ? ` <${a.email || a.homepage || a.url}>` : '');
}
function getNpmLicenses() {
// first - check that this is even a bower project
if (!jetpack.exists(path.join(options.baseDir, 'package.json'))) {
console.log('this does not look like an NPM project, skipping NPM checks.');
return [];
}
return bluebird.fromCallback((cb) => {
return npmchecker.init({
start: options.baseDir,
production: true,
customFormat: licenseCheckerCustomFormat
}, cb);
})
.then((result) => {
// we want to exclude the top-level project from being included
var topLevelProjectInfo = jetpack.read(path.join(options.baseDir, 'package.json'), 'json');
var keys = Object.getOwnPropertyNames(result).filter((k) => {
return k !== `${topLevelProjectInfo.name}@${topLevelProjectInfo.version}`;
});
return bluebird.map(keys, (key) => {
console.log('processing', key);
var package = result[key];
return jetpack.findAsync(options.baseDir, {
matching: `**/node_modules/${package.name}`,
directories: true,
files: false
})
.then((hits) => {
var pathToExport = '';
if (hits && hits.length && hits.length > 0) {
pathToExport = path.resolve(hits[0].trim());
if (jetpack.exists(pathToExport)) {
return pathToExport;
}
}
// probably a core module, take a guess at it's path
var possiblePath = path.resolve(path.join(options.baseDir, 'node_modules', package.name));
return jetpack.exists(possiblePath) ? possiblePath : resolution;
})
.then((packagePath) => {
var packageJsonPath = path.join(packagePath, 'package.json');
return jetpack.read(packageJsonPath, 'json');
})
.then((packageJson) => {
console.log('processing', packageJson.name, 'for authors and licenseText');
var props = {};
props.authors = packageJson.author && getAttributionForAuthor(packageJson.author)
|| (packageJson.contributors && packageJson.contributors.map((c) => {
return getAttributionForAuthor(c);
}).join(', '))
|| (packageJson.maintainers && packageJson.maintainers.map((m) => {
return getAttributionForAuthor(m);
}).join(', '));
props.licenseText = package.licenseFile && jetpack.exists(package.licenseFile) ? jetpack.read(package.licenseFile) : '';
return props;
})
.catch(e => {
console.warn('error processing', package.name, '-- missing author and license text fields');
return {
authors: '',
licenseText: ''
};
})
.then(derivedProps => {
return {
ignore: false,
name: package.name,
version: package.version,
authors: derivedProps.authors,
url: package.repository,
license: package.licenses,
licenseText: derivedProps.licenseText
};
});
});
});
}
/**
* TL;DR - normalizing the output format for NPM & Bower license info
*
* The output from license-checker gives us what we need:
* - component name
* - version
* - authors (note: not returned by license-checker, we have to apply our heuristic)
* - url
* - license(s)
* - license contents OR license snippet (in case of license embedded in markdown)
*
* Where we calculate the license information manually for Bower components,
* we'll return an object with these properties.
*/
function getBowerLicenses() {
// first - check that this is even a bower project
if (!jetpack.exists(path.join(options.baseDir, 'bower.json'))) {
console.log('this does not look like a Bower project, skipping Bower checks.');
return [];
}
bower.config.cwd = options.baseDir;
var bowerComponentsDir = path.join(bower.config.cwd, bower.config.directory);
return jetpack.inspectTreeAsync(bowerComponentsDir, { relativePath: true })
.then((result) => {
/**
* for each component, try to calculate the license from the NPM package info
* if it is a available because license-checker more closely aligns with our
* objective.
*/
return bluebird.map(result.children, (component) => {
var absPath = path.join(bowerComponentsDir, component.relativePath);
// npm license check didn't work
// try to get the license and package info from .bower.json first
// because it has more metadata than the plain bower.json
return jetpack.readAsync(path.join(absPath, '.bower.json'), 'json')
.catch(() => {
return jetpack.readAsync(path.join(absPath, 'bower.json'), 'json');
})
.then((package) => {
console.log('processing', package.name);
// assumptions here based on https://github.com/bower/spec/blob/master/json.md
// extract necessary properties as described in TL;DR above
var url = package['_source']
|| (package.repository && package.repository.url)
|| package.url
|| package.homepage;
var authors = '';
if (package.authors) {
authors = _.map(package.authors, (a) => {
return getAttributionForAuthor(a);
}).join(', ');
} else {
// extrapolate author from url if it's a git repository
var githubMatch = url.match(/github\.com\/.*\//);
if (githubMatch) {
authors = githubMatch[0].replace('github.com', '').replace(/\//g, '');
}
}
// normalize the license object
package.license = package.license || package.licenses;
var licenses = package.license && _.isString(package.license) ? package.license
: (_.isArray(package.license) ? package.license.join(',') : package.licenses);
// find the license file if it exists
var licensePath = _.find(component.children, (c) => {
return /licen[cs]e/i.test(c.name);
});
var licenseText = null;
if (licensePath) {
licenseText = jetpack.read(path.join(bowerComponentsDir, licensePath.relativePath));
}
return {
ignore: false,
name: package.name,
version: package.version || package['_release'],
authors: authors,
url: url,
license: licenses,
licenseText: licenseText
};
});
});
});
}
/***********************
*
* MAIN
*
***********************/
// sanitize inputs
var options = {
baseDir: path.resolve(yargs.argv.baseDir),
outputDir: path.resolve(path.join(yargs.argv.baseDir, yargs.argv.outputDir))
};
bluebird.all([
getNpmLicenses(),
getBowerLicenses()
])
.catch((err) => {
console.log(err);
process.exit(1);
})
.spread((npmOutput, bowerOutput) => {
var o = {};
_.concat(npmOutput, bowerOutput).forEach((v) => {
o[v.name] = v;
});
var userOverridesPath = path.join(options.outputDir, 'overrides.json');
if (jetpack.exists(userOverridesPath)) {
var userOverrides = jetpack.read(userOverridesPath, 'json');
console.log('using overrides:', userOverrides);
// foreach override, loop through the properties and assign them to the base object.
o = _.defaultsDeep(userOverrides, o);
}
return o;
})
.catch(e => {
console.error('ERROR processing overrides', e);
process.exit(1);
})
.then((licenseInfos) => {
var attribution = _.filter(licenseInfos, licenseInfo => {
return !licenseInfo.ignore;
}).map(licenseInfo => {
return [
licenseInfo.name,
`${licenseInfo.version} <${licenseInfo.url}>`,
licenseInfo.licenseText || `license: ${licenseInfo.license}${os.EOL}authors: ${licenseInfo.authors}`
].join(os.EOL);
}).join(`${os.EOL}${os.EOL}******************************${os.EOL}${os.EOL}`);
var headerPath = path.join(options.outputDir, 'header.txt');
if (jetpack.exists(headerPath)) {
var template = jetpack.read(headerPath);
console.log('using template', template);
attribution = template + os.EOL + os.EOL + attribution;
}
return jetpack.write(path.join(options.outputDir, 'attribution.txt'), attribution);
})
.catch(e => {
console.error('ERROR writing attribution file', e);
process.exit(1);
})
.then(() => {
console.log('done');
process.exit();
});