Search in sources :

Example 1 with YarnPackageJsonContents

use of org.eclipse.n4js.cli.init.InitResources.YarnPackageJsonContents in project n4js by eclipse.

the class N4jscInit method checkAndGetWorkingDirState.

private static WorkingDirState checkAndGetWorkingDirState(N4jscOptions options, File parentPackageJson) throws N4jscException {
    Path cwd = options.getWorkingDirectory();
    boolean cwdHasPackageJson = parentPackageJson != null && parentPackageJson.exists() && parentPackageJson.getParentFile().equals(cwd.toFile());
    if (options.isN4JS()) {
        if (cwdHasPackageJson) {
            return WorkingDirState.InExistingProject;
        } else {
            throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Given option --n4js requires a package.json file to be in the current working directory.");
        }
    }
    if (!options.isCreate()) {
        if (cwdHasPackageJson) {
            throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Current working directory must not contain a package.json file. Note:" + NL + "  In case you like to add the n4js property to an existing project, use option --n4js." + NL + "  In case you like to add a project to an existing workspace project, use options -w -c.");
        }
        if (options.isScope() && !cwd.getParent().toFile().getName().startsWith("@")) {
            throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "When creating a scoped package the parent directory of current working directory must start with '@'. Note:" + NL + "  In case you like to create a new project in a subfolder of the current working directory, use option -c.");
        }
    }
    if (parentPackageJson == null || !parentPackageJson.exists()) {
        return WorkingDirState.InEmptyFolder;
    }
    try (JsonReader jReader = new JsonReader(new FileReader(parentPackageJson))) {
        JsonElement packageJsonCandidate = JsonParser.parseReader(jReader);
        if (!packageJsonCandidate.isJsonObject()) {
            return WorkingDirState.InEmptyFolder;
        }
        JsonObject packageJson = (JsonObject) packageJsonCandidate;
        boolean isYarnProject = packageJson.has(PackageJsonProperties.WORKSPACES_ARRAY.name);
        if (!isYarnProject) {
            throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Current working directory is inside the non-yarn project of " + parentPackageJson);
        }
    } catch (N4jscException e) {
        throw e;
    } catch (Exception e) {
        throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Working directory must be either empty or inside a yarn project.", e);
    }
    if (!options.isWorkspaces()) {
        throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Creating a new project inside an existing yarn project requires option '--workspaces' to be set.");
    }
    Path candidateWorkDir = parentPackageJson.getParentFile().toPath();
    if (candidateWorkDir.equals(cwd)) {
        return WorkingDirState.InYarnProjectRoot;
    }
    YarnPackageJsonContents yarnPackageJson = YarnPackageJsonContents.read(candidateWorkDir);
    Path yarnRoot = parentPackageJson.getParentFile().toPath();
    boolean isCwdWorkspaceMatch = workspaceMatch(yarnPackageJson.workspaces, yarnRoot, cwd);
    if (!options.isCreate() && !isCwdWorkspaceMatch) {
        throw new N4jscException(N4jscExitCode.INIT_ERROR_WORKING_DIR, "Creating a new project inside a yarn project requires the current working directory to " + "be inside a new project folder of a valid workspaces directory of the yarn project. " + "Alternatively add option '--create' to create a new project directory.");
    }
    if (isCwdWorkspaceMatch) {
        return WorkingDirState.InYarnProjectEmptyPackage;
    }
    boolean isCwdWorkspaceParent = workspaceMatch(yarnPackageJson.workspaces, yarnRoot, cwd.resolve("test"));
    if (isCwdWorkspaceParent) {
        return WorkingDirState.InYarnProjectWorkspaces;
    }
    return WorkingDirState.InYarnProject;
}
Also used : Path(java.nio.file.Path) JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) FileReader(java.io.FileReader) YarnPackageJsonContents(org.eclipse.n4js.cli.init.InitResources.YarnPackageJsonContents) N4jscException(org.eclipse.n4js.cli.N4jscException) IOException(java.io.IOException) N4jscException(org.eclipse.n4js.cli.N4jscException)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonReader (com.google.gson.stream.JsonReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 N4jscException (org.eclipse.n4js.cli.N4jscException)1 YarnPackageJsonContents (org.eclipse.n4js.cli.init.InitResources.YarnPackageJsonContents)1