Skip to content

Commit

Permalink
chore: fix license validate script
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Nov 29, 2023
1 parent 49ccb2b commit 5573239
Show file tree
Hide file tree
Showing 3 changed files with 851 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"graduate": "run bump --conventional-graduate",
"publish2": "run build && lerna publish from-package --pre-dist-tag next",
"validate:dependencies": "yarn npm audit && run license-validate",
"license-validate": "sofie-licensecheck --allowPackages \"@elgato-stream-deck/webhid-demo@$(node -p \"require('./packages/webhid-demo/package.json').version\");[email protected]\"",
"license-validate": "node ./scripts/license-check.mjs",
"g:tsc": "cd $INIT_CWD && tsc",
"g:rimraf": "cd $INIT_CWD && rimraf",
"g:husky": "./node_modules/.bin/husky",
Expand All @@ -41,6 +41,7 @@
"lerna": "^8.0.0",
"rimraf": "^5.0.5",
"sharp": "^0.33.0",
"snyk-nodejs-lockfile-parser": "^1.52.8",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "~4.9.5",
Expand Down
63 changes: 63 additions & 0 deletions scripts/license-check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import cp from 'child_process'
import fs from 'fs/promises'
import { buildDepTreeFromFiles } from 'snyk-nodejs-lockfile-parser'

const allDepVersions = new Map()

async function addDepsForRoot(root, subdir) {
const tree = await buildDepTreeFromFiles(
root,
subdir ? `packages/${subdir}/package.json` : 'package.json',
'yarn.lock',
true,
false
)

function flattenAndAddDeps(node) {
let entry = allDepVersions.get(node.name)
if (!entry) {
entry = new Set()
allDepVersions.set(node.name, entry)
}

if (!entry.has(node.version)) {
entry.add(node.version)

if (node.dependencies) {
for (const obj of Object.values(node.dependencies)) {
flattenAndAddDeps(obj)
}
}
}
}

flattenAndAddDeps(tree)
}

await addDepsForRoot('.')
for (const dirname of await fs.readdir(new URL('../packages', import.meta.url))) {
await addDepsForRoot('.', dirname)
}

const allowPackages = []
for (const [name, versions] of allDepVersions) {
if (name.startsWith('@img/sharp') || name === '@elgato-stream-deck/webhid-demo') {
for (const version of versions) {
allowPackages.push(`${name}@${version}`)
}
}
}

cp.exec(`yarn sofie-licensecheck --allowPackages "${allowPackages.join(';')}"`, (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`)
return
}

if (stderr) {
console.error(stderr)
return
}

console.log(stdout)
})
Loading

0 comments on commit 5573239

Please sign in to comment.