@@ -219,6 +219,21 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
219
219
if (field_value == " commonjs" || field_value == " module" ) {
220
220
package_config.type = field_value;
221
221
}
222
+ } else if (key == " scripts" ) {
223
+ if (value.type ().get (field_type)) {
224
+ return throw_invalid_package_config ();
225
+ }
226
+ switch (field_type) {
227
+ case simdjson::ondemand::json_type::object: {
228
+ if (value.raw_json ().get (field_value)) {
229
+ return throw_invalid_package_config ();
230
+ }
231
+ package_config.scripts = field_value;
232
+ break ;
233
+ }
234
+ default :
235
+ break ;
236
+ }
222
237
}
223
238
}
224
239
// package_config could be quite large, so we should move it instead of
@@ -344,6 +359,33 @@ void BindingData::GetNearestParentPackageJSONType(
344
359
args.GetReturnValue ().Set (Array::New (realm->isolate (), values, 3 ));
345
360
}
346
361
362
+ void BindingData::GetPackageJSONScripts (
363
+ const FunctionCallbackInfo<Value>& args) {
364
+ CHECK_EQ (args.Length (), 1 );
365
+ CHECK (args[0 ]->IsString ());
366
+
367
+ Realm* realm = Realm::GetCurrent (args);
368
+ Utf8Value path (realm->isolate (), args[0 ]);
369
+
370
+ THROW_IF_INSUFFICIENT_PERMISSIONS (
371
+ realm->env (),
372
+ permission::PermissionScope::kFileSystemRead ,
373
+ path.ToStringView ());
374
+
375
+ auto package_json = GetPackageJSON (realm, path.ToString ());
376
+ if (package_json == nullptr ) {
377
+ printf (" Can't read package.json\n " );
378
+ return ;
379
+ } else if (!package_json->scripts .has_value ()) {
380
+ printf (" Package.json scripts doesn't exist or parseable\n " );
381
+ return ;
382
+ }
383
+
384
+ args.GetReturnValue ().Set (
385
+ ToV8Value (realm->context (), package_json->scripts .value ())
386
+ .ToLocalChecked ());
387
+ }
388
+
347
389
void BindingData::GetPackageScopeConfig (
348
390
const FunctionCallbackInfo<Value>& args) {
349
391
CHECK_GE (args.Length (), 1 );
@@ -424,6 +466,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
424
466
" getNearestParentPackageJSON" ,
425
467
GetNearestParentPackageJSON);
426
468
SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
469
+ SetMethod (isolate, target, " getPackageJSONScripts" , GetPackageJSONScripts);
427
470
}
428
471
429
472
void BindingData::CreatePerContextProperties (Local<Object> target,
@@ -440,6 +483,7 @@ void BindingData::RegisterExternalReferences(
440
483
registry->Register (GetNearestParentPackageJSONType);
441
484
registry->Register (GetNearestParentPackageJSON);
442
485
registry->Register (GetPackageScopeConfig);
486
+ registry->Register (GetPackageJSONScripts);
443
487
}
444
488
445
489
} // namespace modules
0 commit comments