forked from kessler/license-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (51 loc) · 2.13 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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import createDebugMessages from 'debug';
import config from './lib/config.js';
import getFormatter from './lib/getFormatter.js';
import addLocalPackageData from './lib/addLocalPackageData.js';
import addPackageDataFromRepository from './lib/addPackageDataFromRepository.js';
import getDependencies from './lib/getDependencies.js';
import packageDataToReportData from './lib/packageDataToReportData.js';
import util from './lib/util.js';
const debug = createDebugMessages('license-report');
(async () => {
if (config.help) {
console.log(util.helpText)
return
}
if (!config.package) {
config.package = './package.json'
}
if (path.extname(config.package) !== '.json') {
throw new Error('invalid package.json ' + config.package)
}
const outputFormatter = getFormatter(config.output)
try {
const resolvedPackageJson = path.resolve(process.cwd(), config.package)
debug('loading %s', resolvedPackageJson)
let packageJson
if (fs.existsSync(resolvedPackageJson)) {
packageJson = await util.readJson(resolvedPackageJson)
} else {
throw new Error(`Warning: the file '${resolvedPackageJson}' is required to get installed versions of packages`)
}
// Get a list of all the dependencies we want information about.
const inclusions = util.isNullOrUndefined(config.only) ? null : config.only.split(',')
const exclusions = Array.isArray(config.exclude) ? config.exclude : [config.exclude]
let depsIndex = getDependencies(packageJson, exclusions, inclusions)
const projectRootPath = path.dirname(resolvedPackageJson)
const packagesData = await Promise.all(
depsIndex.map(async (element) => {
const localDataForPackage = await addLocalPackageData(element, projectRootPath, config.fields)
const completeDataForPackage = await addPackageDataFromRepository(localDataForPackage)
return packageDataToReportData(completeDataForPackage, config)
})
)
console.log(outputFormatter(packagesData, config))
} catch (e) {
console.error(e.stack)
process.exit(1)
}
})();