File tree Expand file tree Collapse file tree
packages/stack-cli/src/commands Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -124,12 +124,20 @@ type PackageJsonRead =
124124 | { kind : "missing" }
125125 | { kind : "invalid" , error : string } ;
126126
127+ function isPackageJson ( value : unknown ) : value is PackageJson {
128+ return value !== null && typeof value === "object" && ! Array . isArray ( value ) ;
129+ }
130+
127131function readPackageJson ( projectDir : string ) : PackageJsonRead {
128132 const pkgPath = path . join ( projectDir , "package.json" ) ;
129133 if ( ! fs . existsSync ( pkgPath ) ) return { kind : "missing" } ;
130134 const raw = fs . readFileSync ( pkgPath , "utf-8" ) ;
131135 try {
132- return { kind : "ok" , value : JSON . parse ( raw ) as PackageJson } ;
136+ const parsed : unknown = JSON . parse ( raw ) ;
137+ if ( ! isPackageJson ( parsed ) ) {
138+ return { kind : "invalid" , error : "package.json must be a JSON object." } ;
139+ }
140+ return { kind : "ok" , value : parsed } ;
133141 } catch ( error ) {
134142 if ( error instanceof SyntaxError ) {
135143 return { kind : "invalid" , error : error . message } ;
You can’t perform that action at this time.
0 commit comments