Skip to content
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
30 changes: 23 additions & 7 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export default class Generate extends BaseCommand {

console.log(`[apso] Generating ${language} code for ${entities.length} entities...`);

if (entities.length === 0) {
console.log(
"[apso] No entities found in .apsorc — nothing to generate. Check that the file exists and defines an `entities` array."
);
}

let generatedFileCount = 0;

const generatorConfig: GeneratorConfig = {
language,
rootFolder,
Expand All @@ -113,6 +121,7 @@ export default class Generate extends BaseCommand {
// eslint-disable-next-line no-await-in-loop
await createFile(fullPath, file.content);
}
generatedFileCount += enumFiles.length;

// Generate shared query utilities
const queryUtilFiles = await generator.generateQueryUtils(entities, lowerCaseApiType);
Expand All @@ -121,6 +130,7 @@ export default class Generate extends BaseCommand {
// eslint-disable-next-line no-await-in-loop
await createFile(fullPath, file.content);
}
generatedFileCount += queryUtilFiles.length;

// Generate per-entity files
for (const entity of entities) {
Expand Down Expand Up @@ -170,6 +180,7 @@ export default class Generate extends BaseCommand {
const fullPath = path.join(autogenPath, file.path);
// eslint-disable-next-line no-await-in-loop
await createFile(fullPath, file.content);
generatedFileCount += 1;
}
}

Expand All @@ -186,6 +197,7 @@ export default class Generate extends BaseCommand {
// eslint-disable-next-line no-await-in-loop
await createFile(fullPath, file.content);
}
generatedFileCount += guardFiles.length;

// Generate index module
const indexFiles = await generator.generateIndexModule(entities, lowerCaseApiType);
Expand All @@ -194,19 +206,23 @@ export default class Generate extends BaseCommand {
// eslint-disable-next-line no-await-in-loop
await createFile(fullPath, file.content);
}
generatedFileCount += indexFiles.length;

// Format generated files (TypeScript only)
if (language === "typescript" && !skipFormat) {
// Format generated files (TypeScript only).
// Run prettier directly against the generated output directory so we never
// reformat hand-written code elsewhere in the project tree (the project's
// `npm run format` script targets a broad glob like `{src,test}/**/*.ts`).
if (language === "typescript" && !skipFormat && generatedFileCount > 0) {
const formatStart = performance.now();
console.log("[apso] Formatting files...");
await this.runNpmCommand(
["run", "format", "src/autogen/**/*.ts", "src/guards/**/*.ts"],
true
);
console.log("[apso] Formatting generated files...");
const formatGlob = path.join(autogenPath, "**", "*.ts");
await this.runCommand("npx", ["prettier", "--write", formatGlob], true);
const formatTime = performance.now() - formatStart;
console.log(`[apso] Finished formatting in ${formatTime.toFixed(2)} ms`);
} else if (skipFormat) {
console.log("[apso] Skipping formatting (--skip-format flag set)");
} else if (generatedFileCount === 0) {
console.log("[apso] Skipping formatting (no files were generated)");
}

const totalBuildTime = performance.now() - totalBuildStart;
Expand Down
16 changes: 12 additions & 4 deletions src/lib/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import { spawn } from "child_process";
import os from "os";

export default abstract class BaseCommand extends Command {
async runNpmCommand(args: string[], silent = false): Promise<void> {
async runCommand(
command: string,
args: string[],
silent = false
): Promise<void> {
return new Promise((resolve: any, reject) => {
const isWindows = os.platform() === "win32";
const command = isWindows ? "npm.cmd" : "npm";
const resolvedCommand = isWindows ? `${command}.cmd` : command;
const stdio = silent ? "ignore" : "inherit";

const cmdStr = `${command} ${args.join(" ")}`;
const cmdStr = `${resolvedCommand} ${args.join(" ")}`;
this.log(`Running: ${cmdStr}`);

const child = spawn(command, args, {
const child = spawn(resolvedCommand, args, {
stdio,
shell: isWindows,
});
Expand All @@ -30,4 +34,8 @@ export default abstract class BaseCommand extends Command {
});
});
}

async runNpmCommand(args: string[], silent = false): Promise<void> {
return this.runCommand("npm", args, silent);
}
}
Loading