11import fs from 'node:fs' ;
22
3- import { CommandInfo } from '../command.js' ;
3+ import type { CommandInfo } from '../command.js' ;
44import JSONC from '../jsonc.js' ;
5- import { escapeRegExp } from '../utils.js' ;
6- import { CommandParser } from './command-parser.js' ;
5+ import { escapeRegExp , isObject } from '../utils.js' ;
6+ import type { CommandParser } from './command-parser.js' ;
77
88// Matches a negative filter surrounded by '(!' and ')'.
99const OMISSION = / \( ! ( [ ^ ) ] + ) \) / ;
@@ -14,7 +14,7 @@ const OMISSION = /\(!([^)]+)\)/;
1414 * configuration files of the current directory.
1515 */
1616export class ExpandWildcard implements CommandParser {
17- static readDeno ( ) {
17+ static readDeno ( this : void ) : unknown {
1818 try {
1919 let json : string = '{}' ;
2020
@@ -30,7 +30,7 @@ export class ExpandWildcard implements CommandParser {
3030 }
3131 }
3232
33- static readPackage ( ) {
33+ static readPackage ( this : void ) : unknown {
3434 try {
3535 const json = fs . readFileSync ( 'package.json' , { encoding : 'utf-8' } ) ;
3636 return JSON . parse ( json ) ;
@@ -49,18 +49,23 @@ export class ExpandWildcard implements CommandParser {
4949
5050 private relevantScripts ( command : string ) : string [ ] {
5151 if ( ! this . packageScripts ) {
52- this . packageScripts = Object . keys ( this . readPackage ( ) . scripts || { } ) ;
52+ const content = this . readPackage ( ) ;
53+ const scripts =
54+ isObject ( content ) && isObject ( content . scripts ) ? Object . keys ( content . scripts ) : [ ] ;
55+
56+ this . packageScripts = scripts ;
5357 }
5458
5559 if ( command === 'deno task' ) {
5660 if ( ! this . denoTasks ) {
61+ const content = this . readDeno ( ) ;
62+ const tasks =
63+ isObject ( content ) && isObject ( content . tasks ) ? Object . keys ( content . tasks ) : [ ] ;
64+
5765 // If Deno tries to run a task that doesn't exist,
5866 // it can fall back to running a script with the same name.
5967 // Therefore, the actual list of tasks is the union of the tasks and scripts.
60- this . denoTasks = [
61- ...Object . keys ( this . readDeno ( ) . tasks || { } ) ,
62- ...this . packageScripts ,
63- ] ;
68+ this . denoTasks = [ ...tasks , ...this . packageScripts ] ;
6469 }
6570
6671 return this . denoTasks ;
0 commit comments