Skip to content

Commit

Permalink
Merge pull request #10 from bertdeblock/internal-improvements
Browse files Browse the repository at this point in the history
Various internal improvements
  • Loading branch information
bertdeblock authored Mar 11, 2024
2 parents 7cc2f48 + a450559 commit 1fd952b
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .scaffdog/documents/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ root: "."
output: "**/*"
---

# [[inputs.name + (inputs.gts ? ".gts" : ".gjs")]]
# [[inputs.name]].[[inputs.authoringFormat]]

```gts
[[name := pascal(inputs.name)-]]
import Component from "@glimmer/component";
[[if inputs.gts]]
[[if inputs.authoringFormat == "gts"]]
export interface [[name]]Signature {
Args: {};
Blocks: {
Expand Down
2 changes: 1 addition & 1 deletion .scaffdog/documents/helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ root: "."
output: "**/*"
---

# [[inputs.name + (inputs.ts ? ".ts" : ".js")]]
# [[inputs.name]].[[inputs.authoringFormat]]

```ts
export default function [[camel(inputs.name)]]Helper(positional /*, named*/) {
Expand Down
2 changes: 1 addition & 1 deletion .scaffdog/documents/modifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ root: "."
output: "**/*"
---

# [[inputs.name + (inputs.ts ? ".ts" : ".js")]]
# [[inputs.name]].[[inputs.authoringFormat]]

```ts
import { modifier } from "ember-modifier";
Expand Down
4 changes: 2 additions & 2 deletions .scaffdog/documents/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ root: "."
output: "**/*"
---

# [[inputs.name + (inputs.ts ? ".ts" : ".js")]]
# [[inputs.name]].[[inputs.authoringFormat]]

```ts
[[name := pascal(inputs.name)-]]
import Service from "@ember/service";

export default class [[name]]Service extends Service {}
[[if inputs.ts]]
[[if inputs.authoringFormat == "ts"]]
declare module "@ember/service" {
interface Registry {
"[[inputs.name]]": [[name]]Service;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @bertdeblock/gember
# 🫚 gember

[![CI](https://github.com/bertdeblock/gember/workflows/CI/badge.svg)](https://github.com/bertdeblock/gember/actions?query=workflow%3ACI)
[![NPM Version](https://badge.fury.io/js/%40bertdeblock%2Fgember.svg)](https://badge.fury.io/js/%40bertdeblock%2Fgember)
Expand Down
17 changes: 8 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cwd } from "node:process";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import {
Expand Down Expand Up @@ -32,8 +31,8 @@ yargs(hideBin(process.argv))
});
},
handler(options) {
generateComponent(options.name, cwd(), {
gts: options.gts,
generateComponent(options.name, {
authoringFormat: options.gts ? "gts" : "gjs",
path: options.path,
});
},
Expand Down Expand Up @@ -61,9 +60,9 @@ yargs(hideBin(process.argv))
});
},
handler(options) {
generateHelper(options.name, cwd(), {
generateHelper(options.name, {
authoringFormat: options.ts ? "ts" : "js",
path: options.path,
ts: options.ts,
});
},
})
Expand All @@ -90,9 +89,9 @@ yargs(hideBin(process.argv))
});
},
handler(options) {
generateModifier(options.name, cwd(), {
generateModifier(options.name, {
authoringFormat: options.ts ? "ts" : "js",
path: options.path,
ts: options.ts,
});
},
})
Expand All @@ -119,9 +118,9 @@ yargs(hideBin(process.argv))
});
},
handler(options) {
generateService(options.name, cwd(), {
generateService(options.name, {
authoringFormat: options.ts ? "ts" : "js",
path: options.path,
ts: options.ts,
});
},
})
Expand Down
22 changes: 17 additions & 5 deletions src/generate-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import chalk from "chalk";
import { execa } from "execa";
import { ensureDir } from "fs-extra";
import { writeFile } from "node:fs/promises";
import { dirname, isAbsolute, join, parse } from "node:path";
import { dirname, isAbsolute, join, parse, relative } from "node:path";
import { cwd as processCwd } from "node:process";
import { fileURLToPath } from "node:url";
import { type GenerateInputs, loadScaffdog } from "scaffdog";
import { type DocumentName } from "./types.js";
Expand All @@ -17,8 +18,15 @@ const DOCUMENT_NAME_DIRECTORY: Record<DocumentName, string> = {
export async function generateDocument(
documentName: DocumentName,
entityName: string,
cwd: string,
{ inputs = {}, path = "" }: { inputs?: GenerateInputs; path?: string } = {},
{
cwd = processCwd(),
inputs = {},
path = "",
}: {
cwd?: string;
inputs?: GenerateInputs;
path?: string;
} = {},
) {
const directory = dirname(fileURLToPath(import.meta.url));
const scaffdog = await loadScaffdog(join(directory, "..", ".scaffdog"));
Expand Down Expand Up @@ -50,9 +58,13 @@ export async function generateDocument(
// 🤫
}

console.log(chalk.green(`Generated "${file.path}"`));
console.log(
chalk.green(
`🫚 Generated ${documentName} "${entityName}" at "${relative(cwd, file.path)}".`,
),
);
}
} else {
throw new Error(`Document "${documentName}" not found.`);
throw new Error(`[BUG] Document "${documentName}" not found.`);
}
}
72 changes: 52 additions & 20 deletions src/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,76 @@ import { DocumentName } from "./types.js";

