Skip to content

Commit

Permalink
Add test for v2 apps
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Jan 10, 2025
1 parent 7aa7904 commit 7cf2782
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"scripts": {
"build": "tsc --project tsconfig.json",
"gember": "bin/gember.js --path=test/output",
"lint": "concurrently --group --prefix-colors auto \"npm:lint:*(!fix)\"",
"lint:fix": "concurrently --group --prefix-colors auto \"npm:lint:*:fix\"",
"lint:format": "prettier . --cache --check",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
generateModifier,
generateService,
} from "./generators.js";
import { DocumentName } from "./types.js";
import type { DocumentName } from "./types.js";

yargs(hideBin(process.argv))
.command({
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { findUp } from "find-up";
import { pathToFileURL } from "node:url";
import { GemberError } from "./errors.js";
import { DocumentName, type File } from "./types.js";
import type { DocumentName, File } from "./types.js";

export type Config = {
generators?: {
Expand Down
27 changes: 10 additions & 17 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { fileURLToPath } from "node:url";
import { type GenerateInputs, loadScaffdog } from "scaffdog";
import { resolveConfig } from "./config.js";
import { GemberError } from "./errors.js";
import { isAddon, isV2Addon } from "./helpers.js";
import { type DocumentName } from "./types.js";
import { isV1Addon, isV2Addon, pathCase } from "./helpers.js";
import type { DocumentName } from "./types.js";

export async function generate(
documentName: DocumentName,
Expand Down Expand Up @@ -81,14 +81,14 @@ export async function generate(
});
}

const DOCUMENT_DIRECTORY: Record<DocumentName, string> = {
const DOCUMENT_DIR: Record<DocumentName, string> = {
component: "components",
helper: "helpers",
modifier: "modifiers",
service: "services",
};

const SRC_DIRECTORY: Record<string, string> = {
const SRC_DIR: Record<string, string> = {
APP: "app",
V1_ADDON: "addon",
V2_ADDON: "src",
Expand All @@ -108,18 +108,11 @@ export async function resolveGeneratePath(
}

const packageJson = await readJson(join(packagePath, "package.json"));
const srcDirectory = isAddon(packageJson)
? isV2Addon(packageJson)
? SRC_DIRECTORY.V2_ADDON
: SRC_DIRECTORY.V1_ADDON
: SRC_DIRECTORY.APP;
const srcDir = isV2Addon(packageJson)
? SRC_DIR.V2_ADDON
: isV1Addon(packageJson)
? SRC_DIR.V1_ADDON
: SRC_DIR.APP;

return join(packagePath, srcDirectory, DOCUMENT_DIRECTORY[documentName]);
}

function pathCase(entityName: string): string {
return entityName
.split("/")
.map((part) => kebabCase(part))
.join("/");
return join(packagePath, srcDir, DOCUMENT_DIR[documentName]);
}
34 changes: 29 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { type EmberPackageJson } from "./types.js";
import { kebabCase } from "change-case";
import type { EmberPackageJson } from "./types.js";

export function isAddon(packageJson: EmberPackageJson): boolean {
if (Array.isArray(packageJson.keywords)) {
return packageJson.keywords.includes("ember-addon");
export function isV1Addon(packageJson: EmberPackageJson): boolean {
if (isAddon(packageJson)) {
const { version } = packageJson["ember-addon"] ?? {};

return version === 1 || version === undefined;
}

return false;
}

export function isV2Addon(packageJson: EmberPackageJson): boolean {
return packageJson["ember-addon"]?.version === 2;
if (isAddon(packageJson)) {
const { version } = packageJson["ember-addon"] ?? {};

return version === 2;
}

return false;
}

export function pathCase(entityName: string): string {
return entityName
.split("/")
.map((part) => kebabCase(part))
.join("/");
}

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

return false;
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line n/no-missing-import
import { type PackageJson } from "type-fest";
import type { PackageJson } from "type-fest";

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

Expand Down
12 changes: 12 additions & 0 deletions test/generate-document.test.ts → test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ it("supports v1 apps", async (ctx) => {
.toEqual(join("test/packages", name, "app/components"));
});

it("supports v2 apps", async (ctx) => {
const name = "v2-app";
const generatePath = await resolveGeneratePath(
"component",
Package.createPath(name),
);

ctx
.expect(generatePath)
.toEqual(join("test/packages", name, "app/components"));
});

it("supports v1 addons", async (ctx) => {
const name = "v1-addon";
const generatePath = await resolveGeneratePath(
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions test/packages/v2-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "v2-app",
"ember": {},
"ember-addon": {
"type": "app",
"version": 2
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"moduleResolution": "NodeNext",
"outDir": "./dist",
"strict": true,
"target": "ESNext"
"target": "ESNext",
"verbatimModuleSyntax": true
},
"include": ["src"]
}

0 comments on commit 7cf2782

Please sign in to comment.