Skip to content

Commit

Permalink
Merge pull request #52 from bertdeblock/remove-use-of-the-word-document
Browse files Browse the repository at this point in the history
Remove use of the words document, documents, ...
  • Loading branch information
bertdeblock authored Feb 14, 2025
2 parents 85a63d1 + 3b60d0c commit d8aec37
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/coverage/
/dist/
/documents/*.md
/templates/
/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 @@ -161,9 +161,9 @@ export type Config = {
hooks?: {
// A hook that will be executed post running a generator:
postGenerate?: (info: {
documentName: DocumentName;
entityName: string;
files: File[];
generatorName: GeneratorName;
}) => Promise<void> | void;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"files": [
"bin/",
"dist/",
"documents/",
"templates/",
"CHANGELOG.md"
],
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions 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 type { DocumentName } from "./types.js";
import type { GeneratorName } from "./types.js";

yargs(hideBin(process.argv))
.command({
Expand Down Expand Up @@ -180,11 +180,11 @@ yargs(hideBin(process.argv))
type Options = Record<string, unknown>;

async function applyGemberConfig(
documentName: DocumentName,
generatorName: GeneratorName,
options: Options,
): Promise<Options> {
const config = await resolveConfig(cwd());
const generatorConfig: Options = config.generators?.[documentName] ?? {};
const generatorConfig: Options = config.generators?.[generatorName] ?? {};
const result: Options = { typescript: config.typescript };

for (const key in options) {
Expand Down
8 changes: 6 additions & 2 deletions 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 type { DocumentName, File } from "./types.js";
import type { GeneratorName, File } from "./types.js";

export type Config = {
generators?: {
Expand All @@ -28,9 +28,13 @@ export type Config = {

hooks?: {
postGenerate?: (info: {
documentName: DocumentName;
/**
* @deprecated Please use `generatorName` instead.
*/
documentName: GeneratorName;
entityName: string;
files: File[];
generatorName: GeneratorName;
}) => Promise<void> | void;
};

Expand Down
36 changes: 22 additions & 14 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { type GenerateInputs, loadScaffdog } from "scaffdog";
import { resolveConfig } from "./config.js";
import { GemberError } from "./errors.js";
import { isV1Addon, isV2Addon, pathCase } from "./helpers.js";
import type { DocumentName } from "./types.js";
import type { GeneratorName } from "./types.js";

export async function generate(
documentName: DocumentName,
generatorName: GeneratorName,
entityName: string,
packagePath: string,
{
Expand All @@ -26,23 +26,23 @@ export async function generate(
},
): Promise<void> {
const scaffdog = await loadScaffdog(
join(dirname(fileURLToPath(import.meta.url)), "../documents"),
join(dirname(fileURLToPath(import.meta.url)), "../templates"),
);

const documents = await scaffdog.list();
const document = documents.find((document) => document.name === documentName);
const templates = await scaffdog.list();
const template = templates.find((t) => t.name === generatorName);

if (document === undefined) {
throw new GemberError(`[BUG] Document \`${documentName}\` not found.`);
if (template === undefined) {
throw new GemberError(`[BUG] Template \`${generatorName}\` not found.`);
}

const generatePath = await resolveGeneratePath(
documentName,
generatorName,
packagePath,
path,
);

const files = await scaffdog.generate(document, generatePath, {
const files = await scaffdog.generate(template, generatePath, {
inputs: {
...inputs,
name: {
Expand All @@ -64,24 +64,32 @@ export async function generate(
await writeFile(file.path, file.content);

consola.success(
`🫚 Generated ${documentName} \`${entityName}\` at \`${relative(cwd(), file.path)}\`.`,
`🫚 Generated ${generatorName} \`${entityName}\` at \`${relative(cwd(), file.path)}\`.`,
);
}

const config = await resolveConfig(packagePath);

await config.hooks?.postGenerate?.({
documentName,
get documentName() {
consola.warn(
"🫚 `documentName` is deprecated. Please use `generatorName` instead.",
);

return generatorName;
},

entityName,
files: filesToGenerate.map((file) => ({
content: file.content,
name: file.name,
path: file.path,
})),
generatorName,
});
}

const DOCUMENT_DIR: Record<DocumentName, string> = {
const TARGET_DIR: Record<GeneratorName, string> = {
component: "components",
helper: "helpers",
modifier: "modifiers",
Expand All @@ -95,7 +103,7 @@ const SRC_DIR: Record<string, string> = {
};

export async function resolveGeneratePath(
documentName: DocumentName,
generatorName: GeneratorName,
packagePath: string,
path?: string,
): Promise<string> {
Expand All @@ -114,5 +122,5 @@ export async function resolveGeneratePath(
? SRC_DIR.V1_ADDON
: SRC_DIR.APP;

return join(packagePath, srcDir, DOCUMENT_DIR[documentName]);
return join(packagePath, srcDir, TARGET_DIR[generatorName]);
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line n/no-missing-import
import type { PackageJson } from "type-fest";

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

export type EmberPackageJson = PackageJson & {
ember?: {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion test/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports[`runs the \`postGenerate\` hook 1`] = `
"name": "foo.gjs",
"path": "post-generate-info/src/components/foo.gjs"
}
]
],
"generatorName": "component"
}"
`;
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export default defineConfig({
},
},
test: {
forceRerunTriggers: ["**/documents/**"],
forceRerunTriggers: ["**/templates/**"],
},
});

0 comments on commit d8aec37

Please sign in to comment.