Skip to content

Commit

Permalink
Support a --path option
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Mar 1, 2024
1 parent 05b9ed1 commit c4fc02b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 31 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ or
bunx @bertdeblock/generate-template-registry@latest
```

### Options

#### `--path`

Generate a template registry at a custom path

```shell
npx @bertdeblock/generate-template-registry@latest --path="app/glint/template-registry.ts"
```

## 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
4 changes: 2 additions & 2 deletions bin/generate-template-registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

// eslint-disable-next-line n/no-missing-import
import { generateTemplateRegistry } from "../dist/generate-template-registry.js";
import { cli } from "../dist/cli.js";

generateTemplateRegistry();
cli();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
"change-case": "^5.4.3",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0"
"fs-extra": "^11.2.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@release-it-plugins/lerna-changelog": "^6.1.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.11.20",
"@types/recursive-readdir": "^2.2.4",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@vitest/coverage-v8": "^1.2.2",
Expand All @@ -50,6 +52,7 @@
"release-it": "^17.0.3",
"type-fest": "^4.10.3",
"typescript": "^5.3.3",
"uuid": "^9.0.1",
"vitest": "^1.2.2"
},
"packageManager": "[email protected]",
Expand Down
40 changes: 24 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { cwd } from "node:process";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { generateTemplateRegistry } from "./generate-template-registry.js";

export async function cli() {
const options = await yargs(hideBin(process.argv))
.options({
path: {
describe: "Generate a template registry at a custom path",
type: "string",
},
})
.strict().argv;

await generateTemplateRegistry(cwd(), { path: options.path });
}
18 changes: 13 additions & 5 deletions src/generate-template-registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import chalk from "chalk";
import { pascalCase } from "change-case";
import { execa } from "execa";
import { readJson } from "fs-extra/esm";
import { ensureDir, readJson } from "fs-extra/esm";
import { writeFile } from "node:fs/promises";
import { EOL } from "node:os";
import { join } from "node:path";
import { cwd as processCwd } from "node:process";
import { isAbsolute, join, parse } from "node:path";
import {
getEntries,
isAddon,
Expand All @@ -22,7 +21,10 @@ import {

const TAB = " ";

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

try {
Expand Down Expand Up @@ -95,12 +97,18 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
templateRegistryEntriesContent.push(entriesContent);
}

const templateRegistryPath = join(cwd, entriesDir, "template-registry.ts");
const templateRegistryPath = options.path
? isAbsolute(options.path)
? options.path
: join(cwd, options.path)
: join(cwd, entriesDir, "template-registry.ts");

const templateRegistryContent = `${templateRegistryImportsContent.join(EOL)}
export default interface ${pascalCase(packageJson.name)}Registry {
${templateRegistryEntriesContent.join(EOL)}}
`;

await ensureDir(parse(templateRegistryPath).dir);
await writeFile(templateRegistryPath, templateRegistryContent);

try {
Expand Down
33 changes: 33 additions & 0 deletions test/__snapshots__/generate-template-registry.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates a template registry at a custom path 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;
"foo-bar": typeof FooBar;
FooBar: typeof FooBar;
foo: typeof Foo;
Foo: typeof Foo;
"foo/bar": typeof FooBar2;
"Foo::Bar": typeof FooBar2;
// helpers
foo: typeof Foo2;
// modifiers
foo: typeof Foo3;
}
"
`;

exports[`generates a template registry for a v1 addon 1`] = `
"// components
import type Bar from "v1-addon/components/bar";
Expand Down
Loading

0 comments on commit c4fc02b

Please sign in to comment.