export function generateComponent(
name: string,
cwd: string,
{ gts = false, path = "" } = {},
{
authoringFormat = "gjs",
cwd = "",
path = "",
}: {
authoringFormat?: "gjs" | "gts";
cwd?: string;
path?: string;
} = {},
) {
return generateDocument(DocumentName.Component, name, cwd, {
inputs: { gts },
path: path,
return generateDocument(DocumentName.Component, name, {
cwd,
inputs: { authoringFormat },
path,
});
}

export function generateHelper(
name: string,
cwd: string,
{ path = "", ts = false } = {},
{
authoringFormat = "js",
cwd = "",
path = "",
}: {
authoringFormat?: "js" | "ts";
cwd?: string;
path?: string;
} = {},
) {
return generateDocument(DocumentName.Helper, name, cwd, {
inputs: { ts },
path: path,
return generateDocument(DocumentName.Helper, name, {
cwd,
inputs: { authoringFormat },
path,
});
}

export function generateModifier(
name: string,
cwd: string,
{ path = "", ts = false } = {},
{
authoringFormat = "js",
cwd = "",
path = "",
}: {
authoringFormat?: "js" | "ts";
cwd?: string;
path?: string;
} = {},
) {
return generateDocument(DocumentName.Modifier, name, cwd, {
inputs: { ts },
path: path,
return generateDocument(DocumentName.Modifier, name, {
cwd,
inputs: { authoringFormat },
path,
});
}

export function generateService(
name: string,
cwd: string,
{ path = "", ts = false } = {},
{
authoringFormat = "js",
cwd = "",
path = "",
}: {
authoringFormat?: "js" | "ts";
cwd?: string;
path?: string;
} = {},
) {
return generateDocument(DocumentName.Service, name, cwd, {
inputs: { ts },
path: path,
return generateDocument(DocumentName.Service, name, {
cwd,
inputs: { authoringFormat },
path,
});
}
11 changes: 6 additions & 5 deletions test/generate-component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ afterEach(() => fsExtra.remove(cwd));
it("generates a `.gjs` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", cwd);
await generateComponent("foo", { cwd });

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

Expand All @@ -22,7 +22,7 @@ it("generates a `.gjs` component", async (ctx) => {
it("generates a `.gts` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", cwd, { gts: true });
await generateComponent("foo", { authoringFormat: "gts", cwd });

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

Expand All @@ -32,7 +32,7 @@ it("generates a `.gts` component", async (ctx) => {
it("generates a `.gjs` component at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", cwd, { path: "src/-private" });
await generateComponent("foo", { cwd, path: "src/-private" });

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

Expand All @@ -42,8 +42,9 @@ it("generates a `.gjs` component at a custom path", async (ctx) => {
it("generates a `.gts` component at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateComponent("foo", cwd, {
gts: true,
await generateComponent("foo", {
authoringFormat: "gts",
cwd,
path: "src/-private",
});

Expand Down
11 changes: 6 additions & 5 deletions test/generate-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ afterEach(() => fsExtra.remove(cwd));
it("generates a `.js` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", cwd);
await generateHelper("foo", { cwd });

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

Expand All @@ -22,7 +22,7 @@ it("generates a `.js` helper", async (ctx) => {
it("generates a `.ts` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

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

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

Expand All @@ -32,7 +32,7 @@ it("generates a `.ts` helper", async (ctx) => {
it("generates a `.js` helper at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", cwd, { path: "src/-private" });
await generateHelper("foo", { cwd, path: "src/-private" });

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

Expand All @@ -42,9 +42,10 @@ it("generates a `.js` helper at a custom path", async (ctx) => {
it("generates a `.ts` helper at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateHelper("foo", cwd, {
await generateHelper("foo", {
authoringFormat: "ts",
cwd,
path: "src/-private",
ts: true,
});

const content = await readFile(join(cwd, "src/-private/foo.ts"), "utf-8");
Expand Down
11 changes: 6 additions & 5 deletions test/generate-modifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ afterEach(() => fsExtra.remove(cwd));
it("generates a `.js` modifier", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateModifier("foo", cwd);
await generateModifier("foo", { cwd });

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

Expand All @@ -22,7 +22,7 @@ it("generates a `.js` modifier", async (ctx) => {
it("generates a `.ts` modifier", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

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

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

Expand All @@ -32,7 +32,7 @@ it("generates a `.ts` modifier", async (ctx) => {
it("generates a `.js` modifier at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateModifier("foo", cwd, { path: "src/-private" });
await generateModifier("foo", { cwd, path: "src/-private" });

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

Expand All @@ -42,9 +42,10 @@ it("generates a `.js` modifier at a custom path", async (ctx) => {
it("generates a `.ts` modifier at a custom path", async (ctx) => {
cwd = await copyBlueprint("v2-addon");

await generateModifier("foo", cwd, {
await generateModifier("foo", {
authoringFormat: "ts",
cwd,
path: "src/-private",
ts: true,
});

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

0 comments on commit 1fd952b

Please sign in to comment.