Skip to content

Commit 99535de

Browse files
committed
Update check_licenses.js script
1 parent ed4c3ae commit 99535de

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

scripts/check_licenses.js

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,98 @@
1+
/* eslint-disable no-console */
12
'use strict'
23

3-
const fs = require('fs')
4-
const path = require('path')
5-
const readline = require('readline')
6-
const pkg = require(path.join(__dirname, '..', '/package.json'))
4+
const { createReadStream } = require('node:fs')
5+
const { join } = require('node:path')
6+
const readline = require('node:readline')
7+
const { execSync } = require('node:child_process')
8+
const { name: rootPackageName } = require('../package.json')
79

8-
const filePath = path.join(__dirname, '..', '/LICENSE-3rdparty.csv')
9-
const deps = new Set(Object.keys(pkg.dependencies || {}))
10-
const devDeps = new Set(Object.keys(pkg.devDependencies || {}))
11-
12-
let index = 0
13-
14-
const licenses = {
15-
require: new Set(),
16-
dev: new Set(),
17-
file: new Set()
18-
}
10+
const filePath = join(__dirname, '..', 'LICENSE-3rdparty.csv')
11+
const deps = getProdDeps()
12+
const licenses = new Set()
13+
let isHeader = true
1914

2015
const lineReader = readline.createInterface({
21-
input: fs.createReadStream(filePath)
16+
input: createReadStream(filePath)
2217
})
2318

2419
lineReader.on('line', line => {
25-
if (index !== 0) {
26-
const columns = line.split(',')
27-
const type = columns[0]
28-
const license = columns[1]
29-
30-
licenses[type].add(license)
20+
if (isHeader) {
21+
isHeader = false
22+
return
3123
}
3224

33-
index++
25+
const trimmed = line.trim()
26+
if (!trimmed) return // Skip empty lines
27+
const columns = line.split(',')
28+
const component = columns[0]
29+
30+
// Strip quotes from the component name
31+
licenses.add(component.replaceAll(/^"|"$/g, ''))
3432
})
3533

3634
lineReader.on('close', () => {
37-
if (!checkLicenses(deps, 'require') || !checkLicenses(devDeps, 'dev')) {
35+
if (!checkLicenses(deps)) {
3836
process.exit(1)
3937
}
4038
})
4139

42-
function checkLicenses (typeDeps, type) {
43-
/* eslint-disable no-console */
40+
function getProdDeps () {
41+
// Add root package (dd-trace) to the set of dependencies manually as it is not included in the yarn list output.
42+
const deps = new Set([rootPackageName])
43+
44+
// Use yarn to get full tree of production (non-dev) dependencies (format is ndjson)
45+
const stdout = execSync('yarn list --production --json', {
46+
encoding: 'utf8',
47+
stdio: ['ignore', 'pipe', 'inherit']
48+
})
49+
50+
for (const line of stdout.split('\n')) {
51+
if (!line) continue
52+
const parsed = JSON.parse(line)
53+
if (parsed.type === 'tree' && parsed.data && Array.isArray(parsed.data.trees)) {
54+
collectFromTrees(parsed.data.trees, deps)
55+
}
56+
}
57+
58+
return deps
59+
}
60+
61+
function collectFromTrees (trees, deps) {
62+
for (const node of trees) {
63+
if (typeof node?.name !== 'string') continue
64+
65+
// Remove version from the package name (e.g. `@protobufjs/pool@1.1.0` -> `@protobufjs/pool`)
66+
deps.add(node.name.slice(0, node.name.lastIndexOf('@')))
67+
68+
if (Array.isArray(node.children) && node.children.length) {
69+
collectFromTrees(node.children, deps)
70+
}
71+
}
72+
}
4473

74+
function checkLicenses (typeDeps) {
4575
const missing = []
4676
const extraneous = []
4777

4878
for (const dep of typeDeps) {
49-
if (!licenses[type].has(dep)) {
79+
if (!licenses.has(dep)) {
5080
missing.push(dep)
5181
}
5282
}
5383

54-
for (const dep of licenses[type]) {
84+
for (const dep of licenses) {
5585
if (!typeDeps.has(dep)) {
5686
extraneous.push(dep)
5787
}
5888
}
5989

6090
if (missing.length) {
61-
console.log(`Missing 3rd-party license for ${missing.join(', ')}.`)
91+
console.error(`Missing 3rd-party license for ${missing.join(', ')}.`)
6292
}
6393

6494
if (extraneous.length) {
65-
console.log(`Extraneous 3rd-party license for ${extraneous.join(', ')}.`)
95+
console.error(`Extraneous 3rd-party license for ${extraneous.join(', ')}.`)
6696
}
6797

6898
return missing.length === 0 && extraneous.length === 0

0 commit comments

Comments
 (0)