Skip to content

Commit 8596c1b

Browse files
Merge pull request #117 from vojtechhabarta/generateNpmPackageJson
Generate NPM package.json
2 parents a6277df + 60c1287 commit 8596c1b

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class Settings {
5151
public List<EmitterExtension> extensions = new ArrayList<>();
5252
public List<Class<? extends Annotation>> includePropertyAnnotations = new ArrayList<>();
5353
public List<Class<? extends Annotation>> optionalAnnotations = new ArrayList<>();
54+
public boolean generateNpmPackageJson = false;
55+
public String npmName = null;
56+
public String npmVersion = null;
5457
public boolean displaySerializerWarning = true;
5558
public boolean disableJackson2ModuleDiscovery = false;
5659
public ClassLoader classLoader = null;
@@ -134,6 +137,19 @@ public void validate() {
134137
throw new RuntimeException("'mapClasses' parameter is set to `asClasses` which generates runtime code but 'outputFileType' parameter is not set to 'implementationFile'.");
135138
}
136139
}
140+
if (generateNpmPackageJson && outputKind != TypeScriptOutputKind.module) {
141+
throw new RuntimeException("'generateNpmPackageJson' can only be used when generating proper module ('outputKind' parameter is 'module').");
142+
}
143+
if (generateNpmPackageJson) {
144+
if (npmName == null || npmVersion == null) {
145+
throw new RuntimeException("'npmName' and 'npmVersion' must be specified when generating NPM package.json.");
146+
}
147+
}
148+
if (!generateNpmPackageJson) {
149+
if (npmName != null || npmVersion != null) {
150+
throw new RuntimeException("'npmName' and 'npmVersion' is only applicable when generating NPM package.json.");
151+
}
152+
}
137153
}
138154

139155
public String getExtension() {
@@ -149,6 +165,10 @@ public void validateFileName(File outputFile) {
149165
}
150166
}
151167

