0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -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. // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
export * from './custom-types';
export * from './oidc-model-instance'; export * from './oidc-model-instance';
export * from './user'; export * from './user';

View file

@ -115,6 +115,7 @@ const generate = async () => {
); );
const generatedDir = 'src/db-entries'; 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 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); 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 }), tsName: camelcase(type.name, { pascalCase: true }),
})); }));
// Generate custom types if (allTypes.length > 0) {
await fs.writeFile( // Generate custom types
path.join(generatedDir, 'custom-types.ts'), await fs.writeFile(
header + path.join(generatedDir, `${generatedTypesFilename}.ts`),
allTypes header +
.map(({ tsName, values }) => allTypes
[ .map(({ tsName, values }) =>
`export enum ${tsName} {`, [
...values.map((value) => ` ${value} = '${value}',`), `export enum ${tsName} {`,
'}', ...values.map((value) => ` ${value} = '${value}',`),
].join('\n') '}',
) ].join('\n')
.join('\n') + )
'\n' .join('\n') +
); '\n'
);
}
// Generate DB entry types // Generate DB entry types
await Promise.all( await Promise.all(
@ -160,16 +163,16 @@ const generate = async () => {
}), }),
})); }));
const importTypes = const importTypes = conditionalString(
customTypes.length > 0 customTypes.length > 0 &&
? [ [
'import {', 'import {',
uniq(customTypes) uniq(customTypes)
.map((value) => ` ${value}`) .map((value) => ` ${value}`)
.join(',\n'), .join(',\n'),
"} from './custom-types';", `} from './${generatedTypesFilename}';`,
].join('\n') + '\n\n' ].join('\n') + '\n\n'
: ''; );
const content = const content =
header + header +
@ -202,6 +205,7 @@ const generate = async () => {
await fs.writeFile( await fs.writeFile(
path.join(generatedDir, 'index.ts'), path.join(generatedDir, 'index.ts'),
header + header +
conditionalString(allTypes.length > 0 && `export * from './${generatedTypesFilename}';`) +
generated.map(([file]) => `export * from './${getOutputFileName(file)}';`).join('\n') + generated.map(([file]) => `export * from './${getOutputFileName(file)}';`).join('\n') +
'\n' '\n'
); );