Skip to content

Commit

Permalink
Use camelCase for generating definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dderevjanik committed Mar 1, 2021
1 parent 80a46a1 commit cab5c3a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
test/generated/
dist/
generated/
dist/
*.wsdl
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
src/
test/
generated/
test/
*.wsdl
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"wsdl-tsclient": "dist/cli.js"
},
"scripts": {
"pretest": "rm -rf ./test/generated",
"test": "ts-node ./test/generate-clients.test.ts",
"prepublishOnly": "npm test && npm run dist",
"dev": "ts-node ./dev.ts",
"dist": "tsc"
},
"repository": {
Expand All @@ -35,6 +37,7 @@
"dependencies": {
"@types/node": "^14.14.31",
"@types/yargs-parser": "^20.2.0",
"camelcase": "^6.2.0",
"soap": "^0.36.0",
"ts-morph": "^10.0.1",
"yargs-parser": "^20.2.6"
Expand Down
13 changes: 8 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import yargs from "yargs-parser";
import path from "path";
import { generateClient } from "./index";
import packageJson from "../package.json";

type Config = {
_: any[];
h?: boolean;
help?: boolean;
i?: string;
Expand All @@ -13,18 +15,19 @@ type Config = {

const conf: Config = yargs(process.argv.slice(2)) as any;
if (conf.h || conf.help) {
process.stdout.write("Usage: wsdl -i WSDL_PATH -o OUT_DIR\n");
process.stdout.write(`Version: ${packageJson.version}\n`);
process.stdout.write("Usage: wsdl-tsclient WSDL_PATH -o OUT_DIR\n");
process.stdout.write("\n");
process.stdout.write("Example: wsdl-tsclient -i file.wsdl -o ./generator/client\n");
process.stdout.write("Example: wsdl-tsclient file.wsdl -o ./generator/client\n");
process.stdout.write("\n");
process.stdout.write("\t-i\tpath to your wsdl file\n");
process.stdout.write("\tWSDL_PATH\tpath to your wsdl file\n");
process.stdout.write("\t-o\toutput dir\n");
// TODO: Finish --js
process.exit(0);
}

if (conf.i === undefined || conf.i.length === 0) {
console.error('You forget to pass path to wsdl file -i');
if (conf._ === undefined || conf._.length === 0) {
console.error('You forget to pass path to wsdl file');
process.exit(1);
}

Expand Down
37 changes: 21 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "path";
import camelCase from "camelcase";
import { existsSync } from "fs";
import { ImportDeclarationStructure, MethodSignatureStructure, OptionalKind, Project, PropertySignatureStructure, StructureKind } from "ts-morph";
import { open_wsdl } from "soap/lib/wsdl/index";
Expand Down Expand Up @@ -52,7 +53,7 @@ const propertiesBlaclist = [
function createProperty(name: string, type: string, doc: string, optional = true): PropertySignatureStructure {
return {
kind: StructureKind.PropertySignature,
name: name,
name: camelCase(name),
docs: [doc],
hasQuestionToken: true,
type: type
Expand All @@ -63,11 +64,13 @@ function createProperty(name: string, type: string, doc: string, optional = true
* Generate definiton
* @param project ts-morph project
* @param defDir dir where to put definition file
* @param defName name of definition
* @param name name of definition
* @param defParts definitions parts (it's properties from wsdl)
* @param stack definition stack (for deep objects)
*/
function generateDefinitionFile(project: Project, defDir: string, defName: string, defParts: { [propNameType: string]: any }, stack: string[]): string {
function generateDefinitionFile(project: Project, defDir: string, name: string, defParts: { [propNameType: string]: any }, stack: string[]): string {
const defName = camelCase(name, { pascalCase: true });

const fileName = findNonUseDefNameInCache(defName);
const filePath = path.join(defDir, `${fileName}.ts`);

Expand Down Expand Up @@ -182,7 +185,7 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
for (const [methodName, method] of Object.entries(port.binding.methods)) { // [O_CustomerChange]

// TODO: Deduplicate code below by refactoring it to external function. Is it possible ?
let paramName = "requset";
let paramName = "request";
let paramType = "{}";
if (method.input) {
paramName = method.input.$name;
Expand Down Expand Up @@ -217,21 +220,18 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
}

allMethods[methodName] = {
paramName: paramName,
paramName: camelCase(paramName),
paramType: paramType, // TODO: Use string from generated definition files
returnType: returnType // TODO: Use string from generated definition files
};

portMethods.push({
name: methodName,
paramName: paramName,
paramName: camelCase(paramName),
paramType: paramType, // TODO: Use string from generated definition files
returnType: returnType // TODO: Use string from generated definition files
});
}

const portFilePath = path.resolve(portsDir, `${portName}.ts`);
const portFile = project.createSourceFile(portFilePath);
const portFileMethods: Array<OptionalKind<MethodSignatureStructure>> = [];
portMethods.forEach((method) => {
portFileMethods.push({
Expand All @@ -247,6 +247,10 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
});
})

const portFinalName = camelCase(portName, { pascalCase: true });
const portFilePath = path.resolve(portsDir, `${portFinalName}.ts`);
const portFile = project.createSourceFile(portFilePath);

portFile.addImportDeclarations(Array.from(portImports).map<OptionalKind<ImportDeclarationStructure>>((imp) => ({
moduleSpecifier: `../definitions/${imp}`,
namedImports: [{ name: imp }]
Expand All @@ -256,17 +260,18 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
leadingTrivia: writer => writer.newLine(),
isExported: true,
kind: StructureKind.Interface,
name: portName,
name: portFinalName,
// NOTE: Could be optimized with Object.entries(port) above
methods: portFileMethods
}
]);
portFile.saveSync();
portsExport.add(portName);
console.debug(`Created Port file: ${path.join(portsDir, portName)}`);
portsExport.add(portFinalName);
console.debug(`Created Port file: ${path.join(portsDir, portFinalName)}`);
} // End of Port cycle

const serviceFilePath = path.resolve(servicesDir, `${serviceName}.ts`);
const finalServiceName = camelCase(serviceName);
const serviceFilePath = path.resolve(servicesDir, `${finalServiceName}.ts`);
const serviceFile = project.createSourceFile(serviceFilePath);

serviceFile.addImportDeclarations(Array.from(portsExport).map<OptionalKind<ImportDeclarationStructure>>(imp => ({
Expand All @@ -278,7 +283,7 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
leadingTrivia: writer => writer.newLine(),
isExported: true,
kind: StructureKind.Interface,
name: serviceName,
name: finalServiceName,
properties: Array.from(portsExport).map<OptionalKind<PropertySignatureStructure>>(portImport => ({
name: portImport,
isReadonly: true,
Expand All @@ -287,8 +292,8 @@ export async function generateClient(wsdlPath: string, outDir: string): Promise<
}
]);
serviceFile.saveSync();
servicesExports.add(serviceName);
console.debug(`Created Service file: ${path.join(servicesDir, serviceName)}`);
servicesExports.add(finalServiceName);
console.debug(`Created Service file: ${path.join(servicesDir, finalServiceName)}`);
} // End of Service cycle

// Generate client
Expand Down
11 changes: 6 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@

/* Strict Type-Checking Options */
"strict": false, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
Expand All @@ -53,6 +53,7 @@
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true,

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down

0 comments on commit cab5c3a

Please sign in to comment.