Add post processing to files generated by TS generators (#1063)

* add file post proess to TS generators

* update ts node petstore with prettier

* update base on feedback
This commit is contained in:
William Cheng 2018-09-29 16:35:24 +08:00 committed by GitHub
parent 8e57e0d44f
commit e0cbf46041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ package org.openapitools.codegen.languages;
import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter; import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*; import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.ModelUtils;
@ -116,6 +117,10 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
public void processOpts() { public void processOpts() {
super.processOpts(); super.processOpts();
if (StringUtils.isEmpty(System.getenv("TS_POST_PROCESS_FILE"))) {
LOGGER.info("Hint: Environment variable 'TS_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export TS_POST_PROCESS_FILE=\"/usr/local/bin/prettier --write\"' (Linux/Mac)");
}
if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) { if (additionalProperties.containsKey(CodegenConstants.MODEL_PROPERTY_NAMING)) {
setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING)); setModelPropertyNaming((String) additionalProperties.get(CodegenConstants.MODEL_PROPERTY_NAMING));
} }
@ -561,4 +566,32 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
public String escapeUnsafeCharacters(String input) { public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*"); return input.replace("*/", "*_/").replace("/*", "/_*");
} }
@Override
public void postProcessFile(File file, String fileType) {
super.postProcessFile(file, fileType);
if (file == null) {
return;
}
String tsPostProcessFile = System.getenv("TS_POST_PROCESS_FILE");
if (StringUtils.isEmpty(tsPostProcessFile)) {
return; // skip if TS_POST_PROCESS_FILE env variable is not defined
}
// only process files with ts extension
if ("ts".equals(FilenameUtils.getExtension(file.toString()))) {
String command = tsPostProcessFile + " " + file.toString();
try {
Process p = Runtime.getRuntime().exec(command);
int exitValue = p.waitFor();
if (exitValue != 0) {
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
} else {
LOGGER.info("Successfully executed: " + command);
}
} catch (Exception e) {
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
}
}
}
} }