@@ -334,32 +334,32 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
334334 return nullptr ;
335335}
336336
337- void BindingData::GetNearestParentPackageJSON (
338- const v8::FunctionCallbackInfo<v8::Value>& args) {
339- CHECK_GE (args.Length (), 1 );
340- CHECK (args[0 ]->IsString ());
341-
342- Realm* realm = Realm::GetCurrent (args);
343- BufferValue path_value (realm->isolate (), args[0 ]);
337+ const std::filesystem::path BindingData::NormalizePath (
338+ Realm* realm, BufferValue* path_value) {
344339 // Check if the path has a trailing slash. If so, add it after
345340 // ToNamespacedPath() as it will be deleted by ToNamespacedPath()
346- bool slashCheck = path_value. ToStringView ().ends_with (kPathSeparator );
341+ bool slashCheck = path_value-> ToStringView ().ends_with (kPathSeparator );
347342
348- ToNamespacedPath (realm->env (), &path_value);
343+ ToNamespacedPath (realm->env (), path_value);
344+
345+ auto path = path_value->ToPath ();
349346
350- std::string path_value_str = path_value.ToString ();
351347 if (slashCheck) {
352- path_value_str. push_back ( kPathSeparator ) ;
348+ path /= " " ;
353349 }
354350
355- std::filesystem::path path;
351+ return path;
352+ }
356353
357- #ifdef _WIN32
358- std::wstring wide_path = ConvertToWideString (path_value_str, GetACP ());
359- path = std::filesystem::path (wide_path);
360- #else
361- path = std::filesystem::path (path_value_str);
362- #endif
354+ void BindingData::GetNearestParentPackageJSON (
355+ const v8::FunctionCallbackInfo<v8::Value>& args) {
356+ CHECK_GE (args.Length (), 1 );
357+ CHECK (args[0 ]->IsString ());
358+
359+ Realm* realm = Realm::GetCurrent (args);
360+ BufferValue path_value (realm->isolate (), args[0 ]);
361+
362+ auto path = NormalizePath (realm, &path_value);
363363
364364 auto package_json = TraverseParent (realm, path);
365365
@@ -375,17 +375,8 @@ void BindingData::GetNearestParentPackageJSONType(
375375
376376 Realm* realm = Realm::GetCurrent (args);
377377 BufferValue path_value (realm->isolate (), args[0 ]);
378- // Check if the path has a trailing slash. If so, add it after
379- // ToNamespacedPath() as it will be deleted by ToNamespacedPath()
380- bool slashCheck = path_value.ToStringView ().ends_with (kPathSeparator );
381-
382- ToNamespacedPath (realm->env (), &path_value);
383-
384- auto path = path_value.ToPath ();
385378
386- if (slashCheck) {
387- path /= " " ;
388- }
379+ auto path = NormalizePath (realm, &path_value);
389380
390381 auto package_json = TraverseParent (realm, path);
391382
0 commit comments