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 5f1728a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 31 deletions.
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: "Custom path to save the template registry file to",
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
36 changes: 29 additions & 7 deletions test/generate-template-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import fsExtra from "fs-extra";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import recursiveCopy from "recursive-copy";
import { it } from "vitest";
import { v4 as uuidv4 } from "uuid";
import { afterEach, it } from "vitest";
import { generateTemplateRegistry } from "../src/generate-template-registry.ts";

let cwd: string;

afterEach(() => fsExtra.remove(cwd));

it("generates a template registry for an app", async (ctx) => {
const cwd = await copyBlueprint("app");
cwd = await copyBlueprint("app");

await generateTemplateRegistry(cwd);

const templateRegistryContent = await readFile(
Expand All @@ -18,7 +24,8 @@ it("generates a template registry for an app", async (ctx) => {
});

it("generates a template registry for a v1 addon", async (ctx) => {
const cwd = await copyBlueprint("v1-addon");
cwd = await copyBlueprint("v1-addon");

await generateTemplateRegistry(cwd);

const templateRegistryContent = await readFile(
Expand All @@ -30,7 +37,8 @@ it("generates a template registry for a v1 addon", async (ctx) => {
});

it("generates a template registry for a v2 addon", async (ctx) => {
const cwd = await copyBlueprint("v2-addon");
cwd = await copyBlueprint("v2-addon");

await generateTemplateRegistry(cwd);

const templateRegistryContent = await readFile(
Expand All @@ -41,10 +49,24 @@ it("generates a template registry for a v2 addon", async (ctx) => {
ctx.expect(templateRegistryContent).toMatchSnapshot();
});

async function copyBlueprint(name) {
const cwd = join("test/output", name);
it("generates a template registry at a custom path", async (ctx) => {
cwd = await copyBlueprint("app");

await generateTemplateRegistry(cwd, {
path: "./app/glint/template-registry.ts",
});

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

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

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

await fsExtra.remove(cwd);
await recursiveCopy(join("test", "blueprints", name), cwd);

return cwd;
Expand Down

0 comments on commit 5f1728a

Please sign in to comment.