feat: yarn workspaces: now handles both package.json formats for workspaces
@@ -35,12 +35,15 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson;
import com.synopsys.integration.detectable.detectables.yarn.packagejson.PackageJsonCurrent;
+import com.synopsys.integration.detectable.detectables.yarn.packagejson.PackageJsonPreV1_5_0;
import com.synopsys.integration.detectable.extraction.Extraction;
public class YarnLockExtractor {
@@ -60,10 +63,29 @@ public class YarnLockExtractor {
// Doesn't seem to belong here; maybe a new class for all the gson work?
// It's the same code here and in yarnPackager
Gson gson = new Gson();
- PackageJsonCurrent rootPackageJson = gson.fromJson(rootPackageJsonText, PackageJsonCurrent.class);
+ GsonBuilder builder = new GsonBuilder();
+ Map<String, Object> packageJsonMap = builder.create().fromJson(rootPackageJsonText, Map.class);
+ Object workspacesObject = packageJsonMap.get("workspaces");
+ List<String> workspaceSubdirPatterns = new LinkedList<>();
+ if (workspacesObject != null) {
+ System.out.printf("workspacesObject type: %s\n", workspacesObject.getClass().getName());
+ if (workspacesObject instanceof Map) {
+ System.out.printf("workspacesObject is a Map\n");
+ PackageJsonCurrent rootPackageJsonCurrent = gson.fromJson(rootPackageJsonText, PackageJsonCurrent.class);
+ // TODO pull workspaces out to a neutral format, like List<String>
+ workspaceSubdirPatterns.addAll(rootPackageJsonCurrent.workspaces.workspaceSubdirPatterns);
+ } else if (workspacesObject instanceof List) {
+ System.out.printf("workspacesObject is a List\n");
+ PackageJsonPreV1_5_0 rootPackageJsonPreV1_5_0 = gson.fromJson(rootPackageJsonText, PackageJsonPreV1_5_0.class);
+ // TODO pull workspaces out to a neutral format
+ workspaceSubdirPatterns.addAll(rootPackageJsonPreV1_5_0.workspaceSubdirPatterns);
+ } else {
+ System.out.printf("workspacesObject is something I don't understand\n");
+ }
+ }
+
// TODO factor this out:
- if (rootPackageJson.workspaces != null) {
- for (String workspaceSubDir : rootPackageJson.workspaces.workspaceSubdirPatterns) {
+ for (String workspaceSubDir : workspaceSubdirPatterns) {
System.out.printf("workspaceSubDir: %s\n", workspaceSubDir);
String globString = String.format("glob:%s/%s/package.json", sourceDir.getAbsolutePath(), workspaceSubDir);
System.out.printf("globString: %s\n", globString);
@@ -86,8 +108,10 @@ public class YarnLockExtractor {
}
});
}
- }
+
////////////////////////////////
+ // TODO any way to avoid re-reading the json here?
+ PackageJson rootPackageJson = gson.fromJson(rootPackageJsonText, PackageJson.class);
List<String> yarnLockLines = FileUtils.readLines(yarnLockFile, StandardCharsets.UTF_8);
YarnResult yarnResult = yarnPackager.generateYarnResult(rootPackageJson, workspacePackageJsons,
yarnLockLines, yarnLockFile.getAbsolutePath(), new ArrayList<>());
