0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

feat: conditional generate custom types file and export types in index

This commit is contained in:
Gao Sun 2021-07-03 19:19:12 +08:00
parent efa550834a
commit d30260634d
No known key found for this signature in database
GPG key ID: 0F0EFA2E36639F31
2 changed files with 30 additions and 25 deletions

View file

@ -1,4 +1,5 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
export * from './custom-types';
export * from './oidc-model-instance';
export * from './user';

View file

@ -115,6 +115,7 @@ const generate = async () => {
);
const generatedDir = 'src/db-entries';
const generatedTypesFilename = 'custom-types';
const header = '// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n\n';
const getOutputFileName = (file: string) => pluralize(file.slice(0, -4).replace(/_/g, '-'), 1);
@ -127,21 +128,23 @@ const generate = async () => {
tsName: camelcase(type.name, { pascalCase: true }),
}));
// Generate custom types
await fs.writeFile(
path.join(generatedDir, 'custom-types.ts'),
header +
allTypes
.map(({ tsName, values }) =>
[
`export enum ${tsName} {`,
...values.map((value) => ` ${value} = '${value}',`),
'}',
].join('\n')
)
.join('\n') +
'\n'
);
if (allTypes.length > 0) {
// Generate custom types
await fs.writeFile(
path.join(generatedDir, `${generatedTypesFilename}.ts`),
header +
allTypes
.map(({ tsName, values }) =>
[
`export enum ${tsName} {`,
...values.map((value) => ` ${value} = '${value}',`),
'}',
].join('\n')
)
.join('\n') +
'\n'
);
}
// Generate DB entry types
await Promise.all(
@ -160,16 +163,16 @@ const generate = async () => {
}),
}));
const importTypes =
customTypes.length > 0
? [
'import {',
uniq(customTypes)
.map((value) => ` ${value}`)
.join(',\n'),
"} from './custom-types';",
].join('\n') + '\n\n'
: '';
const importTypes = conditionalString(
customTypes.length > 0 &&
[
'import {',
uniq(customTypes)
.map((value) => ` ${value}`)
.join(',\n'),
`} from './${generatedTypesFilename}';`,
].join('\n') + '\n\n'
);
const content =
header +
@ -202,6 +205,7 @@ const generate = async () => {
await fs.writeFile(
path.join(generatedDir, 'index.ts'),
header +
conditionalString(allTypes.length > 0 && `export * from './${generatedTypesFilename}';`) +
generated.map(([file]) => `export * from './${getOutputFileName(file)}';`).join('\n') +
'\n'
);