@@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType(
404404 }
405405}
406406
407+ template <bool return_only_type>
407408void BindingData::GetPackageScopeConfig (
408409 const FunctionCallbackInfo<Value>& args) {
409410 CHECK_GE (args.Length (), 1 );
@@ -442,6 +443,13 @@ void BindingData::GetPackageScopeConfig(
442443 error_context.specifier = resolved.ToString ();
443444 auto package_json = GetPackageJSON (realm, *file_url, &error_context);
444445 if (package_json != nullptr ) {
446+ if constexpr (return_only_type) {
447+ Local<Value> value;
448+ if (ToV8Value (realm->context (), package_json->type ).ToLocal (&value)) {
449+ args.GetReturnValue ().Set (value);
450+ }
451+ return ;
452+ }
445453 return args.GetReturnValue ().Set (package_json->Serialize (realm));
446454 }
447455
@@ -460,6 +468,12 @@ void BindingData::GetPackageScopeConfig(
460468 }
461469 }
462470
471+ if constexpr (return_only_type) {
472+ return ;
473+ }
474+
475+ // If the package.json could not be found return a string containing a path
476+ // to the non-existent package.json file in the initial requested location
463477 auto package_json_url_as_path =
464478 url::FileURLToPath (realm->env (), *package_json_url);
465479 CHECK (package_json_url_as_path);
@@ -604,7 +618,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
604618 target,
605619 " getNearestParentPackageJSON" ,
606620 GetNearestParentPackageJSON);
607- SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
621+ SetMethod (
622+ isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig<false >);
623+ SetMethod (isolate, target, " getPackageType" , GetPackageScopeConfig<true >);
608624 SetMethod (isolate, target, " enableCompileCache" , EnableCompileCache);
609625 SetMethod (isolate, target, " getCompileCacheDir" , GetCompileCacheDir);
610626 SetMethod (isolate, target, " flushCompileCache" , FlushCompileCache);
@@ -658,7 +674,8 @@ void BindingData::RegisterExternalReferences(
658674 registry->Register (ReadPackageJSON);
659675 registry->Register (GetNearestParentPackageJSONType);
660676 registry->Register (GetNearestParentPackageJSON);
661- registry->Register (GetPackageScopeConfig);
677+ registry->Register (GetPackageScopeConfig<false >);
678+ registry->Register (GetPackageScopeConfig<true >);
662679 registry->Register (EnableCompileCache);
663680 registry->Register (GetCompileCacheDir);
664681 registry->Register (FlushCompileCache);
0 commit comments