Skip to content

Commit

Permalink
chore: suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
peternhale committed Aug 6, 2024
1 parent 25b4956 commit 0b7bda4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 58 deletions.
48 changes: 0 additions & 48 deletions src/generators/index.ts

This file was deleted.

19 changes: 15 additions & 4 deletions src/service/templateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { type CreateOutput, TemplateType } from '../utils/types';
import { Generators, generators } from '../generators';
import {
type CreateOutput,
GeneratorClass,
generators,
TemplateOptions,
TemplateType,
} from '../utils/types';
import { nls } from '../i18n';

export function importGenerator(templateType: TemplateType) {
const generator = generators.get(templateType) as Generators;
export function importGenerator<TOptions extends TemplateOptions>(
templateType: TemplateType
): GeneratorClass<TOptions> {
const generator = generators.get(templateType);
if (!generator) {
throw new Error(nls.localize('templateTypeNotFound'));
}
return generator;
}

Expand Down
57 changes: 52 additions & 5 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,37 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export type CreateOutput = {
outputDir: string;
created: string[];
rawOutput: string;
};
import AnalyticsTemplateGenerator from '../generators/analyticsTemplateGenerator';
import ApexClassGenerator from '../generators/apexClassGenerator';
import ApexTriggerGenerator from '../generators/apexTriggerGenerator';
import LightningAppGenerator from '../generators/lightningAppGenerator';
import LightningComponentGenerator from '../generators/lightningComponentGenerator';
import LightningEventGenerator from '../generators/lightningEventGenerator';
import LightningInterfaceGenerator from '../generators/lightningInterfaceGenerator';
import LightningTestGenerator from '../generators/lightningTestGenerator';
import ProjectGenerator from '../generators/projectGenerator';
import StaticResourceGenerator from '../generators/staticResourceGenerator';
import VisualforceComponentGenerator from '../generators/visualforceComponentGenerator';
import VisualforcePageGenerator from '../generators/visualforcePageGenerator';
import { BaseGenerator } from '../generators/baseGenerator';

export type GeneratorClass<TOptions extends TemplateOptions> = new (
options: TOptions
) => BaseGenerator<TOptions>;

export type Generators =
| typeof AnalyticsTemplateGenerator
| typeof ApexClassGenerator
| typeof ApexTriggerGenerator
| typeof LightningAppGenerator
| typeof LightningComponentGenerator
| typeof LightningEventGenerator
| typeof LightningTestGenerator
| typeof LightningInterfaceGenerator
| typeof ProjectGenerator
| typeof StaticResourceGenerator
| typeof VisualforceComponentGenerator
| typeof VisualforcePageGenerator;

/**
* Available Template types
Expand All @@ -33,6 +59,27 @@ export enum TemplateType {
StaticResource,
}

export const generators = new Map<TemplateType, GeneratorClass<any>>([
[TemplateType.AnalyticsTemplate, AnalyticsTemplateGenerator],
[TemplateType.ApexClass, ApexClassGenerator],
[TemplateType.ApexTrigger, ApexTriggerGenerator],
[TemplateType.LightningApp, LightningAppGenerator],
[TemplateType.LightningComponent, LightningComponentGenerator],
[TemplateType.LightningEvent, LightningEventGenerator],
[TemplateType.LightningInterface, LightningInterfaceGenerator],
[TemplateType.LightningTest, LightningTestGenerator],
[TemplateType.Project, ProjectGenerator],
[TemplateType.StaticResource, StaticResourceGenerator],
[TemplateType.VisualforceComponent, VisualforceComponentGenerator],
[TemplateType.VisualforcePage, VisualforcePageGenerator],
]);

export type CreateOutput = {
outputDir: string;
created: string[];
rawOutput: string;
};

/**
* Template Options
* If not supplied, the apiversion and outputdir use default values.
Expand Down
2 changes: 1 addition & 1 deletion test/service/templateService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('TemplateService', () => {
describe('Generators', () => {
it('should have a generator for each TemplateType', () => {
const templateTypes = Object.values(TemplateType).filter(
(v) => !isNaN(Number(v))
(value) => typeof value === 'number'
);

for (const templateType of templateTypes) {
Expand Down

0 comments on commit 0b7bda4

Please sign in to comment.