Skip to content

Commit

Permalink
Support v1 apps and addons
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Mar 20, 2024
1 parent 087d117 commit e232b67
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"prettier": "^3.2.5",
"recursive-copy": "^2.0.14",
"release-plan": "^0.8.0",
"type-fest": "^4.13.1",
"typescript": "^5.3.3",
"uuid": "^9.0.1",
"vitest": "^1.2.2"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

20 changes: 14 additions & 6 deletions src/generate-document.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import chalk from "chalk";
import { camelCase, kebabCase, pascalCase } from "change-case";
import { ensureDir } from "fs-extra";
import { ensureDir, readJson } from "fs-extra/esm";
import { writeFile } from "node:fs/promises";
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 { isAddon, isV2Addon } from "./helpers.js";
import { type DocumentName } from "./types.js";

export async function generateDocument(
Expand All @@ -30,7 +31,7 @@ export async function generateDocument(
throw new Error(`[BUG] Document \`${documentName}\` not found.`);
}

const documentPath = getDocumentPath(documentName, cwd, path);
const documentPath = await getDocumentPath(documentName, cwd, path);
const files = await scaffdog.generate(document, documentPath, {
inputs: {
...inputs,
Expand Down Expand Up @@ -67,11 +68,11 @@ const DOCUMENT_DIRECTORY: Record<DocumentName, string> = {
service: "services",
};

function getDocumentPath(
export async function getDocumentPath(
documentName: DocumentName,
cwd: string,
path: string,
): string {
path?: string,
): Promise<string> {
if (path) {
if (isAbsolute(path)) {
return path;
Expand All @@ -80,5 +81,12 @@ function getDocumentPath(
}
}

return join(cwd, "src", DOCUMENT_DIRECTORY[documentName]);
const packageJson = await readJson(join(cwd, "package.json"));
const srcDirectory = isAddon(packageJson)
? isV2Addon(packageJson)
? "src" // v2 addon
: "addon" // v1 addon
: "app"; // v1 app

return join(cwd, srcDirectory, DOCUMENT_DIRECTORY[documentName]);
}
13 changes: 13 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type EmberPackageJson } from "./types.js";

export function isAddon(packageJson: EmberPackageJson): boolean {
if (Array.isArray(packageJson.keywords)) {
return packageJson.keywords.includes("ember-addon");
}

return false;
}

export function isV2Addon(packageJson: EmberPackageJson): boolean {
return packageJson["ember-addon"]?.version === 2;
}
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
// eslint-disable-next-line n/no-missing-import
import { type PackageJson } from "type-fest";

export type DocumentName = "component" | "helper" | "modifier" | "service";

export type EmberPackageJson = PackageJson & {
ember?: {
edition?: string;
};
"ember-addon"?: {
version?: number;
};
};
Empty file.
8 changes: 8 additions & 0 deletions test/blueprints/v1-addon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "v1-addon",
"keywords": [
"ember-addon"
],
"ember": {},
"ember-addon": {}
}
Empty file.
5 changes: 5 additions & 0 deletions test/blueprints/v1-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "v1-app",
"ember": {},
"ember-addon": {}
}
10 changes: 10 additions & 0 deletions test/blueprints/v2-addon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "v2-addon",
"keywords": [
"ember-addon"
],
"ember": {},
"ember-addon": {
"version": 2
}
}
4 changes: 2 additions & 2 deletions test/generate-component.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsExtra from "fs-extra";
import { remove } from "fs-extra";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, it } from "vitest";
Expand All @@ -7,7 +7,7 @@ import { copyBlueprint } from "./helpers.ts";

let cwd: string;

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

it("generates a template-only `.gjs` component", async (ctx) => {
cwd = await copyBlueprint("v2-addon");
Expand Down
24 changes: 24 additions & 0 deletions test/generate-document.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { it } from "vitest";
import { getDocumentPath } from "../src/generate-document.ts";
import { blueprintPath } from "./helpers.ts";

it("supports v1 apps", async (ctx) => {
const cwd = blueprintPath("v1-app");
const documentPath = await getDocumentPath("component", cwd);

ctx.expect(documentPath).toEqual("test/blueprints/v1-app/app/components");
});

it("supports v1 addons", async (ctx) => {
const cwd = blueprintPath("v1-addon");
const documentPath = await getDocumentPath("component", cwd);

ctx.expect(documentPath).toEqual("test/blueprints/v1-addon/addon/components");
});

it("supports v2 addons", async (ctx) => {
const cwd = blueprintPath("v2-addon");
const documentPath = await getDocumentPath("component", cwd);

ctx.expect(documentPath).toEqual("test/blueprints/v2-addon/src/components");
});
4 changes: 2 additions & 2 deletions test/generate-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsExtra from "fs-extra";
import { remove } from "fs-extra";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, it } from "vitest";
Expand All @@ -7,7 +7,7 @@ import { copyBlueprint } from "./helpers.ts";

let cwd: string;

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

it("generates a function-based `.js` helper", async (ctx) => {
cwd = await copyBlueprint("v2-addon");
Expand Down
4 changes: 2 additions & 2 deletions test/generate-modifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsExtra from "fs-extra";
import { remove } from "fs-extra";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, it } from "vitest";
Expand All @@ -7,7 +7,7 @@ import { copyBlueprint } from "./helpers.ts";

let cwd: string;

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

it("generates a function-based `.js` modifier", async (ctx) => {
cwd = await copyBlueprint("v2-addon");
Expand Down
4 changes: 2 additions & 2 deletions test/generate-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fsExtra from "fs-extra";
import { remove } from "fs-extra";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, it } from "vitest";
Expand All @@ -7,7 +7,7 @@ import { copyBlueprint } from "./helpers.ts";

let cwd: string;

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

it("generates a `.js` service", async (ctx) => {
cwd = await copyBlueprint("v2-addon");
Expand Down
10 changes: 8 additions & 2 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { join } from "node:path";
import recursiveCopy from "recursive-copy";
import { v4 as uuidv4 } from "uuid";

export async function copyBlueprint(name: "v2-addon") {
type Blueprint = "v1-app" | "v1-addon" | "v2-addon";

export function blueprintPath(name: Blueprint) {
return join("test/blueprints", name);
}

export async function copyBlueprint(name: Blueprint) {
const cwd = join("test/output", uuidv4());

await recursiveCopy(join("test/blueprints", name), cwd);
await recursiveCopy(blueprintPath(name), cwd);

return cwd;
}

0 comments on commit e232b67

Please sign in to comment.