Skip to content

Commit 7f1cab8

Browse files
committed
fix(a11y): guard missing scripts object in update-max-warnings script
Make pkg.scripts optional and guard before indexing lint:a11y, so a package.json without a scripts object hits the explicit error path instead of throwing a TypeError. Signed-off-by: mschwab <mschwab@nvidia.com>
1 parent e916175 commit 7f1cab8

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

web/packages/common/scripts/update-max-warnings-a11y.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import path from 'path';
99
async function main() {
1010
const pkgPath = path.resolve(process.cwd(), 'package.json');
1111
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as {
12-
scripts: Record<string, string>;
12+
scripts?: Record<string, string>;
1313
};
1414

15-
const a11yScript: string = pkg.scripts['lint:a11y'];
16-
if (!a11yScript) {
15+
const scripts = pkg.scripts;
16+
const a11yScript = scripts?.['lint:a11y'];
17+
if (!scripts || !a11yScript) {
1718
console.error('No lint:a11y script found in package.json');
1819
process.exit(1);
1920
}
@@ -35,10 +36,7 @@ async function main() {
3536
const warningCount: number = results.reduce((sum, result) => sum + result.warningCount, 0);
3637

3738
if (warningCount !== currentMax) {
38-
pkg.scripts['lint:a11y'] = a11yScript.replace(
39-
maxWarningsRegex,
40-
`--max-warnings ${warningCount}`
41-
);
39+
scripts['lint:a11y'] = a11yScript.replace(maxWarningsRegex, `--max-warnings ${warningCount}`);
4240
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
4341
// eslint-disable-next-line no-console
4442
console.log(`Updated lint:a11y max-warnings from ${currentMax} to ${warningCount}`);

0 commit comments

Comments
 (0)