Skip to content

Commit

Permalink
Merge pull request #7 from bertdeblock/minor-improvements
Browse files Browse the repository at this point in the history
Support a `--path` option
  • Loading branch information
bertdeblock authored Mar 1, 2024
2 parents d971ecb + 08692b9 commit 9d8a1e8
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 69 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ or
bunx @bertdeblock/generate-template-registry@latest
```

## Options

### `--path`

Generate a template registry at a custom path.

```shell
npx @bertdeblock/generate-template-registry@latest --path="app/glint/template-registry.ts"
```

## Caveats

- If your app or addon has components, helpers or modifiers with the same name, duplicate template registry entries will be generated, which will need to be fixed manually
4 changes: 2 additions & 2 deletions bin/generate-template-registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

// eslint-disable-next-line n/no-missing-import
import { generateTemplateRegistry } from "../dist/generate-template-registry.js";
import { cli } from "../dist/cli.js";

generateTemplateRegistry();
cli();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
"change-case": "^5.4.3",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0"
"fs-extra": "^11.2.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@release-it-plugins/lerna-changelog": "^6.1.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.11.20",
"@types/recursive-readdir": "^2.2.4",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@vitest/coverage-v8": "^1.2.2",
Expand All @@ -50,6 +52,7 @@
"release-it": "^17.0.3",
"type-fest": "^4.10.3",
"typescript": "^5.3.3",
"uuid": "^9.0.1",
"vitest": "^1.2.2"
},
"packageManager": "[email protected]",
Expand Down
40 changes: 24 additions & 16 deletions pnpm-lock.yaml

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

17 changes: 17 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { cwd } from "node:process";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { generateTemplateRegistry } from "./generate-template-registry.js";

export async function cli() {
const options = await yargs(hideBin(process.argv))
.options({
path: {
describe: "Generate a template registry at a custom path",
type: "string",
},
})
.strict().argv;

await generateTemplateRegistry(cwd(), { path: options.path });
}
79 changes: 38 additions & 41 deletions src/generate-template-registry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chalk from "chalk";
import { pascalCase } from "change-case";
import { execa } from "execa";
import { readJson } from "fs-extra/esm";
import { ensureDir, readJson } from "fs-extra/esm";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { cwd as processCwd } from "node:process";
import { EOL } from "node:os";
import { isAbsolute, join, parse } from "node:path";
import {
getEntries,
isAddon,
Expand All @@ -21,7 +21,10 @@ import {

const TAB = " ";

export async function generateTemplateRegistry(cwd = processCwd()) {
export async function generateTemplateRegistry(
cwd: string,
options: { path?: string } = {},
) {
let packageJson: EmberPackageJson;

try {
Expand All @@ -32,16 +35,17 @@ 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.");
}

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

const isV2AddonPackage = isV2Addon(packageJson);
const entriesDir = isAddon(packageJson)
? isV2Addon(packageJson)
? isV2AddonPackage
? "src"
: "addon"
: "app";
Expand All @@ -60,9 +64,9 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
);
}

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

let templateRegistryContent = "";
const importRoot = isV2AddonPackage ? "." : packageJson.name;
const templateRegistryImportsContent: string[] = [];
const templateRegistryEntriesContent: string[] = [];

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

const imports = typeEntries.map((entry) => {
let importsContent = `// ${type}${EOL}`;
let entriesContent = `${TAB}// ${type}${EOL}`;

for (const entry of typeEntries) {
let entryName = entry.name;

if (isV2Addon(packageJson)) {
if (isV2AddonPackage) {
entryName += entry.extension;
}

return `import type ${entry.identifier} from "${importRoot}/${type}/${entryName}";`;
});

templateRegistryContent += `// ${type}\n${imports.join("\n")}\n\n`;
}

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

const entriesContent: string[] = [];

for (const type in entries) {
const typeEntries = entries[type as keyof EntriesResult];

if (typeEntries.length === 0) {
continue;
}

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

typeEntries.forEach((entry) => {
content += `${TAB}${toRegistryKey(entry.name)}: typeof ${entry.identifier};\n`;
importsContent += `import type ${entry.identifier} from "${importRoot}/${type}/${entryName}";${EOL}`;
entriesContent += `${TAB}${toRegistryKey(entry.name)}: typeof ${entry.identifier};${EOL}`;

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

entriesContent.push(content);
templateRegistryImportsContent.push(importsContent);
templateRegistryEntriesContent.push(entriesContent);
}

templateRegistryContent += `${entriesContent.join("\n")}}\n`;
const templateRegistryPath = options.path
? isAbsolute(options.path)
? options.path
: join(cwd, options.path)
: join(cwd, entriesDir, "template-registry.ts");

const templateRegistryPath = join(cwd, entriesDir, "template-registry.ts");
const templateRegistryContent = `${templateRegistryImportsContent.join(EOL)}
export default interface ${pascalCase(packageJson.name)}Registry {
${templateRegistryEntriesContent.join(EOL)}}
`;

await ensureDir(parse(templateRegistryPath).dir);
await writeFile(templateRegistryPath, templateRegistryContent);

try {
Expand All @@ -121,6 +118,6 @@ export async function generateTemplateRegistry(cwd = processCwd()) {
}

console.log(
chalk.green(`Template registry generated at ${templateRegistryPath}`),
chalk.green(`Template registry generated at ${templateRegistryPath}.`),
);
}
Loading

0 comments on commit 9d8a1e8

Please sign in to comment.