Fix import name collision. Remove extra options (#29)

This commit is contained in:
Ildar Galeev 2022-12-20 17:56:36 +03:00 committed by GitHub
parent 246fc01328
commit a8cdbd96ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 64 deletions

View File

@ -13,8 +13,6 @@ Options:
```
-i, --inputs List of thrift file folders for compilation. [array] [required]
-n, --namespaces List of service namespaces which will be included. [array] [required]
-t, --types List of types namespaces witch will be exported. [array] [required]
-p, --path Default service connection path. [string] [required]
```
## Testing
@ -23,6 +21,6 @@ Options:
- Run
npm run codegen -- --i ./proto --n domain_config --t domain_config domain --p /wachter
npm run codegen -- --i ./proto --n domain_config
- Codegen client will be available in `dist` directory.

View File

@ -1,7 +1,7 @@
const path = require('path');
const webpack = require('webpack');
const build = async () =>
const build = () =>
new Promise((resolve, reject) => {
webpack(
{
@ -54,7 +54,7 @@ const build = async () =>
colors: true,
})
);
resolve();
stats.hasErrors() ? reject('Build failed') : resolve();
}
);
});

View File

@ -1,51 +1,15 @@
const fs = require('fs');
const fse = require('fs-extra');
const glob = require('glob');
const path = require('path');
const rimraf = require('rimraf');
const camelCase = require('lodash/camelCase');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const compileProto = require('./compile-proto');
const generateServiceTemplate = require('./generate-service-template');
const prepareGenerateServiceConfig = require('./prepare-generate-service-config');
const build = require('./build');
/**
* Dist with compiled proto contains files with name: '{namespace}-{serviceName}.ext'
* Pair of namespace and serviceName requires for preparing codegen client.
*/
const prepareGenerateServiceConfig = (compiledDist, includedNamespaces) => {
const result = fs
.readdirSync(compiledDist)
.map((filePath) => path.parse(filePath))
.filter(({ ext, name }) => ext === '.js' && name.includes('-'))
.map(({ name }) => {
const [namespace, serviceName] = name.split('-');
return {
namespace,
serviceName,
};
})
.reduce((acc, curr) => {
const duplicate = acc.find(({ serviceName }) => serviceName === curr.serviceName);
const result = {
...curr,
exportName: duplicate
? camelCase(`${camelCase(curr.namespace)}${curr.serviceName}`)
: curr.serviceName,
};
return [...acc, result];
}, []);
if (includedNamespaces.length === 0) {
return result;
}
return result.reduce(
(acc, curr) => (includedNamespaces.includes(curr.namespace) ? [...acc, curr] : [...acc]),
[]
);
};
const rm = (path) =>
new Promise((resolve, reject) => rimraf(path, (err) => (err ? reject(err) : resolve())));
@ -96,25 +60,13 @@ async function codegenClient() {
type: 'array',
description: 'List of service namespaces which will be included.',
},
types: {
alias: 't',
demandOption: true,
type: 'array',
description: 'List of types namespaces witch will be exported.',
},
path: {
alias: 'p',
demandOption: true,
type: 'string',
description: 'Default service connection path.',
},
}).argv;
await clean();
const outputPath = './clients';
const outputProtoPath = `${outputPath}/internal`;
await compileProto(argv.inputs, outputProtoPath);
const serviceTemplateConfig = prepareGenerateServiceConfig(outputProtoPath, argv.namespaces);
await generateServiceTemplate(serviceTemplateConfig, argv.types, outputPath, argv.path);
await generateServiceTemplate(serviceTemplateConfig, argv.namespaces, outputPath);
await copyTsUtils();
await build();
await copyMetadata();

View File

@ -14,12 +14,7 @@ const prepareIndexFileContent = (config, typeExportNamespaces) => {
);
};
const generateServiceTemplate = async (
config,
typeExportNamespaces,
outputPath,
connectionPath
) => {
const generateServiceTemplate = async (config, typeExportNamespaces, outputPath) => {
await generateTemplateFilesBatch([
...config.map(({ serviceName, namespace, exportName }) => ({
option: 'Create thrift client',
@ -32,7 +27,6 @@ const generateServiceTemplate = async (
{ slot: '__serviceName__', slotValue: serviceName },
{ slot: '__namespace__', slotValue: namespace },
{ slot: '__utilsPath__', slotValue: './utils' },
{ slot: '__connectionPath__', slotValue: connectionPath },
],
output: {
path: `${outputPath}/__exportName__.ts`,

View File

@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');
const camelCase = require('lodash/camelCase');
const duplicatedNamespaceServiceNameReducer = (config, configCurrEl) => {
const result = {
...configCurrEl,
exportName:
configCurrEl.namespace === configCurrEl.serviceName.toLowerCase()
? `${configCurrEl.serviceName}Service`
: configCurrEl.serviceName,
};
return [...config, result];
};
const duplicatedServiceNamesReducer = (config, configCurrEl) => {
const duplicate = config.find(({ serviceName }) => serviceName === configCurrEl.serviceName);
const result = {
...configCurrEl,
exportName: duplicate
? camelCase(`${camelCase(configCurrEl.namespace)}${configCurrEl.serviceName}`)
: configCurrEl.serviceName,
};
return [...config, result];
};
const namespacesReducer = (includedNamespaces) => (config, configCurrEl) =>
includedNamespaces.includes(configCurrEl.namespace) ? [...config, configCurrEl] : [...config];
/**
* Dist with compiled proto contains files with name: '{namespace}-{serviceName}.ext'
* Namespace, serviceName, exportName require for preparing codegen client.
*/
const prepareGenerateServiceConfig = (compiledDist, includedNamespaces) =>
fs
.readdirSync(compiledDist)
.map((filePath) => path.parse(filePath))
.filter(({ ext, name }) => ext === '.js' && name.includes('-'))
.map(({ name }) => {
const [namespace, serviceName] = name.split('-');
return {
namespace,
serviceName,
exportName: serviceName,
};
})
.reduce(duplicatedServiceNamesReducer, [])
.reduce(duplicatedNamespaceServiceNameReducer, [])
.reduce(namespacesReducer(includedNamespaces), []);
module.exports = prepareGenerateServiceConfig;

View File

@ -14,7 +14,7 @@ export const __exportName__ = async (
const namespace = '__namespace__';
const methodsMeta = getMethodsMetadata(options.metadata, namespace, serviceName);
const connectionContext = {
path: options.path || '__connectionPath__',
path: options.path,
service,
headers: options.headers,
deadlineConfig: options.deadlineConfig,

View File

@ -4,7 +4,7 @@ import { ThriftAstMetadata } from './thrift-ast-metadata';
export interface ConnectOptions {
metadata: ThriftAstMetadata[];
headers: KeyValue;
path?: string;
path: string;
deadlineConfig?: DeadlineConfig;
logging?: boolean;
}