-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyLicenseGenerator.js
96 lines (88 loc) · 3.98 KB
/
DependencyLicenseGenerator.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
const async = require('async')
const fs = require('fs')
const addPackagesToIndex = require('license-report/lib/addPackagesToIndex')
const getPackageReportData = require('license-report/lib/getPackageReportData.js')
const packageDataToReportData = require('license-report/lib/packageDataToReportData')
const getInstalledVersions = require('license-report/lib/getInstalledVersions.js')
const tabelify = require('@kessler/tableify')
const packageJson = require('./package.json')
const packageLockJson = require('./package-lock.json')
const config = {
fields: ['name', 'author', 'licenseType', 'link', 'installedVersion', 'remoteVersion'].reverse(),
name: {label: 'Name'},
author: {label: 'Author'},
licenseType: {label: 'License Type'},
link: {label: 'Link'},
installedVersion: {label: 'Installed Version'},
remoteVersion: {label: 'Available Version'},
}
const deps = packageJson.dependencies
const devDeps = packageJson.devDependencies
let depsIndex = []
addPackagesToIndex(deps, depsIndex, [])
addPackagesToIndex(devDeps, depsIndex, [])
const installedVersions = getInstalledVersions(packageLockJson, depsIndex)
async function myGetPackageReportData(packageEntry) {
return await getPackageReportData(packageEntry, installedVersions)
}
async.map(depsIndex, myGetPackageReportData, function(err, results) {
if (err) return console.error(err)
if (results.length === 0) return console.log('nothing to do')
try {
let packagesData = results.map(element => packageDataToReportData(element, config))
packagesData = packagesData.map(row => renameRowsProperties(row, config))
let html = `<html>
<head>
<title>License and dependencies for project ${packageJson.name}</title>
</head>
<body>
<style>
${fs.readFileSync('node_modules/license-report/defaultHtmlStyle.css')}
</style>
<h2>Project ${packageJson.name}</h2>
Version: ${packageJson.version}<br/>
Author: ${packageJson.author.name}<br/>
${packageJson.contributors.map(contributor => `Contributor: ${contributor.name}</br>`)}
Source: <a href="${packageJson.repository.url}">${packageJson.repository.url}</a></br>
License: ${packageJson.license}<br/>
<a href="/api-docs">See here for the documentation about this API</a><br/>
<pre>${fs.readFileSync('LICENSE')}</pre>
<h2>Search engine</h2>
<img alt="NoSketchEngine" src="NoSkE_logo.png" style="float: left;"/><p>This API uses the <a href="https://nlp.fi.muni.cz/trac/noske">NoSketchEngine https://nlp.fi.muni.cz/trac/noske</a> for searching.<br/>
The ACDH-CH provides a container image <a href="https://hub.docker.com/r/acdhch/noske">acdhch/noske:4.24.6-2.167.10-open</a> for NoSketchEngine</p>
<p>Credits:<br/>
Kilgarriff, Adam; et al. 2014. “The Sketch Engine: ten years on”. <i>Lexicography 1(1)</i>, 7-36.<br/>
Rychlý, Pavel. 2007. “Manatee/Bonito: a modular corpus manager”. <i>RASLAN</i>, 65-70.</p>
<h2 style="clear: left;">Dependencies</h2>
${tabelify(packagesData)}
</body>
</html>`
console.log(html)
} catch (e) {
console.error(e.stack)
process.exit(1)
}
})
/**
* Rename the property of an object
* @param {string} oldProp - old name of the property
* @param {string} newProp - new name of the property
* @param {object} anonymous - object with the property to be renamed
* @returns {object} object with the renamed property
*/
function renameProp(oldProp, newProp, { [oldProp]: old, ...others }) {
const newObject = {[newProp]:old, ...others}
return newObject
}
/**
* Rename each property of row with the value of its label from config
* @param {object} row - object with data of one row to be displayed
* @param {object} config - configuration object
*/
function renameRowsProperties(row, config) {
let renamedRow = row
for (const fieldname of config.fields) {
renamedRow = renameProp(fieldname, config[fieldname].label, renamedRow)
}
return renamedRow
}