|
| 1 | +/* eslint-disable no-console */ |
1 | 2 | 'use strict' |
2 | 3 |
|
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') |
7 | 9 |
|
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 |
19 | 14 |
|
20 | 15 | const lineReader = readline.createInterface({ |
21 | | - input: fs.createReadStream(filePath) |
| 16 | + input: createReadStream(filePath) |
22 | 17 | }) |
23 | 18 |
|
24 | 19 | 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 |
31 | 23 | } |
32 | 24 |
|
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, '')) |
34 | 32 | }) |
35 | 33 |
|
36 | 34 | lineReader.on('close', () => { |
37 | | - if (!checkLicenses(deps, 'require') || !checkLicenses(devDeps, 'dev')) { |
| 35 | + if (!checkLicenses(deps)) { |
38 | 36 | process.exit(1) |
39 | 37 | } |
40 | 38 | }) |
41 | 39 |
|
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 | +} |
44 | 73 |
|
| 74 | +function checkLicenses (typeDeps) { |
45 | 75 | const missing = [] |
46 | 76 | const extraneous = [] |
47 | 77 |
|
48 | 78 | for (const dep of typeDeps) { |
49 | | - if (!licenses[type].has(dep)) { |
| 79 | + if (!licenses.has(dep)) { |
50 | 80 | missing.push(dep) |
51 | 81 | } |
52 | 82 | } |
53 | 83 |
|
54 | | - for (const dep of licenses[type]) { |
| 84 | + for (const dep of licenses) { |
55 | 85 | if (!typeDeps.has(dep)) { |
56 | 86 | extraneous.push(dep) |
57 | 87 | } |
58 | 88 | } |
59 | 89 |
|
60 | 90 | if (missing.length) { |
61 | | - console.log(`Missing 3rd-party license for ${missing.join(', ')}.`) |
| 91 | + console.error(`Missing 3rd-party license for ${missing.join(', ')}.`) |
62 | 92 | } |
63 | 93 |
|
64 | 94 | if (extraneous.length) { |
65 | | - console.log(`Extraneous 3rd-party license for ${extraneous.join(', ')}.`) |
| 95 | + console.error(`Extraneous 3rd-party license for ${extraneous.join(', ')}.`) |
66 | 96 | } |
67 | 97 |
|
68 | 98 | return missing.length === 0 && extraneous.length === 0 |
|
0 commit comments