Skip to content

Commit b689fa3

Browse files
authored
Merge pull request #94 from evoactivity/route-template
Support generating route templates
2 parents e6a43a4 + 5994ec1 commit b689fa3

File tree

12 files changed

+1021
-25
lines changed

12 files changed

+1021
-25
lines changed

src/cli.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { GemberError, logGemberErrors } from "./errors.js";
99
import {
1010
generators,
1111
getGenerator,
12+
getTemplateGenerator,
1213
getTestGenerator,
1314
} from "./generators/generators.js";
1415
import { readOwnPackageJsonSync } from "./internal.js";
@@ -135,6 +136,16 @@ function generatorCommands(deprecated?: boolean): SubCommandsDef {
135136
);
136137
}
137138
}
139+
140+
if (context.args.template) {
141+
if (generator.args.find((arg) => arg.name === "template")) {
142+
await getTemplateGenerator().run(context.args);
143+
} else {
144+
logger.warn(
145+
`You passed the \`--template\` option, but the \`${generator.name}\` generator does not support generating a template.`,
146+
);
147+
}
148+
}
138149
});
139150
},
140151
};

src/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ export type Config = {
159159
log?: boolean;
160160
// Generate a route at a custom path, e.g. `--path=src/-private`:
161161
path?: string;
162+
// Generate a corresponding template:
163+
template?: boolean;
162164
// Generate a corresponding route-test:
163165
test?: boolean;
164166
// Generate a `.ts` route, instead of a `.js` route:
@@ -178,6 +180,22 @@ export type Config = {
178180
// Generate a `.ts` route-test, instead of a `.js` route-test:
179181
typescript?: boolean;
180182
};
183+
template?: {
184+
// Generate a `class-based` template, instead of a `template-only` template:
185+
classBased?: boolean;
186+
// Copy the generated template to the clipboard, instead of writing it to disk:
187+
copy?: boolean;
188+
// The current working directory to run the template generator in:
189+
cwd?: string;
190+
// Destroy a template by name:
191+
destroy?: boolean;
192+
// Log the generated template to the console, instead of writing it to disk:
193+
log?: boolean;
194+
// Generate a template at a custom path, e.g. `--path=src/-private`:
195+
path?: string;
196+
// Generate a `.gts` template, instead of a `.gjs` template:
197+
typescript?: boolean;
198+
};
181199
service?: {
182200
// Copy the generated service to the clipboard, instead of writing it to disk:
183201
copy?: boolean;

src/generators/generator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ export function path(): GeneratorArgFactory {
358358
});
359359
}
360360

361+
export function template(): GeneratorArgFactory {
362+
return () => ({
363+
description: `Generate a corresponding template`,
364+
name: "template",
365+
type: "boolean",
366+
});
367+
}
368+
361369
export function test(): GeneratorArgFactory {
362370
return (generatorName) => ({
363371
description: `Generate a corresponding ${testGeneratorName(generatorName)}`,

src/generators/generators.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
defineTestGenerator,
66
namedExport,
77
nested,
8+
template,
89
test,
910
testGeneratorName,
1011
typescript,
@@ -73,7 +74,7 @@ export const generators: Generator[] = [
7374
}),
7475

7576
defineGenerator({
76-
args: [test(), typescript()],
77+
args: [template(), test(), typescript()],
7778
name: "route",
7879
}),
7980

@@ -94,6 +95,17 @@ export const generators: Generator[] = [
9495
testsDir: "unit",
9596
}),
9697

98+
defineGenerator({
99+
args: [
100+
classBased({ functionBasedName: "template-only" }),
101+
typescript({ gts: true }),
102+
],
103+
modifyTemplateFile: (templateFile) => {
104+
templateFile.name = "component";
105+
},
106+
name: "template",
107+
}),
108+
97109
defineGenerator({
98110
args: [namedExport(), test(), typescript()],
99111
name: "util",
@@ -121,3 +133,7 @@ export function getGenerator(generatorName: string): Generator {
121133
export function getTestGenerator(generatorName: string): Generator {
122134
return getGenerator(testGeneratorName(generatorName));
123135
}
136+
137+
export function getTemplateGenerator(): Generator {
138+
return getGenerator("template");
139+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Component from '@glimmer/component';
2+
3+
export default class {{name.pascal}} extends Component {
4+
<template>\{{yield}}</template>
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Component from '@glimmer/component';
2+
3+
export interface {{name.signature}} {
4+
Args: {
5+
model: unknown;
6+
controller: unknown;
7+
};
8+
Blocks: {
9+
default: [];
10+
};
11+
Element: null;
12+
}
13+
14+
export default class {{name.pascal}} extends Component<{{name.signature}}> {
15+
<template>
16+
\{{yield}}
17+
</template>
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template>\{{yield}}</template>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { TOC } from '@ember/component/template-only';
2+
3+
export interface {{name.signature}} {
4+
Args: {
5+
model: unknown;
6+
controller: unknown;
7+
};
8+
Blocks: {
9+
default: [];
10+
};
11+
Element: null;
12+
}
13+
14+
const {{name.pascal}}: TOC<{{name.signature}}> = <template>\{{yield}}</template>;
15+
16+
export default {{name.pascal}};

0 commit comments

Comments
 (0)