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 class-based helpers #18

Merged
merged 1 commit into from
Mar 13, 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
3 changes: 3 additions & 0 deletions .scaffdog/documents/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ output: "**/*"

```gjs
[[name := pascal(inputs.name)-]]

import Component from "@glimmer/component";

export default class [[name]] extends Component {
Expand All @@ -27,6 +28,7 @@ export default class [[name]] extends Component {

```gts
[[name := pascal(inputs.name)-]]

import type { TOC } from '@ember/component/template-only';

export interface [[name]]Signature {
Expand All @@ -47,6 +49,7 @@ export default [[name]];

```gts
[[name := pascal(inputs.name)-]]

import Component from "@glimmer/component";

export interface [[name]]Signature {
Expand Down
21 changes: 19 additions & 2 deletions .scaffdog/documents/helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@ root: "."
output: "**/*"
---

# [[inputs.name]].[[inputs.authoringFormat]]
# [[inputs.classBased ? "!" : ""]][[inputs.name]].[[inputs.authoringFormat]]

```ts
export default function [[camel(inputs.name)]]Helper(positional /*, named*/) {
[[name := camel(inputs.name)-]]

export default function [[name]](positional /*, named*/) {
return positional;
}

```

# [[inputs.classBased ? "" : "!"]][[inputs.name]].[[inputs.authoringFormat]]

```ts
[[name := pascal(inputs.name)-]]

import Helper from "@ember/component/helper";

export default class [[name]] extends Helper {
compute(positional /*, named*/) {
return positional;
}
}

```
1 change: 1 addition & 0 deletions .scaffdog/documents/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ output: "**/*"

```ts
[[name := pascal(inputs.name)-]]

import Service from "@ember/service";

export default class [[name]]Service extends Service {}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pnpm gember component foo --gts
pnpm gember component foo --path="src/-private"

pnpm gember helper foo
pnpm gember helper foo --class
pnpm gember helper foo --ts
pnpm gember helper foo --path="src/-private"

Expand Down
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ yargs(hideBin(process.argv))
description: "The helper's name",
type: "string",
})
.option("class-based", {
alias: ["class"],
default: false,
description: "Generate a class-based helper",
type: "boolean",
})
.option("path", {
default: "",
description: "Generate a helper at a custom path",
Expand All @@ -69,6 +75,7 @@ yargs(hideBin(process.argv))
handler(options) {
generateHelper(options.name, {
authoringFormat: options.ts ? "ts" : "js",
classBased: options.classBased,
path: options.path,
});
},
Expand Down
4 changes: 2 additions & 2 deletions src/generate-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function generateDocument(
const document = documents.find((document) => document.name === documentName);

if (document === undefined) {
throw new Error(`[BUG] Document "${documentName}" not found.`);
throw new Error(`[BUG] Document \`${documentName}\` not found.`);
}

const documentPath = path
Expand All @@ -56,7 +56,7 @@ export async function generateDocument(

console.log(
chalk.green(
`🫚 Generated ${documentName} "${entityName}" at "${relative(cwd, file.path)}".`,
`🫚 Generated ${documentName} \`${entityName}\` at \`${relative(cwd, file.path)}\`.`,
),
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ export function generateHelper(
name: string,
{
authoringFormat = "js",
classBased = false,
cwd = "",
path = "",
}: {
authoringFormat?: "js" | "ts";
classBased?: boolean;
cwd?: string;
path?: string;
} = {},
) {
return generateDocument(DocumentName.Helper, name, {
cwd,
inputs: { authoringFormat },
inputs: { authoringFormat, classBased },
path,
});
}
Expand Down
28 changes: 14 additions & 14 deletions test/__snapshots__/generate-component.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates a \`.gjs\` class-based component 1`] = `
exports[`generates a class-based \`.gjs\` component 1`] = `
"import Component from "@glimmer/component";

export default class Foo extends Component {
Expand All @@ -9,17 +9,7 @@ export default class Foo extends Component {
"
`;

exports[`generates a \`.gjs\` template-only component 1`] = `
"<template>{{yield}}</template>
"
`;

exports[`generates a \`.gjs\` template-only component at a custom path 1`] = `
"<template>{{yield}}</template>
"
`;

exports[`generates a \`.gts\` class-based component 1`] = `
exports[`generates a class-based \`.gts\` component 1`] = `
"import Component from "@glimmer/component";

export interface FooSignature {
Expand All @@ -38,7 +28,17 @@ export default class Foo extends Component<FooSignature> {
"
`;

exports[`generates a \`.gts\` template-only component 1`] = `
exports[`generates a template-only \`.gjs\` component 1`] = `
"<template>{{yield}}</template>
"
`;

exports[`generates a template-only \`.gjs\` component at a custom path 1`] = `
"<template>{{yield}}</template>
"
`;

exports[`generates a template-only \`.gts\` component 1`] = `
"import type { TOC } from '@ember/component/template-only';

export interface FooSignature {
Expand All @@ -55,7 +55,7 @@ export default Foo;
"
`;

exports[`generates a \`.gts\` template-only component at a custom path 1`] = `
exports[`generates a template-only \`.gts\` component at a custom path 1`] = `
"import type { TOC } from '@ember/component/template-only';

export interface FooSignature {
Expand Down
38 changes: 30 additions & 8 deletions test/__snapshots__/generate-helper.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates a \`.js\` helper 1`] = `
"export default function fooHelper(positional /*, named*/) {
exports[`generates a class-based \`.js\` helper 1`] = `
"import Helper from "@ember/component/helper";

export default class Foo extends Helper {
compute(positional /*, named*/) {
return positional;
}
}
"
`;

