Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a --include-curly-component-invocations option #8

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ Generate a template registry at a custom path.
npx @bertdeblock/generate-template-registry@latest --path="app/glint/template-registry.ts"
```

### `--include-curly-component-invocations`

Generate a template registry including curly component invocations. By default, curly component invocations are not included.

```shell
npx @bertdeblock/generate-template-registry@latest --include-curly-component-invocations
```

## Caveats

- If your app or addon has components, helpers or modifiers with the same name, duplicate template registry entries will be generated, which will need to be fixed manually
11 changes: 10 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import { generateTemplateRegistry } from "./generate-template-registry.js";
export async function cli() {
const options = await yargs(hideBin(process.argv))
.options({
"include-curly-component-invocations": {
default: false,
describe:
"Generate a template registry including curly component invocations",
type: "boolean",
},
path: {
describe: "Generate a template registry at a custom path",
type: "string",
},
})
.strict().argv;

await generateTemplateRegistry(cwd(), { path: options.path });
await generateTemplateRegistry(cwd(), {
includeCurlyComponentInvocations: options.includeCurlyComponentInvocations,
path: options.path,
});
}
16 changes: 14 additions & 2 deletions src/generate-template-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ const TAB = " ";

export async function generateTemplateRegistry(
cwd: string,
options: { path?: string } = {},
options: {
includeCurlyComponentInvocations?: boolean;
path?: string;
} = {
includeCurlyComponentInvocations: false,
path: "",
},
) {
let packageJson: EmberPackageJson;

Expand Down Expand Up @@ -86,11 +92,17 @@ export async function generateTemplateRegistry(
}

importsContent += `import type ${entry.identifier} from "${importRoot}/${type}/${entryName}";${EOL}`;
entriesContent += `${TAB}${toRegistryKey(entry.name)}: typeof ${entry.identifier};${EOL}`;

if (type === EntryType.Components) {
entriesContent += `${TAB}${toRegistryKey(toAngleBracketNotation(entry.name))}: typeof ${entry.identifier};${EOL}`;
}

if (
type !== EntryType.Components ||
options.includeCurlyComponentInvocations
) {
entriesContent += `${TAB}${toRegistryKey(entry.name)}: typeof ${entry.identifier};${EOL}`;
}
}

templateRegistryImportsContent.push(importsContent);
Expand Down
49 changes: 33 additions & 16 deletions test/__snapshots__/generate-template-registry.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ import type Foo3 from "app/modifiers/foo";

export default interface AppRegistry {
// components
bar: typeof Bar;
Bar: typeof Bar;
"foo-bar": typeof FooBar;
FooBar: typeof FooBar;
foo: typeof Foo;
Foo: typeof Foo;
"foo/bar": typeof FooBar2;
"Foo::Bar": typeof FooBar2;

// helpers
Expand All @@ -48,13 +44,9 @@ import type Foo3 from "v1-addon/modifiers/foo";

export default interface V1AddonRegistry {
// components
bar: typeof Bar;
Bar: typeof Bar;
"foo-bar": typeof FooBar;
FooBar: typeof FooBar;
foo: typeof Foo;
Foo: typeof Foo;
"foo/bar": typeof FooBar2;
"Foo::Bar": typeof FooBar2;

// helpers
Expand All @@ -81,13 +73,9 @@ import type Foo3 from "./modifiers/foo.ts";

export default interface V2AddonRegistry {
// components
bar: typeof Bar;
Bar: typeof Bar;
"foo-bar": typeof FooBar;
FooBar: typeof FooBar;
foo: typeof Foo;
Foo: typeof Foo;
"foo/bar": typeof FooBar2;
"Foo::Bar": typeof FooBar2;

// helpers
Expand All @@ -114,13 +102,9 @@ import type Foo3 from "app/modifiers/foo";

export default interface AppRegistry {
// components
bar: typeof Bar;
Bar: typeof Bar;
"foo-bar": typeof FooBar;
FooBar: typeof FooBar;
foo: typeof Foo;
Foo: typeof Foo;
"foo/bar": typeof FooBar2;
"Foo::Bar": typeof FooBar2;

// helpers
Expand All @@ -131,3 +115,36 @@ export default interface AppRegistry {
}
"
`;

exports[`generates a template registry including curly component invocations 1`] = `
"// components
import type Bar from "app/components/bar";
import type FooBar from "app/components/foo-bar";
import type Foo from "app/components/foo";
import type FooBar2 from "app/components/foo/bar";

// helpers
import type Foo2 from "app/helpers/foo";

// modifiers
import type Foo3 from "app/modifiers/foo";

export default interface AppRegistry {
// components
Bar: typeof Bar;
bar: typeof Bar;
FooBar: typeof FooBar;
"foo-bar": typeof FooBar;
Foo: typeof Foo;
foo: typeof Foo;
"Foo::Bar": typeof FooBar2;
"foo/bar": typeof FooBar2;

// helpers
foo: typeof Foo2;

// modifiers
foo: typeof Foo3;
}
"
`;
15 changes: 15 additions & 0 deletions test/generate-template-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ it("generates a template registry at a custom path", async (ctx) => {
ctx.expect(templateRegistryContent).toMatchSnapshot();
});

it("generates a template registry including curly component invocations", async (ctx) => {
cwd = await copyBlueprint("app");

await generateTemplateRegistry(cwd, {
includeCurlyComponentInvocations: true,
});

const templateRegistryContent = await readFile(
join(cwd, "app/template-registry.ts"),
"utf-8",
);

ctx.expect(templateRegistryContent).toMatchSnapshot();
});

async function copyBlueprint(name: "app" | "v1-addon" | "v2-addon") {
const cwd = join("test/output", uuidv4());

Expand Down
Loading