feat: run test script for all pkgs with changed files

This commit is contained in:
Julien Fontanet 2022-11-22 17:28:50 +01:00
parent 83a7dd7ea1
commit 09a2f45ada
2 changed files with 29 additions and 0 deletions

View File

@ -70,6 +70,7 @@
"testRegex": "\\.spec\\.js$"
},
"lint-staged": {
"*": "scripts/run-changed-pkgs.js test",
"*.{{,c,m}j,t}s{,x}": [
"prettier --write",
"eslint --ignore-pattern '!*'",

28
scripts/run-changed-pkgs.js Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env node
'use strict'
const { join, relative, sep } = require('path')
const [, , script, ...files] = process.argv
const pkgs = new Set()
const root = join(__dirname, '..')
for (const file of files) {
const parts = relative(root, file).split(sep)
if ((parts.length > 2 && parts[0] === 'packages') || parts[0][0] === '@') {
pkgs.add(parts.slice(0, 2).join(sep))
}
}
if (pkgs.size !== 0) {
const args = ['run', '--if-present', script]
for (const pkg of pkgs) {
args.push('-w', pkg)
}
const { status } = require('child_process').spawnSync('npm', args, { stdio: 'inherit' })
if (status !== 0) {
process.exit(status)
}
}