exports[`generates a class-based \`.ts\` helper 1`] = `
"import Helper from "@ember/component/helper";

export default class Foo extends Helper {
compute(positional /*, named*/) {
return positional;
}
}
"
`;

exports[`generates a function-based \`.js\` helper 1`] = `
"export default function foo(positional /*, named*/) {
return positional;
}
"
`;

exports[`generates a \`.js\` helper at a custom path 1`] = `
"export default function fooHelper(positional /*, named*/) {
exports[`generates a function-based \`.js\` helper at a custom path 1`] = `
"export default function foo(positional /*, named*/) {
return positional;
}
"
`;

exports[`generates a \`.ts\` helper 1`] = `
"export default function fooHelper(positional /*, named*/) {
exports[`generates a function-based \`.ts\` helper 1`] = `
"export default function foo(positional /*, named*/) {
return positional;
}
"
`;

exports[`generates a \`.ts\` helper at a custom path 1`] = `
"export default function fooHelper(positional /*, named*/) {
exports[`generates a function-based \`.ts\` helper at a custom path 1`] = `
"export default function foo(positional /*, named*/) {
return positional;
}
"
Expand Down
12 changes: 6 additions & 6 deletions test/generate-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let cwd: string;

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

it("generates a `.gjs` template-only component", async (ctx) => {
it("generates a template-only `.gjs` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", { cwd });
Expand All @@ -19,7 +19,7 @@ it("generates a `.gjs` template-only component", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.gjs` class-based component", async (ctx) => {
it("generates a class-based `.gjs` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", { classBased: true, cwd });
Expand All @@ -29,7 +29,7 @@ it("generates a `.gjs` class-based component", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.gjs` template-only component at a custom path", async (ctx) => {
it("generates a template-only `.gjs` component at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", { cwd, path: "src/-private" });
Expand All @@ -39,7 +39,7 @@ it("generates a `.gjs` template-only component at a custom path", async (ctx) =>
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.gts` template-only component", async (ctx) => {
it("generates a template-only `.gts` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", { authoringFormat: "gts", cwd });
Expand All @@ -49,7 +49,7 @@ it("generates a `.gts` template-only component", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.gts` class-based component", async (ctx) => {
it("generates a class-based `.gts` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", {
Expand All @@ -63,7 +63,7 @@ it("generates a `.gts` class-based component", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.gts` template-only component at a custom path", async (ctx) => {
it("generates a template-only `.gts` component at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", {
Expand Down
32 changes: 26 additions & 6 deletions test/generate-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let cwd: string;

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

it("generates a `.js` helper", async (ctx) => {
it("generates a function-based `.js` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", { cwd });
Expand All @@ -19,17 +19,17 @@ it("generates a `.js` helper", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.ts` helper", async (ctx) => {
it("generates a class-based `.js` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", { authoringFormat: "ts", cwd });
await generateHelper("foo", { classBased: true, cwd });

const content = await readFile(join(cwd, "src/helpers/foo.ts"), "utf-8");
const content = await readFile(join(cwd, "src/helpers/foo.js"), "utf-8");

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

it("generates a `.js` helper at a custom path", async (ctx) => {
it("generates a function-based `.js` helper at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", { cwd, path: "src/-private" });
Expand All @@ -39,7 +39,27 @@ it("generates a `.js` helper at a custom path", async (ctx) => {
ctx.expect(content).toMatchSnapshot();
});

it("generates a `.ts` helper at a custom path", async (ctx) => {
it("generates a function-based `.ts` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", { authoringFormat: "ts", cwd });

const content = await readFile(join(cwd, "src/helpers/foo.ts"), "utf-8");

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

it("generates a class-based `.ts` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", { authoringFormat: "ts", classBased: true, cwd });

const content = await readFile(join(cwd, "src/helpers/foo.ts"), "utf-8");

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

it("generates a function-based `.ts` helper at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", {
Expand Down
Loading