Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit b16e07c

Browse files
authored
add post processing to nodejs express server (OpenAPITools#5369)
1 parent 734a35a commit b16e07c

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,8 @@ public CodegenModel fromModel(String name, Schema model) {
887887
String itemType = getSchemaType(ModelUtils.getAdditionalProperties(model));
888888
codegenModel.vendorExtensions.put("x-isMap", true); // TODO: 5.0 Remove
889889
codegenModel.vendorExtensions.put("x-is-map", true);
890-
codegenModel.vendorExtensions.put("x-itemType",itemType); // TODO: 5.0 Remove
891-
codegenModel.vendorExtensions.put("x-item-type",itemType);
890+
codegenModel.vendorExtensions.put("x-itemType", itemType); // TODO: 5.0 Remove
891+
codegenModel.vendorExtensions.put("x-item-type", itemType);
892892
} else {
893893
String type = model.getType();
894894
if (codegenModel != null && isPrimitiveType(type)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java

+40-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import io.swagger.v3.oas.models.PathItem.HttpMethod;
2626
import io.swagger.v3.oas.models.Paths;
2727
import io.swagger.v3.oas.models.info.Info;
28+
import org.apache.commons.io.FilenameUtils;
29+
import org.apache.commons.lang3.StringUtils;
2830
import org.openapitools.codegen.*;
2931
import org.openapitools.codegen.meta.GeneratorMetadata;
3032
import org.openapitools.codegen.meta.Stability;
@@ -38,7 +40,7 @@
3840
import java.util.*;
3941
import java.util.Map.Entry;
4042

41-
import static org.openapitools.codegen.utils.StringUtils.*;
43+
import static org.openapitools.codegen.utils.StringUtils.camelize;
4244

4345
public class NodeJSExpressServerCodegen extends DefaultCodegen implements CodegenConfig {
4446

@@ -76,8 +78,8 @@ public NodeJSExpressServerCodegen() {
7678
.build();
7779

7880
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
79-
.stability(Stability.BETA)
80-
.build();
81+
.stability(Stability.BETA)
82+
.build();
8183

8284
outputFolder = "generated-code/nodejs-express-server";
8385
embeddedTemplateDir = templateDir = "nodejs-express-server";
@@ -303,6 +305,11 @@ private static List<Map<String, Object>> sortOperationsByPath(List<CodegenOperat
303305
public void processOpts() {
304306
super.processOpts();
305307

308+
if (StringUtils.isEmpty(System.getenv("JS_POST_PROCESS_FILE"))) {
309+
LOGGER.info("Environment variable JS_POST_PROCESS_FILE not defined so the JS code may not be properly formatted. To define it, try 'export JS_POST_PROCESS_FILE=\"/usr/local/bin/js-beautify -r -f\"' (Linux/Mac)");
310+
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
311+
}
312+
306313
if (additionalProperties.containsKey(EXPORTED_NAME)) {
307314
setExportedName((String) additionalProperties.get(EXPORTED_NAME));
308315
}
@@ -321,8 +328,8 @@ public void processOpts() {
321328
@Override
322329
public void preprocessOpenAPI(OpenAPI openAPI) {
323330
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
324-
String host = URLPathUtils.getProtocolAndHost(url);
325-
String port = URLPathUtils.getPort(url, defaultServerPort) ;
331+
String host = URLPathUtils.getProtocolAndHost(url);
332+
String port = URLPathUtils.getPort(url, defaultServerPort);
326333
String basePath = url.getPath();
327334

328335
if (additionalProperties.containsKey(SERVER_PORT)) {
@@ -407,4 +414,32 @@ public String escapeQuotationMark(String input) {
407414
// remove " to avoid code injection
408415
return input.replace("\"", "");
409416
}
417+
418+
@Override
419+
public void postProcessFile(File file, String fileType) {
420+
if (file == null) {
421+
return;
422+
}
423+
424+
String jsPostProcessFile = System.getenv("JS_POST_PROCESS_FILE");
425+
if (StringUtils.isEmpty(jsPostProcessFile)) {
426+
return; // skip if JS_POST_PROCESS_FILE env variable is not defined
427+
}
428+
429+
// only process files with js extension
430+
if ("js".equals(FilenameUtils.getExtension(file.toString()))) {
431+
String command = jsPostProcessFile + " " + file.toString();
432+
try {
433+
Process p = Runtime.getRuntime().exec(command);
434+
p.waitFor();
435+
int exitValue = p.exitValue();
436+
if (exitValue != 0) {
437+
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
438+
}
439+
LOGGER.info("Successfully executed: " + command);
440+
} catch (Exception e) {
441+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
442+
}
443+
}
444+
}
410445
}

0 commit comments

Comments
 (0)