168+
public String getDefaultNpmVersion() {
169+
return "1.0.0";
170+
}
171+
152172
public Predicate<String> getExcludeFilter() {
153173
if (excludeFilter == null) {
154174
setExcludeFilter(null, null);

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TypeScriptGenerator.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class TypeScriptGenerator {
1717
private ModelParser modelParser = null;
1818
private ModelCompiler modelCompiler = null;
1919
private Emitter emitter = null;
20+
private NpmPackageJsonEmitter npmPackageJsonEmitter = null;
2021

2122
public TypeScriptGenerator() {
2223
this (new Settings());
@@ -49,6 +50,21 @@ private void generateTypeScript(Input input, Output output, boolean forceExportK
4950
final Model model = getModelParser().parseModel(input.getSourceTypes());
5051
final TsModel tsModel = getModelCompiler().javaToTypeScript(model);
5152
getEmitter().emit(tsModel, output.getWriter(), output.getName(), output.shouldCloseWriter(), forceExportKeyword, initialIndentationLevel);
53+
generateNpmPackageJson(output);
54+
}
55+
56+
private void generateNpmPackageJson(Output output) {
57+
if (settings.generateNpmPackageJson) {
58+
if (output.getName() == null) {
59+
throw new RuntimeException("Generating NPM package.json can only be used when output is specified using file name");
60+
}
61+
final Output npmOutput = Output.to(new File(new File(output.getName()).getParent(), "package.json"));
62+
final NpmPackageJson npmPackageJson = new NpmPackageJson();
63+
npmPackageJson.name = settings.npmName;
64+
npmPackageJson.version = settings.npmVersion;
65+
npmPackageJson.types = new File(output.getName()).getName();
66+
getNpmPackageJsonEmitter().emit(npmPackageJson, npmOutput.getWriter(), npmOutput.getName(), npmOutput.shouldCloseWriter());
67+
}
5268
}
5369

5470
public TypeProcessor getTypeProcessor() {
@@ -99,6 +115,13 @@ public Emitter getEmitter() {
99115
return emitter;
100116
}
101117

118+
public NpmPackageJsonEmitter getNpmPackageJsonEmitter() {
119+
if (npmPackageJsonEmitter == null) {
120+
npmPackageJsonEmitter = new NpmPackageJsonEmitter();
121+
}
122+
return npmPackageJsonEmitter;
123+
}
124+
102125
private static String getVersion() {
103126
try {
104127
final InputStream inputStream = TypeScriptGenerator.class.getResourceAsStream(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
package cz.habarta.typescript.generator.emitter;
3+
4+
5+
public class NpmPackageJson {
6+
7+
public String name;
8+
public String version;
9+
public String types;
10+
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
package cz.habarta.typescript.generator.emitter;
3+
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import cz.habarta.typescript.generator.util.StandardJsonPrettyPrinter;
7+
import java.io.*;
8+
9+
10+
public class NpmPackageJsonEmitter {
11+
12+
private Writer writer;
13+
14+
public void emit(NpmPackageJson npmPackageJson, Writer output, String outputName, boolean closeOutput) {
15+
this.writer = output;
16+
if (outputName != null) {
17+
System.out.println("Writing NPM package to: " + outputName);
18+
}
19+
emitPackageJson(npmPackageJson);
20+
if (closeOutput) {
21+
close();
22+
}
23+
}
24+
25+
private void emitPackageJson(NpmPackageJson npmPackageJson) {
26+
try {
27+
final ObjectMapper objectMapper = new ObjectMapper();
28+
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
29+
objectMapper.setDefaultPrettyPrinter(new StandardJsonPrettyPrinter(" ", "\n"));
30+
objectMapper.writeValue(writer, npmPackageJson);
31+
} catch (IOException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
private void close() {
37+
try {
38+
writer.close();
39+
} catch (IOException e) {
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package cz.habarta.typescript.generator.util;
3+
4+
import com.fasterxml.jackson.core.JsonGenerator;
5+
import com.fasterxml.jackson.core.util.DefaultIndenter;
6+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
7+
import java.io.IOException;
8+
9+
10+
/**
11+
* Jackson2 PrettyPrinter implementation that produces JSON format similar to JSON.stringify() output.
12+
*/
13+
public class StandardJsonPrettyPrinter extends DefaultPrettyPrinter {
14+
private static final long serialVersionUID = 1;
15+
16+
private final String indent;
17+
private final String eol;
18+
19+
public StandardJsonPrettyPrinter() {
20+
this(" ", String.format("%n"));
21+
}
22+
23+
public StandardJsonPrettyPrinter(String indent, String eol) {
24+
this.indent = indent;
25+
this.eol = eol;
26+
final DefaultIndenter indenter = new DefaultIndenter(indent, eol);
27+
this._arrayIndenter = indenter;
28+
this._objectIndenter = indenter;
29+
}
30+
31+
@Override
32+
public DefaultPrettyPrinter createInstance() {
33+
return new StandardJsonPrettyPrinter(indent, eol);
34+
}
35+
36+
@Override
37+
public void writeObjectFieldValueSeparator(JsonGenerator jg) throws IOException {
38+
jg.writeRaw(": ");
39+
}
40+
41+
}

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class GenerateTask extends DefaultTask {
5050
public List<File> javadocXmlFiles;
5151
public List<String> extensionClasses;
5252
public List<String> optionalAnnotations;
53+
public boolean generateNpmPackageJson;
54+
public String npmName;
55+
public String npmVersion;
5356
public StringQuotes stringQuotes;
5457
public boolean displaySerializerWarning = true;
5558
public boolean disableJackson2ModuleDiscovery;
@@ -113,6 +116,9 @@ public void generate() throws Exception {
113116
settings.loadExtensions(classLoader, extensionClasses);
114117
settings.loadIncludePropertyAnnotations(classLoader, includePropertyAnnotations);
115118
settings.loadOptionalAnnotations(classLoader, optionalAnnotations);
119+
settings.generateNpmPackageJson = generateNpmPackageJson;
120+
settings.npmName = npmName != null ? npmName : getProject().getName();
121+
settings.npmVersion = npmVersion != null ? npmVersion : settings.getDefaultNpmVersion();
116122
settings.setStringQuotes(stringQuotes);
117123
settings.displaySerializerWarning = displaySerializerWarning;
118124
settings.disableJackson2ModuleDiscovery = disableJackson2ModuleDiscovery;

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,30 @@ public class GenerateMojo extends AbstractMojo {
310310
@Parameter
311311
private List<String> optionalAnnotations;
312312

313+
/**
314+
* If true NPM package.json will be generated.
315+
* Only applicable when 'outputKind' is set to 'module'.
316+
* NPM package name and version can be specified using 'npmName' and 'npmVersion' parameters.
317+
*/
318+
@Parameter
319+
private boolean generateNpmPackageJson;
320+
321+
/**
322+
* Specifies NPM package name.
323+
* Only applicable when 'generateNpmPackageJson' parameter is 'true'.
324+
* Default value is <code>${project.artifactId}</code>.
325+
*/
326+
@Parameter
327+
private String npmName;
328+
329+
/**
330+
* Specifies NPM package version.
331+
* Only applicable when 'generateNpmPackageJson' parameter is 'true'.
332+
* Default value is <code>1.0.0</code>.
333+
*/
334+
@Parameter
335+
private String npmVersion;
336+
313337
/**
314338
* Specifies how strings will be quoted.
315339
* Supported values are 'doubleQuotes', 'singleQuotes'.
@@ -384,6 +408,9 @@ public void execute() {
384408
settings.loadExtensions(classLoader, extensions);
385409
settings.loadIncludePropertyAnnotations(classLoader, includePropertyAnnotations);
386410
settings.loadOptionalAnnotations(classLoader, optionalAnnotations);
411+
settings.generateNpmPackageJson = generateNpmPackageJson;
412+
settings.npmName = npmName != null ? npmName : project.getArtifactId();
413+
settings.npmVersion = npmVersion != null ? npmVersion : settings.getDefaultNpmVersion();
387414
settings.setStringQuotes(stringQuotes);
388415
settings.displaySerializerWarning = displaySerializerWarning;
389416
settings.disableJackson2ModuleDiscovery = disableJackson2ModuleDiscovery;

0 commit comments

Comments
 (0)