Skip to content

Commit f14dd58

Browse files
author
nikelborm
committed
scripts
1 parent e0b3324 commit f14dd58

File tree

5 files changed

+216
-159
lines changed

5 files changed

+216
-159
lines changed

backend/scripts/common/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const typeormModelInjectImport = async (
3838
) => {
3939
console.log('entityName: ', entityName);
4040
const typeormImportsRegexp =
41-
/(import {[\nA-Za-z, ]*)( |,[ ]*\n)} from 'typeorm';/g;
41+
/(import {[\nA-Za-z, ]*)( |,\n)} from 'typeorm';/g;
4242

4343
const filePath = join(
4444
dirname(fileURLToPath(import.meta.url)),
@@ -62,10 +62,8 @@ export const typeormModelInjectImport = async (
6262

6363
if (shouldWeInjectImport)
6464
tsFileContent = `${tsFileContent.slice(0, index + group.length)}${
65-
spaceGroup === ' ' ? ', ' : ''
66-
}${searchForInjectable}${
67-
spaceGroup === ' ' ? '' : ' '
68-
}${tsFileContent.slice(index + group.length)}`;
65+
spaceGroup === ' ' ? ', ' : ',\n '
66+
}${searchForInjectable}${tsFileContent.slice(index + group.length)}`;
6967

7068
console.log(tsFileContent);
7169

backend/scripts/generateManyToManyBoilerplate/index.js

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
12
/* eslint-disable security/detect-non-literal-regexp */
23
/* eslint-disable @typescript-eslint/restrict-plus-operands */
34
/* eslint-disable security/detect-non-literal-fs-filename */
@@ -25,17 +26,44 @@ import {
2526
writeNewRepositoryFileAndExtendDirReexportsAndLog,
2627
} from '../common/index.js';
2728

28-
const { first, second, dryRun, selectedFilesToGenerate } = await prompts([
29+
const {
30+
first,
31+
second,
32+
dryRun,
33+
selectedFilesToGenerate,
34+
firstCamelMultiple,
35+
secondCamelMultiple,
36+
intermediatePascal,
37+
} = await prompts([
2938
{
3039
type: 'text',
3140
name: 'first',
3241
message: 'First entity name (fully lower case) with space delimiter',
3342
},
43+
{
44+
type: 'text',
45+
name: 'firstCamelMultiple',
46+
message: 'firstCamel multiple form:',
47+
initial: (prev) => `${camelCase(prev)}s`,
48+
},
3449
{
3550
type: 'text',
3651
name: 'second',
3752
message: 'Second entity name (fully lower case) with space delimiter',
3853
},
54+
{
55+
type: 'text',
56+
name: 'secondCamelMultiple',
57+
message: 'secondCamel multiple form:',
58+
initial: (prev) => `${camelCase(prev)}s`,
59+
},
60+
{
61+
type: 'text',
62+
name: 'intermediatePascal',
63+
message: 'Intermediate pascal entity name',
64+
initial: (prev, values) =>
65+
`${pascalCase(values.first)}To${pascalCase(values.second)}`,
66+
},
3967
{
4068
type: 'toggle',
4169
name: 'dryRun',
@@ -80,26 +108,29 @@ const secondPascal = pascalCase(second);
80108
const secondSnake = snakeCase(second);
81109
const secondCamel = camelCase(second);
82110

111+
const intermediateCamel = camelCase(intermediatePascal);
112+
const intermediateSnake = snakeCase(intermediatePascal);
113+
83114
const getIntermediateModel =
84115
() => `/* eslint-disable @typescript-eslint/no-unsafe-member-access */
85116
/* eslint-disable @typescript-eslint/no-unsafe-return */
86117
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
87118
import { ${secondPascal}, ${firstPascal} } from '.';
88119
${
89120
selectedFilesToGenerate.includes('interfaces')
90-
? `import type { I${firstPascal}To${secondPascal} } from 'src/types';`
121+
? `import type { I${intermediatePascal} } from 'src/types';`
91122
: ''
92123
}
93124
94-
@Entity({ name: '${firstSnake}_to_${secondSnake}' })
95-
export class ${firstPascal}To${secondPascal} ${
125+
@Entity({ name: '${intermediateSnake}' })
126+
export class ${intermediatePascal} ${
96127
selectedFilesToGenerate.includes('interfaces')
97-
? `implements I${firstPascal}To${secondPascal} `
128+
? `implements I${intermediatePascal} `
98129
: ''
99130
}{
100131
@ManyToOne(
101132
() => ${firstPascal},
102-
(${firstCamel}) => ${firstCamel}.${firstCamel}To${secondPascal}Relations,
133+
(${firstCamel}) => ${firstCamel}.${intermediateCamel}Relations,
103134
)
104135
@JoinColumn({ name: '${firstSnake}_id' })
105136
${firstCamel}!: ${firstPascal};
@@ -113,7 +144,7 @@ export class ${firstPascal}To${secondPascal} ${
113144
114145
@ManyToOne(
115146
() => ${secondPascal},
116-
(${secondCamel}) => ${secondCamel}.${firstCamel}To${secondPascal}Relations,
147+
(${secondCamel}) => ${secondCamel}.${intermediateCamel}Relations,
117148
)
118149
@JoinColumn({ name: '${secondSnake}_id' })
119150
${secondCamel}!: ${secondPascal};
@@ -130,7 +161,7 @@ export class ${firstPascal}To${secondPascal} ${
130161
const getIntermediateModelInterface =
131162
() => `import type { I${secondPascal}, I${firstPascal} } from '.';
132163
133-
export class I${firstPascal}To${secondPascal} {
164+
export class I${intermediatePascal} {
134165
${firstCamel}!: I${firstPascal};
135166
136167
${firstCamel}Id!: number;
@@ -147,42 +178,42 @@ import { InjectRepository } from '@nestjs/typeorm';
147178
import { DefaultEntityWithIdentityRepoImplementation } from 'src/tools';
148179
import { Repository } from 'typeorm';
149180
import {
150-
DI_${firstPascal}To${secondPascal}Repo,
181+
DI_${intermediatePascal}Repo,
151182
type RepoTypes,
152-
} from '../di/${firstCamel}To${secondPascal}.repo.di';
153-
import { ${firstPascal}To${secondPascal} } from '../model';
183+
} from '../di/${intermediateCamel}.repo.di';
184+
import { ${intermediatePascal} } from '../model';
154185
155186
@Injectable()
156-
class ${firstPascal}To${secondPascal}Repo
187+
class ${intermediatePascal}Repo
157188
extends DefaultEntityWithIdentityRepoImplementation<RepoTypes>
158-
implements DI_${firstPascal}To${secondPascal}Repo
189+
implements DI_${intermediatePascal}Repo
159190
{
160191
constructor(
161-
@InjectRepository(${firstPascal}To${secondPascal})
162-
protected override readonly repo: Repository<${firstPascal}To${secondPascal}>,
192+
@InjectRepository(${intermediatePascal})
193+
protected override readonly repo: Repository<${intermediatePascal}>,
163194
) {
164195
super(repo);
165196
}
166197
}
167198
168-
export const ${firstPascal}To${secondPascal}RepoDIProvider: Provider = {
169-
provide: DI_${firstPascal}To${secondPascal}Repo,
170-
useClass: ${firstPascal}To${secondPascal}Repo,
199+
export const ${intermediatePascal}RepoDIProvider: Provider = {
200+
provide: DI_${intermediatePascal}Repo,
201+
useClass: ${intermediatePascal}Repo,
171202
};
172203
`;
173204

174205
const getIntermediateModelDI_Repo = () => `import {
175206
type EntityRepoMethodTypes,
176-
type I${firstPascal}To${secondPascal},
207+
type I${intermediatePascal},
177208
IDefaultIdentityRepo,
178209
} from 'src/types';
179210
180-
export abstract class DI_${firstPascal}To${secondPascal}Repo extends IDefaultIdentityRepo<RepoTypes> {}
211+
export abstract class DI_${intermediatePascal}Repo extends IDefaultIdentityRepo<RepoTypes> {}
181212
182213
export type RepoTypes = EntityRepoMethodTypes<
183-
I${firstPascal}To${secondPascal},
214+
I${intermediatePascal},
184215
{
185-
EntityName: '${firstPascal}To${secondPascal}';
216+
EntityName: '${intermediatePascal}';
186217
187218
RequiredToCreateAndSelectRegularPlainKeys: '${firstCamel}Id' | '${secondCamel}Id';
188219
@@ -199,87 +230,86 @@ export type RepoTypes = EntityRepoMethodTypes<
199230
const getFirstModelMixin = () => `
200231
@ManyToMany(
201232
() => ${secondPascal},
202-
(${secondCamel}) => ${secondCamel}.${firstCamel}sWithThat${secondPascal},
233+
(${secondCamel}) => ${secondCamel}.${firstCamelMultiple}WithThat${secondPascal},
203234
)
204235
@JoinTable({
205-
name: '${firstSnake}_to_${secondSnake}',
236+
name: '${intermediateSnake}',
206237
joinColumn: { name: '${firstSnake}_id', referencedColumnName: 'id' },
207238
inverseJoinColumn: { name: '${secondSnake}_id', referencedColumnName: 'id' },
208239
// synchronize is important flag! Without it your migrations will have two conflicting declarations for question_to_category table
209240
// from https://github.com/typeorm/typeorm/blob/master/docs/decorator-reference.md#jointable
210241
synchronize: false,
211242
})
212-
${secondCamel}s!: ${secondPascal}[];
243+
${secondCamelMultiple}!: ${secondPascal}[];
213244
214245
@OneToMany(
215-
() => ${firstPascal}To${secondPascal},
216-
(${firstCamel}To${secondPascal}) => ${firstCamel}To${secondPascal}.${firstCamel},
246+
() => ${intermediatePascal},
247+
(${intermediateCamel}) => ${intermediateCamel}.${firstCamel},
217248
)
218-
${firstCamel}To${secondPascal}Relations!: ${firstPascal}To${secondPascal}[];
249+
${intermediateCamel}Relations!: ${intermediatePascal}[];
219250
`;
220251

221252
const getFirstModelImportMixin = () => `
222-
import { ${secondPascal}, ${firstPascal}To${secondPascal} } from '.';`;
253+
import { ${secondPascal}, ${intermediatePascal} } from '.';`;
223254

224255
const getFirstInterfaceImportMixin = () => `
225-
import type { I${secondPascal}, I${firstPascal}To${secondPascal} } from '.';`;
256+
import type { I${secondPascal}, I${intermediatePascal} } from '.';`;
226257

227258
const getSecondModelMixin = () => `
228259
@ManyToMany(
229260
() => ${firstPascal},
230-
(${firstCamel}) => ${firstCamel}.${secondCamel}s,
261+
(${firstCamel}) => ${firstCamel}.${secondCamelMultiple},
231262
)
232-
${firstCamel}sWithThat${secondPascal}!: ${firstPascal}[];
263+
${firstCamelMultiple}WithThat${secondPascal}!: ${firstPascal}[];
233264
234265
@OneToMany(
235-
() => ${firstPascal}To${secondPascal},
236-
(${firstCamel}To${secondPascal}) => ${firstCamel}To${secondPascal}.${secondCamel},
266+
() => ${intermediatePascal},
267+
(${intermediateCamel}) => ${intermediateCamel}.${secondCamel},
237268
)
238-
${firstCamel}To${secondPascal}Relations!: ${firstPascal}To${secondPascal}[];
269+
${intermediateCamel}Relations!: ${intermediatePascal}[];
239270
`;
240271

241272
const getSecondModelImportMixin = () => `
242-
import { ${firstPascal}, ${firstPascal}To${secondPascal} } from '.';`;
273+
import { ${firstPascal}, ${intermediatePascal} } from '.';`;
243274

244275
const getSecondInterfaceImportMixin = () => `
245-
import type { I${firstPascal}, I${firstPascal}To${secondPascal} } from '.';`;
276+
import type { I${firstPascal}, I${intermediatePascal} } from '.';`;
246277

247278
const getFirstModelInterfaceMixin = () => `
248-
${secondCamel}s!: I${secondPascal}[];
279+
${secondCamelMultiple}!: I${secondPascal}[];
249280
250-
${firstCamel}To${secondPascal}Relations!: I${firstPascal}To${secondPascal}[];
281+
${intermediateCamel}Relations!: I${intermediatePascal}[];
251282
`;
252283

253284
const getSecondModelInterfaceMixin = () => `
254-
${firstCamel}sWithThat${secondPascal}!: I${firstPascal}[];
285+
${firstCamelMultiple}WithThat${secondPascal}!: I${firstPascal}[];
255286
256-
${firstCamel}To${secondPascal}Relations!: I${firstPascal}To${secondPascal}[];
287+
${intermediateCamel}Relations!: I${intermediatePascal}[];
257288
`;
258289

259290
const getMixinToFirstModelInRelationMap =
260-
() => ` ${secondCamel}s: ['${secondPascal}'],
261-
${firstCamel}To${secondPascal}Relations: ['${firstPascal}To${secondPascal}'],
291+
() => ` ${secondCamelMultiple}: ['${secondPascal}'],
292+
${intermediateCamel}Relations: ['${intermediatePascal}'],
262293
`;
263294

264295
const getMixinToSecondModelInRelationMap =
265-
() => ` ${firstCamel}sWithThat${secondPascal}: ['${firstPascal}'],
266-
${firstCamel}To${secondPascal}Relations: ['${firstPascal}To${secondPascal}'],
296+
() => ` ${firstCamelMultiple}WithThat${secondPascal}: ['${firstPascal}'],
297+
${intermediateCamel}Relations: ['${intermediatePascal}'],
267298
`;
268299

269-
const getIntermediateModelToRelationMapMixin =
270-
() => ` ${firstPascal}To${secondPascal}: {
300+
const getIntermediateModelToRelationMapMixin = () => ` ${intermediatePascal}: {
271301
identityKeys: ['${firstCamel}Id', '${secondCamel}Id'],
272302
relationToEntityNameMap: {
273303
${firstCamel}: '${firstPascal}',
274304
${secondCamel}: '${secondPascal}',
275-
// ${firstPascal}To${secondPascal} relationToEntityNameMap token
305+
// ${intermediatePascal} relationToEntityNameMap token
276306
},
277307
},
278308
`;
279309

280310
if (selectedFilesToGenerate.includes('models')) {
281311
await writeNewModelFileAndExtendDirReexportsAndLog(
282-
`${firstPascal}To${secondPascal}`,
312+
`${intermediatePascal}`,
283313
getIntermediateModel(),
284314
dryRun,
285315
);
@@ -308,7 +338,7 @@ if (selectedFilesToGenerate.includes('models')) {
308338

309339
if (selectedFilesToGenerate.includes('interfaces')) {
310340
await writeNewModelInterfaceFileAndExtendDirReexportsAndLog(
311-
`${firstPascal}To${secondPascal}`,
341+
`${intermediatePascal}`,
312342
getIntermediateModelInterface(),
313343
dryRun,
314344
);
@@ -340,23 +370,23 @@ if (selectedFilesToGenerate.includes('interfaces')) {
340370

341371
if (selectedFilesToGenerate.includes('repository')) {
342372
await writeNewRepositoryFileAndExtendDirReexportsAndLog(
343-
`${firstPascal}To${secondPascal}`,
373+
`${intermediatePascal}`,
344374
getIntermediateModelRepo(),
345375
dryRun,
346376
);
347377
}
348378

349379
if (selectedFilesToGenerate.includes('DI_Repository')) {
350380
await writeNewDI_RepoFileAndExtendDirReexportsAndLog(
351-
`${firstPascal}To${secondPascal}`,
381+
`${intermediatePascal}`,
352382
getIntermediateModelDI_Repo(),
353383
dryRun,
354384
);
355385
}
356386

357387
if (selectedFilesToGenerate.includes('relationMapExtension')) {
358388
await appendRelationMapMixinToFileAndLog(
359-
`${firstPascal}To${secondPascal}`,
389+
`${intermediatePascal}`,
360390
getIntermediateModelToRelationMapMixin(),
361391
dryRun,
362392
/ {2}\/\/ RelationMapValue end token/g,

0 commit comments

Comments
 (0)