Skip to content

Commit

Permalink
Merge pull request #4 from momentumframework/cli-generate-fix
Browse files Browse the repository at this point in the history
Cli generate fix
  • Loading branch information
KerryRitter committed Feb 16, 2021
2 parents 8942ba6 + b913fd1 commit 449d9c1
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 266 deletions.
10 changes: 6 additions & 4 deletions cli/commands/generate-file/generate-file.command-handler.ts
@@ -1,5 +1,4 @@
import { Injectable } from "../../deps.ts";
import { FileIOService } from "../../global/mod.ts";
import { GenerateFileCommandParameters } from "./generate-file.command-parameters.ts";
import { SchematicsService } from "./schematics.service.ts";
import { TemplateApplicatorService } from "./template-applicator.service.ts";
Expand All @@ -17,11 +16,14 @@ export class GenerateFileCommandHandler
}

async handle(commandParameters: GenerateFileCommandParameters) {
const { schematicFileContents, schematicFileName } = this.schematicsService
const schematic = this.schematicsService
.getSchematicDetails(commandParameters.schematicType);

const generatedFileName = this.templateApplicator
.applySchematicNameTemplating(commandParameters, schematicFileName);
.applySchematicNameTemplating(
commandParameters,
schematic.fileNameTemplate,
);

commandParameters.files.destinationFile = this.fileFinderService
.getDestinationFile(commandParameters, generatedFileName);
Expand All @@ -41,7 +43,7 @@ export class GenerateFileCommandHandler
.getContainingModuleFile(commandParameters);

const generatedFileContents = this.templateApplicator
.applySchematicTemplating(commandParameters, schematicFileContents);
.applySchematicTemplating(commandParameters, schematic.template);

await this.templateApplicator.writeGeneratedFile(
commandParameters,
Expand Down
14 changes: 13 additions & 1 deletion cli/commands/generate-file/generate-file.module.ts
Expand Up @@ -2,14 +2,26 @@ import { MvModule } from "../../deps.ts";
import { GenerateFileCommandController } from "./generate-file.command-controller.ts";
import { GenerateFileCommandHandler } from "./generate-file.command-handler.ts";
import { SchematicsService } from "./schematics.service.ts";
import {
CONTROLLER_SCHEMATIC,
MODULE_SCHEMATIC,
SERVICE_SCHEMATIC,
} from "./schematics/mod.ts";
import { TemplateApplicatorService } from "./template-applicator.service.ts";

@MvModule({
providers: [
SchematicsService,
TemplateApplicatorService,
GenerateFileCommandController,
GenerateFileCommandHandler,
{
provide: SchematicsService,
useValue: new SchematicsService([
CONTROLLER_SCHEMATIC,
MODULE_SCHEMATIC,
SERVICE_SCHEMATIC,
]),
},
],
exports: [
GenerateFileCommandController,
Expand Down
40 changes: 7 additions & 33 deletions cli/commands/generate-file/schematics.service.ts
@@ -1,43 +1,17 @@
import { Injectable } from "../../deps.ts";
import { FileIOService } from "../../global/mod.ts";
import { SchematicType } from "./generate-file.command-parameters.ts";
import { Schematic } from "./schematics/schematic.interface.ts";

export interface SchematicDetails {
schematicFileContents: string;
schematicFileName: string;
}

@Injectable({ global: false })
export class SchematicsService {
constructor(
private readonly fileIOService: FileIOService,
private readonly schematics: Schematic[],
) {
}

getSchematicDetails(type: SchematicType): SchematicDetails {
const schematicsDirectoryPath = this.fileIOService
.getCliWorkingDirectoryPath("schematics");

const files = this.fileIOService.getDirectoryContents(
schematicsDirectoryPath,
);

const file = files.find((f: Deno.DirEntry) =>
f.isFile && f.name.endsWith(`.${type}.ts.tpl`)
);

if (!file) {
throw new Error(`Could not find schematic for type "${type}".`);
getSchematicDetails(type: SchematicType): Schematic {
const schematic = this.schematics.find((s) => s.type === type);
if (!schematic) {
throw new Error(`Schematic could not be found for type "${type}".`);
}

const schematicFilePath = this.fileIOService.getCliWorkingDirectoryPath([
"schematics",
file.name,
]);

return {
schematicFileContents: this.fileIOService.readFile(schematicFilePath),
schematicFileName: file.name,
};
return schematic;
}
}
11 changes: 11 additions & 0 deletions cli/commands/generate-file/schematics/controller.schematic.ts
@@ -0,0 +1,11 @@
import { Schematic } from "./schematic.interface.ts";

export const CONTROLLER_SCHEMATIC: Schematic = {
type: "controller",
fileNameTemplate: "__name__.controller.ts",
template: `import { Controller } from "__depsPath__";
@Controller("__name__")
export class __className__ {}
`,
};
3 changes: 3 additions & 0 deletions cli/commands/generate-file/schematics/mod.ts
@@ -0,0 +1,3 @@
export * from "./controller.schematic.ts";
export * from "./module.schematic.ts";
export * from "./service.schematic.ts";
11 changes: 11 additions & 0 deletions cli/commands/generate-file/schematics/module.schematic.ts
@@ -0,0 +1,11 @@
import { Schematic } from "./schematic.interface.ts";

export const MODULE_SCHEMATIC: Schematic = {
type: "module",
fileNameTemplate: "__name__.module.ts",
template: `import { MvModule } from "__depsPath__";
@MvModule({})
export class __className__ {}
`,
};
7 changes: 7 additions & 0 deletions cli/commands/generate-file/schematics/schematic.interface.ts
@@ -0,0 +1,7 @@
import { SchematicType } from "../generate-file.command-parameters.ts";

export interface Schematic {
type: SchematicType;
fileNameTemplate: string;
template: string;
}
11 changes: 11 additions & 0 deletions cli/commands/generate-file/schematics/service.schematic.ts
@@ -0,0 +1,11 @@
import { Schematic } from "./schematic.interface.ts";

export const SERVICE_SCHEMATIC: Schematic = {
type: "service",
fileNameTemplate: "__name__.service.ts",
template: `import { Injectable } from "__depsPath__";
@Injectable(__injectableOptions__)
export class __className__ {}
`,
};
6 changes: 1 addition & 5 deletions cli/commands/generate-file/template-applicator.service.ts
Expand Up @@ -50,11 +50,7 @@ export class TemplateApplicatorService {
schematicFileName: string,
) {
return schematicFileName
.replaceAll("__name__", commandParameters.name)
.replaceAll(
".tpl",
"",
);
.replaceAll("__name__", commandParameters.name);
}

async writeGeneratedFile(
Expand Down
17 changes: 0 additions & 17 deletions cli/global/services/file-io.service.ts
Expand Up @@ -14,23 +14,6 @@ export class FileIOService {
return SEP;
}

getCliWorkingDirectoryPath(subpath?: string | string[]) {
let pathParts = dirname(fromFileUrl(import.meta.url))
.split(this.pathDelimiter);

// remove the `global/services/` part of the path
pathParts.pop();
pathParts.pop();

if (subpath?.length) {
pathParts = pathParts.concat(
Array.isArray(subpath) ? subpath : [subpath],
);
}

return join(...pathParts);
}

getUserWorkingDirectoryPath(subpath?: string | string[]) {
let pathParts = [Deno.cwd()];

Expand Down
194 changes: 0 additions & 194 deletions cli/global/services/mvf-manager.service-nodi.ts

This file was deleted.

4 changes: 0 additions & 4 deletions cli/schematics/__name__.controller.ts.tpl

This file was deleted.

0 comments on commit 449d9c1

Please sign in to comment.