Skip to content

Commit

Permalink
support pnpm for project discovery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmews committed Jan 8, 2024
1 parent 7d45d1b commit 180bc4f
Showing 1 changed file with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@
*/
public class CreateProjectStructureUtils {

enum WorkspaceType {
Yarn, Pnpm
}

static class Folder {
final Folder parent;
final String folderName;
final boolean isWorkingDir;
final boolean isProject;
final boolean isPlainJS;
final String yarnWorkspacesFolder;
final WorkspaceType workspaceType;
final String workspacesFolder; // either yarn or pnpm
final String name;
final String dependencies;
final String packagejson;
Expand All @@ -47,14 +52,16 @@ static class Folder {
final Path symLinkTarget;

Folder(Folder parent, String folderName, boolean isWorkingDir, boolean isProject, boolean isPlainJS,
String yarnWorkspacesFolder, String name, String dependencies, Path symLinkTarget, String packagejson) {
WorkspaceType workspaceType, String workspacesFolder, String name, String dependencies,
Path symLinkTarget, String packagejson) {

this.parent = parent;
this.folderName = folderName;
this.isWorkingDir = isWorkingDir;
this.isProject = isProject;
this.isPlainJS = isPlainJS;
this.yarnWorkspacesFolder = yarnWorkspacesFolder;
this.workspaceType = workspaceType;
this.workspacesFolder = workspacesFolder;
this.name = name;
this.dependencies = dependencies;
this.symLinkTarget = symLinkTarget;
Expand All @@ -76,7 +83,7 @@ public String toString() {
str += folderName;
if (isProject) {
str += "[PROJECT";
str += (yarnWorkspacesFolder == null) ? "" : " workspaces= " + yarnWorkspacesFolder + " ";
str += (workspacesFolder == null) ? "" : " workspaces= " + workspacesFolder + " ";
str += (name == null) ? "" : " name= " + name + " ";
str += (dependencies == null) ? "" : " dependencies= " + dependencies + " ";
str += (packagejson == null) ? "" : " package.json= " + packagejson + " ";
Expand Down Expand Up @@ -205,7 +212,8 @@ private static Folder readFolder(String folderStr, List<Folder> parents) {

boolean isProject = false;
boolean isPlainJS = false;
String yarnWorkspacesFolder = null;
WorkspaceType workspaceType = WorkspaceType.Yarn;
String workspacesFolder = null;
String dependencies = null;
String name = null;
Path symLinkTarget = null;
Expand Down Expand Up @@ -246,11 +254,21 @@ private static Folder readFolder(String folderStr, List<Folder> parents) {

if (restLine.startsWith("workspaces")) {
restLine = restLine.substring("workspaces".length()).trim();
if (restLine.startsWith("-yarn")) {
// default
restLine = restLine.substring("-yarn".length()).trim();
workspaceType = WorkspaceType.Yarn;
}
if (restLine.startsWith("-pnpm")) {
restLine = restLine.substring("-pnpm".length()).trim();
workspaceType = WorkspaceType.Pnpm;
}
if (restLine.startsWith("=")) {
restLine = restLine.substring(1).trim();
int startIndex = restLine.indexOf("[");
int endIndex = restLine.indexOf("]");
yarnWorkspacesFolder = restLine.substring(startIndex, endIndex + 1);
// includes brackets
workspacesFolder = restLine.substring(startIndex, endIndex + 1);

restLine = restLine.substring(endIndex + 1).trim();
}
Expand Down Expand Up @@ -280,25 +298,26 @@ private static Folder readFolder(String folderStr, List<Folder> parents) {
}
}

return new Folder(parent, folderName, isWorkingDir, isProject, isPlainJS, yarnWorkspacesFolder, name,
dependencies,
symLinkTarget, packagejson);
return new Folder(parent, folderName, isWorkingDir, isProject, isPlainJS, workspaceType, workspacesFolder, name,
dependencies, symLinkTarget, packagejson);
}

/** Creates the folder structure specified by {@link ProjectDiscoveryTestData} in the given dir */
public static void createFolderStructure(File dir, ProjectDiscoveryTestData pdtd) {
for (Folder folder : pdtd.folders) {
String folderPath = folder.getPath();
File folderFile = new File(dir, folderPath);
if (folder.symLinkTarget != null) {
createSymbolicLink(dir, folderFile, folder.symLinkTarget);
} else {
if (folder.symLinkTarget == null) {
File folderFile = new File(dir, folder.getPath());
folderFile.mkdir();
if (folder.isProject) {
createPackageJson(folderFile, folder);
}
}
}
for (Folder folder : pdtd.folders) {
if (folder.symLinkTarget != null) {
createSymbolicLink(dir, new File(dir, folder.getPath()), folder.symLinkTarget);
}
}
}

private static void createSymbolicLink(File root, File folderFile, Path symLinkTarget) {
Expand All @@ -319,7 +338,7 @@ private static void createPackageJson(File folderFile, Folder folder) {
contents = folder.packagejson;
} else {
contents = "{";
if (folder.yarnWorkspacesFolder == null) {
if (folder.workspacesFolder == null) {
String projectName = folder.folderName;
if (folder.parent != null && folder.parent.folderName.startsWith("@")) {
projectName = folder.parent.folderName + "/" + projectName;
Expand All @@ -328,19 +347,29 @@ private static void createPackageJson(File folderFile, Folder folder) {
if (!folder.isPlainJS) {
contents += "\"n4js\": {\"projectType\": \"library\"}";
}
} else {
contents += "\"private\": true, \"workspaces\": " + folder.yarnWorkspacesFolder + "";
} else if (folder.workspaceType == WorkspaceType.Yarn) {
contents += "\"private\": true, \"workspaces\": " + folder.workspacesFolder + "";
} else if (folder.workspaceType == WorkspaceType.Pnpm) {
File pnpmWorkspaceYaml = new File(folderFile, N4JSGlobals.PNPM_WORKSPACE);
try (PrintWriter printWriter = new PrintWriter(new FileWriter(pnpmWorkspaceYaml))) {
String workspacesFolder = folder.workspacesFolder.substring(1,
folder.workspacesFolder.length() - 1);
printWriter.println("packages:\n - " + workspacesFolder);
} catch (IOException e) {
throw new WrappedException("exception while creating a package.json file", e);
}
}
contents += (folder.dependencies == null) ? "" : ", \"dependencies\":" + folder.dependencies;
contents += "}";
}

File packageJson = new File(folderFile, N4JSGlobals.PACKAGE_JSON);

try (PrintWriter printWriter = new PrintWriter(new FileWriter(packageJson))) {
printWriter.println(contents);
} catch (IOException e) {
throw new WrappedException("exception while creating a package.json file", e);
if (!contents.isBlank()) {
File packageJson = new File(folderFile, N4JSGlobals.PACKAGE_JSON);
try (PrintWriter printWriter = new PrintWriter(new FileWriter(packageJson))) {
printWriter.println(contents);
} catch (IOException e) {
throw new WrappedException("exception while creating a package.json file", e);
}
}
}
}

0 comments on commit 180bc4f

Please sign in to comment.