Skip to content

Commit

Permalink
Warn when an empty template registry will be generated
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Feb 29, 2024
1 parent fbd0f75 commit e76970c
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/dist/
/test/output/
7 changes: 6 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

module.exports = {
env: { node: true },
extends: ["eslint:recommended", "plugin:n/recommended"],
extends: [
"eslint:recommended",
"plugin:n/recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: { ecmaVersion: "latest" },
plugins: ["@typescript-eslint"],
root: true,
};
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/coverage/
/dist/
/test/output/
/CHANGELOG.md
/pnpm-lock.yaml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/bertdeblock/generate-template-registry/workflows/CI/badge.svg)](https://github.com/bertdeblock/generate-template-registry/actions?query=workflow%3ACI)

Generate a template registry for [Glint](https://github.com/typed-ember/glint).
Generate a [template registry](https://typed-ember.gitbook.io/glint/environments/ember/template-registry) for [Glint](https://github.com/typed-ember/glint).

## Usage

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.11.20",
"@types/recursive-readdir": "^2.2.4",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@vitest/coverage-v8": "^1.2.2",
"concurrently": "^8.2.2",
Expand All @@ -47,6 +48,7 @@
"prettier": "^3.2.5",
"recursive-copy": "^2.0.14",
"release-it": "^17.0.3",
"type-fest": "^4.10.3",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
},
Expand Down
87 changes: 87 additions & 0 deletions pnpm-lock.yaml

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

39 changes: 31 additions & 8 deletions src/generate-template-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import {
toAngleBracketNotation,
toRegistryKey,
} from "./helpers.js";
import type { EntriesResult } from "./types.js";
import {
EntryType,
type EmberPackageJson,
type EntriesResult,
} from "./types.js";

const TAB = " ";

export async function generateTemplateRegistry(cwd = processCwd()) {
let packageJson: any;
let packageJson: EmberPackageJson;

try {
packageJson = await readJson(join(cwd, "package.json"));
Expand All @@ -26,11 +32,14 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
);
}

if (typeof packageJson.name === "undefined") {
throw new Error(`The found "package.json" file is missing a "name" entry.`);
}

if (isEmberPackage(packageJson) === false) {
throw new Error("The current package is not an Ember app or addon.");
}

const importRoot = isV2Addon(packageJson) ? "." : packageJson.name;
const entriesDir = isAddon(packageJson)
? isV2Addon(packageJson)
? "src"
Expand All @@ -39,6 +48,20 @@ export async function generateTemplateRegistry(cwd = processCwd()) {

const entries = await getEntries(join(cwd, entriesDir));

if (
entries.components.length === 0 &&
entries.helpers.length === 0 &&
entries.modifiers.length === 0
) {
console.warn(
chalk.yellow(
"No component, helper or modifier gts/ts files found. An empty template registry will be generated.",
),
);
}

const importRoot = isV2Addon(packageJson) ? "." : packageJson.name;

let templateRegistryContent = "";

for (const type in entries) {
Expand All @@ -63,7 +86,7 @@ export async function generateTemplateRegistry(cwd = processCwd()) {

templateRegistryContent += `export default interface ${pascalCase(packageJson.name)}Registry {\n`;

let entriesContent: string[] = [];
const entriesContent: string[] = [];

for (const type in entries) {
const typeEntries = entries[type as keyof EntriesResult];
Expand All @@ -72,13 +95,13 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
continue;
}

let content = ` // ${type}\n`;
let content = `${TAB}// ${type}\n`;

typeEntries.forEach((entry) => {
content += ` ${toRegistryKey(entry.name)}: typeof ${entry.identifier};\n`;
content += `${TAB}${toRegistryKey(entry.name)}: typeof ${entry.identifier};\n`;

if (type === "components") {
content += ` ${toRegistryKey(toAngleBracketNotation(entry.name))}: typeof ${entry.identifier};\n`;
if (type === EntryType.Components) {
content += `${TAB}${toRegistryKey(toAngleBracketNotation(entry.name))}: typeof ${entry.identifier};\n`;
}
});

Expand Down
26 changes: 17 additions & 9 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { pascalCase } from "change-case";
import glob from "fast-glob";
import { pathExists } from "fs-extra/esm";
import { join, parse } from "node:path";
import type { EntriesResult } from "./types.js";
import {
EntryType,
type EmberPackageJson,
type EntriesResult,
} from "./types.js";

export async function getEntries(directory: string): Promise<EntriesResult> {
const seenIdentifiers: Record<string, number> = {};
Expand All @@ -12,11 +16,11 @@ export async function getEntries(directory: string): Promise<EntriesResult> {
modifiers: [],
};

for (const type in entriesResult) {
const path = join(directory, type);
for (const entryType of Object.values(EntryType)) {
const path = join(directory, entryType);

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

for (let index = 0; index < filesSorted.length; index++) {
Expand All @@ -40,7 +44,7 @@ export async function getEntries(directory: string): Promise<EntriesResult> {
seenIdentifiers[identifier] = 1;
}

entriesResult[type as keyof EntriesResult].push({
entriesResult[entryType as keyof EntriesResult].push({
extension: fileParsed.ext,
identifier,
name,
Expand All @@ -52,15 +56,19 @@ export async function getEntries(directory: string): Promise<EntriesResult> {
return entriesResult;
}

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

return false;
}

export function isEmberPackage(packageJson: any): boolean {
export function isEmberPackage(packageJson: EmberPackageJson): boolean {
return typeof packageJson.ember === "object";
}

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

Expand Down
22 changes: 18 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
export interface EntriesResult {
components: Entry[];
helpers: Entry[];
modifiers: Entry[];
// eslint-disable-next-line n/no-missing-import
import type { PackageJson } from "type-fest";

export type EntriesResult = Record<EntryType, Entry[]>;

export enum EntryType {
Components = "components",
Helpers = "helpers",
Modifiers = "modifiers",
}

export interface Entry {
extension: string;
identifier: string;
name: string;
}

export type EmberPackageJson = PackageJson & {
ember?: {
edition?: string;
};
"ember-addon"?: {
version?: number;
};
};

0 comments on commit e76970c

Please sign in to comment.