Search in sources :

Example 1 with TSNpmConfiguration

use of io.crnk.gen.typescript.TSNpmConfiguration in project crnk-framework by crnk-project.

the class TSGenerator method writePackaging.

protected void writePackaging() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    ObjectNode packageJson = mapper.createObjectNode();
    TSNpmConfiguration npm = config.getNpm();
    packageJson.put("name", npm.getPackageName());
    packageJson.put("version", npm.getPackageVersion());
    packageJson.put("description", npm.getDescription());
    packageJson.put("license", npm.getLicense());
    if (npm.getGitRepository() != null) {
        ObjectNode repository = packageJson.putObject("repository");
        repository.put("type", "git");
        repository.put("url", npm.getGitRepository());
    }
    ObjectNode peerDependencies = packageJson.putObject("peerDependencies");
    ObjectNode devDependencies = packageJson.putObject("devDependencies");
    for (Map.Entry<String, String> entry : npm.getPeerDependencies().entrySet()) {
        peerDependencies.put(entry.getKey(), entry.getValue());
        devDependencies.put(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, String> entry : npm.getDevDependencies().entrySet()) {
        devDependencies.put(entry.getKey(), entry.getValue());
    }
    File packageJsonFile = new File(outputDir, "package.json");
    packageJsonFile.getParentFile().mkdirs();
    ObjectNode scripts = packageJson.putObject("scripts");
    scripts.put("build", "npm run build:js");
    scripts.put("build:js", "tsc -p ./src --declaration");
    packageJson.put("name", npm.getPackageName());
    packageJson.put("version", npm.getPackageVersion());
    // match what is expected by NPM. Otherwise NPM will reformat it and up-to-date checking will fail
    // the first time (also platform specific)
    String text = mapper.writer().writeValueAsString(packageJson);
    text = text.replace(" : ", ": ");
    text = text + "\n";
    text = text.replace(System.lineSeparator(), "\n");
    try (FileWriter writer = new FileWriter(packageJsonFile)) {
        writer.write(text);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileWriter(java.io.FileWriter) TSNpmConfiguration(io.crnk.gen.typescript.TSNpmConfiguration) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TSNpmConfiguration (io.crnk.gen.typescript.TSNpmConfiguration)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1