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

Include extensions for imports in a V2 addon #3

Merged
merged 1 commit into from
Feb 29, 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
62 changes: 33 additions & 29 deletions src/generate-template-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
);
}

const srcDir = isAddon(packageJson)
? isV2Addon(packageJson)
? "src"
: "addon"
: "app";
const packageIsAddon = isAddon(packageJson);
const packageIsV2Addon = isV2Addon(packageJson);

const importRoot = isV2Addon(packageJson) ? "." : packageJson.name;
const importRoot = packageIsV2Addon ? "." : packageJson.name;
const srcDir = packageIsAddon ? (packageIsV2Addon ? "src" : "addon") : "app";

const components = await getEntries(join(cwd, srcDir, "components"));
const helpers = await getEntries(join(cwd, srcDir, "helpers"));
Expand All @@ -35,32 +33,35 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
let templateRegistryContent = "";

if (components.length > 0) {
const componentImports = components.map((name) => {
const identifier = identifiers.generate(name, pascalCase, "Component");
const componentImports = components.map(({ ext, path }) => {
const identifier = identifiers.generate(path, pascalCase, "Component");
const file = packageIsV2Addon ? path + ext : path;

return `import type ${identifier} from "${importRoot}/components/${name}";`;
return `import type ${identifier} from "${importRoot}/components/${file}";`;
});

templateRegistryContent += "// Components:\n";
templateRegistryContent += componentImports.join("\n") + "\n\n";
}

if (helpers.length > 0) {
const helperImports = helpers.map((name) => {
const identifier = identifiers.generate(name, camelCase, "Helper");
const helperImports = helpers.map(({ ext, path }) => {
const identifier = identifiers.generate(path, camelCase, "Helper");
const file = packageIsV2Addon ? path + ext : path;

return `import type ${identifier} from "${importRoot}/helpers/${name}";`;
return `import type ${identifier} from "${importRoot}/helpers/${file}";`;
});

templateRegistryContent += "// Helpers:\n";
templateRegistryContent += helperImports.join("\n") + "\n\n";
}

if (modifiers.length > 0) {
const modifierImports = modifiers.map((name) => {
const identifier = identifiers.generate(name, camelCase, "Modifier");
const modifierImports = modifiers.map(({ ext, path }) => {
const identifier = identifiers.generate(path, camelCase, "Modifier");
const file = packageIsV2Addon ? path + ext : path;

return `import type ${identifier} from "${importRoot}/modifiers/${name}";`;
return `import type ${identifier} from "${importRoot}/modifiers/${file}";`;
});

templateRegistryContent += "// Modifiers:\n";
Expand All @@ -74,9 +75,9 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
if (components.length > 0) {
let componentsContent = " // Components:\n";

components.forEach((name) => {
componentsContent += ` ${angleBracketNotation(name)}: typeof ${identifiers.for(name)};\n`;
componentsContent += ` "${name}": typeof ${identifiers.for(name)};\n`;
components.forEach(({ path }) => {
componentsContent += ` ${angleBracketNotation(path)}: typeof ${identifiers.for(path)};\n`;
componentsContent += ` "${path}": typeof ${identifiers.for(path)};\n`;
});

entriesContent.push(componentsContent);
Expand All @@ -85,8 +86,8 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
if (helpers.length > 0) {
let helpersContent = " // Helpers:\n";

helpers.forEach((name) => {
helpersContent += ` "${name}": typeof ${identifiers.for(name)};\n`;
helpers.forEach(({ path }) => {
helpersContent += ` "${path}": typeof ${identifiers.for(path)};\n`;
});

entriesContent.push(helpersContent);
Expand All @@ -95,8 +96,8 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
if (modifiers.length > 0) {
let modifiersContent = " // Modifiers:\n";

modifiers.forEach((name) => {
modifiersContent += ` "${name}": typeof ${identifiers.for(name)};\n`;
modifiers.forEach(({ path }) => {
modifiersContent += ` "${path}": typeof ${identifiers.for(path)};\n`;
});

entriesContent.push(modifiersContent);
Expand Down Expand Up @@ -127,11 +128,16 @@ function isV2Addon(packageFile: any): boolean {
return packageFile["ember-addon"]?.version === 2;
}

async function getEntries(dir: string): Promise<string[]> {
const paths: string[] = [];
interface Entry {
ext: string;
path: string;
}

async function getEntries(dir: string): Promise<Entry[]> {
const entries: Entry[] = [];

if (await pathExists(dir)) {
const files = await glob("**/*", { cwd: dir });
const files = await glob("**/*.{gjs,gts,ts}", { cwd: dir });

files.sort().forEach((file) => {
const parsed = parse(file);
Expand All @@ -143,13 +149,11 @@ async function getEntries(dir: string): Promise<string[]> {
path = parsed.dir ? `${parsed.dir}/${parsed.name}` : parsed.name;
}

if (paths.includes(path) === false) {
paths.push(path);
}
entries.push({ ext: parsed.ext, path });
});
}

return paths;
return entries;
}

function angleBracketNotation(name: string) {
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/generate-template-registry.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export default interface Registry {

exports[`generates a template registry for a v2 addon 1`] = `
"// Components:
import type BarComponent from "./components/bar";
import type FooBarComponent from "./components/foo-bar";
import type FooComponent from "./components/foo";
import type FooBarComponent2 from "./components/foo/bar";
import type BarComponent from "./components/bar.ts";
import type FooBarComponent from "./components/foo-bar.gts";
import type FooComponent from "./components/foo.ts";
import type FooBarComponent2 from "./components/foo/bar.ts";

export default interface Registry {
// Components:
Expand Down
Empty file.
Empty file.
Empty file.
Loading