diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index e6d774783d4..be0e5ddb59b 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -1,15 +1,28 @@ package io.swagger.codegen.languages; import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; +import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenParameter; import io.swagger.codegen.SupportingFile; import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.FileProperty; import io.swagger.models.properties.MapProperty; import io.swagger.models.properties.Property; public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCodegen { + private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); + public static final String NMP_NAME = "nmpName"; + public static final String NMP_VERSION = "nmpVersion"; + public static final String NPM_REPOSITORY = "npmRepository"; + public static final String SNAPSHOT = "Snapshot"; + + private String npmName = null; + private String npmVersion = "1.0.0"; + private String npmRepository = null; public TypeScriptAngular2ClientCodegen() { super(); @@ -21,6 +34,12 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod typeMapping.put("Date","Date"); apiPackage = "api"; modelPackage = "model"; + + + this.cliOptions.add(new CliOption(NMP_NAME, "The name under which you want to publish generated npm package")); + this.cliOptions.add(new CliOption(NMP_VERSION, "The version of your npm package")); + this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); + this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); } @Override @@ -36,11 +55,31 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod @Override public void processOpts() { super.processOpts(); - supportingFiles.clear(); supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts")); supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts")); supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts")); + + if(additionalProperties.containsKey(NMP_NAME)) { + addNpmPackageGeneration(); + } + } + + private void addNpmPackageGeneration() { + if(additionalProperties.containsKey(NMP_NAME)) { + this.setNpmName(additionalProperties.get(NMP_NAME).toString()); + } + + if (additionalProperties.containsKey(NMP_VERSION)) { + this.setNpmVersion(additionalProperties.get(NMP_VERSION).toString()); + } + + if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) { + this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date())); + } + additionalProperties.put(NMP_VERSION, npmVersion); + + supportingFiles.add(new SupportingFile("README.mustache", getIndexDirectory(), "README.md")); } private String getIndexDirectory() { @@ -87,4 +126,27 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod parameter.dataType = addModelPrefix(parameter.dataType); } + public String getNpmName() { + return npmName; + } + + public void setNpmName(String npmName) { + this.npmName = npmName; + } + + public String getNpmVersion() { + return npmVersion; + } + + public void setNpmVersion(String npmVersion) { + this.npmVersion = npmVersion; + } + + public String getNpmRepository() { + return npmRepository; + } + + public void setNpmRepository(String npmRepository) { + this.npmRepository = npmRepository; + } } diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache new file mode 100644 index 00000000000..345cd299a72 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache @@ -0,0 +1,33 @@ +## {{npmName}}@{{npmVersion}} + +### Building + +To build an compile the typescript sources to javascript use: +``` +npm install +npm run build +``` + +### publishing + +First build the package than run ```npm publish``` + +### consuming + +navigate to the folder of your consuming project and run one of next commando's. + +_published:_ + +``` +npm install {{npmName}}@{{npmVersion}} --save +``` + +_unPublished (not recommended):_ + +``` +npm install PATH_TO_GENERATED_PACKAGE --save +``` + +In your angular2 project: + +TODO: paste example. \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/package.json b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.json new file mode 100644 index 00000000000..9d24adc7165 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.json @@ -0,0 +1,41 @@ +{ + "name": "@restore/fxt-configurator-client", + "version": "1.${project.version}", + "description": "client to consume the fxt-configurator-api", + "keywords": [ + "swagger-client" + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/swagger-api/swagger-codegen/issues" + }, + "files": [ + "lib" + ], + "main": "./lib/index.js", + "typings": "./lib/index.d.ts", + "scripts": { + "build": "typings install && tsc" + }, + "repository": { + "type": "git", + "url": "https://stash.flexpond.com/scm/flxp/flextract-service-module.git" + }, + "peerDependencies": { + "angular2": "^2.0.0-beta.15", + "rxjs": "^5.0.0-beta.2" + }, + "devDependencies": { + "typescript": "^1.8.10", + "typings": "^0.8.1", + "angular2": "^2.0.0-beta.15", + "es6-shim": "^0.35.0", + "es7-reflect-metadata": "^1.6.0", + "rxjs": "5.0.0-beta.2", + "zone.js": "^0.6.10" + }, + "private": true, + "publishConfig":{ + "registry":"http://my-internal-registry.local" + } +} diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/tsconfig.json b/modules/swagger-codegen/src/main/resources/typescript-angular2/tsconfig.json new file mode 100644 index 00000000000..07fbdf7e1b1 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "noImplicitAny": false, + "suppressImplicitAnyIndexErrors": true, + "target": "es5", + "module": "commonjs", + "moduleResolution": "node", + "removeComments": true, + "sourceMap": true, + "outDir": "./lib", + "noLib": false, + "declaration": true + }, + "exclude": [ + "node_modules", + "typings/main.d.ts", + "typings/main", + "lib" + ], + "filesGlob": [ + "./model/*.ts", + "./api/*.ts", + "typings/browser.d.ts" + ] +} diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.json b/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.json new file mode 100644 index 00000000000..32530eeb63d --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.json @@ -0,0 +1,5 @@ +{ + "ambientDependencies": { + "core-js": "registry:dt/core-js#0.0.0+20160317120654" + } +} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/TypeScriptAngular2ClientOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/TypeScriptAngular2ClientOptionsProvider.java index c7bddc9f663..97a2095c334 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/TypeScriptAngular2ClientOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/TypeScriptAngular2ClientOptionsProvider.java @@ -5,11 +5,15 @@ import com.google.common.collect.ImmutableMap; import java.util.Map; import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; public class TypeScriptAngular2ClientOptionsProvider implements OptionsProvider { public static final String SORT_PARAMS_VALUE = "false"; public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + public static final String NMP_NAME = "npmName"; + private static final String NMP_VERSION = "1.1.2"; + private static final String NPM_REPOSITORY = "https://registry.npmjs.org"; @Override public String getLanguage() { @@ -22,6 +26,10 @@ public class TypeScriptAngular2ClientOptionsProvider implements OptionsProvider return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(TypeScriptAngular2ClientCodegen.NMP_NAME, NMP_NAME) + .put(TypeScriptAngular2ClientCodegen.NMP_VERSION, NMP_VERSION) + .put(TypeScriptAngular2ClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) + .put(TypeScriptAngular2ClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) .build(); }