Skip to content

Commit

Permalink
fix: Do not log debug info unless asked to (#2549)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- authored Jul 1, 2024
1 parent ca6c731 commit 210fae2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
20 changes: 14 additions & 6 deletions packages/ts/file-router/src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export type PluginOptions = Readonly<{
* @defaultValue `false`
*/
isDevMode?: boolean;
/**
* The flag to indicate whether to output debug information
*
* @defaultValue `false`
*/
debug?: boolean;
}>;

/**
Expand All @@ -50,6 +56,7 @@ export default function vitePluginFileSystemRouter({
generatedDir = 'frontend/generated/',
extensions = ['.tsx', '.jsx'],
isDevMode = false,
debug = false,
}: PluginOptions = {}): Plugin {
let _viewsDir: URL;
let _outDir: URL;
Expand All @@ -66,18 +73,19 @@ export default function vitePluginFileSystemRouter({
_outDir = pathToFileURL(outDir);
_logger = logger;

_logger.info(`The directory of route files: ${String(_viewsDir)}`);
_logger.info(`The directory of generated files: ${String(_generatedDir)}`);
_logger.info(`The output directory: ${String(_outDir)}`);

if (debug) {
_logger.info(`The directory of route files: ${String(_viewsDir)}`);
_logger.info(`The directory of generated files: ${String(_generatedDir)}`);
_logger.info(`The output directory: ${String(_outDir)}`);
}
runtimeUrls = {
json: new URL('file-routes.json', isDevMode ? _generatedDir : _outDir),
code: new URL('file-routes.ts', _generatedDir),
};
},
async buildStart() {
try {
await generateRuntimeFiles(_viewsDir, runtimeUrls, extensions, _logger);
await generateRuntimeFiles(_viewsDir, runtimeUrls, extensions, _logger, debug);
} catch (e: unknown) {
_logger.error(String(e));
}
Expand All @@ -93,7 +101,7 @@ export default function vitePluginFileSystemRouter({
return;
}

generateRuntimeFiles(_viewsDir, runtimeUrls, extensions, _logger).catch((e: unknown) =>
generateRuntimeFiles(_viewsDir, runtimeUrls, extensions, _logger, debug).catch((e: unknown) =>
_logger.error(String(e)),
);
};
Expand Down
22 changes: 15 additions & 7 deletions packages/ts/file-router/src/vite-plugin/generateRuntimeFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,32 @@ async function generateRuntimeFile(url: URL, data: string): Promise<void> {
* @param urls - The URLs of the files to generate.
* @param extensions - The list of extensions that will be collected as routes.
* @param logger - The Vite logger instance.
* @param debug - true to debug
*/
export async function generateRuntimeFiles(
viewsDir: URL,
urls: RuntimeFileUrls,
extensions: readonly string[],
logger: Logger,
debug: boolean,
): Promise<void> {
const routeMeta = existsSync(viewsDir) ? await collectRoutesFromFS(viewsDir, { extensions, logger }) : [];
logger.info('Collected file-based routes');
if (debug) {
logger.info('Collected file-based routes');
}
const runtimeRoutesCode = createRoutesFromMeta(routeMeta, urls);
const viewConfigJson = await createViewConfigJson(routeMeta);

await Promise.all([
generateRuntimeFile(urls.json, viewConfigJson).then(() =>
logger.info(`Frontend route list is generated: ${String(urls.json)}`),
),
generateRuntimeFile(urls.code, runtimeRoutesCode).then(() =>
logger.info(`File Route module is generated: ${String(urls.code)}`),
),
generateRuntimeFile(urls.json, viewConfigJson).then(() => {
if (debug) {
logger.info(`Frontend route list is generated: ${String(urls.json)}`);
}
}),
generateRuntimeFile(urls.code, runtimeRoutesCode).then(() => {
if (debug) {
logger.info(`File Route module is generated: ${String(urls.code)}`);
}
}),
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('@vaadin/hilla-file-router', () => {
});

it('should generate the runtime files', async () => {
await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger);
await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger, true);
expect(existsSync(runtimeUrls.json)).to.be.true;
expect(existsSync(runtimeUrls.code)).to.be.true;
const listener = () => {
Expand All @@ -45,7 +45,7 @@ describe('@vaadin/hilla-file-router', () => {
const json = watch(runtimeUrls.json, listener);
const code = watch(runtimeUrls.code, listener);

await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger);
await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger, true);
await new Promise((resolve) => {
// Wait some time to ensure that the file is not changed
setTimeout(resolve, 100);
Expand All @@ -56,7 +56,7 @@ describe('@vaadin/hilla-file-router', () => {

it('should not throw if views does not exist', async () => {
await rm(viewsDir, { force: true, recursive: true });
await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger);
await generateRuntimeFiles(viewsDir, runtimeUrls, ['.tsx', '.jsx'], logger, true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('@vaadin/hilla-file-router', () => {
},
watcher,
};
const plugin = vitePluginFileSystemRouter({ isDevMode: true });
const plugin = vitePluginFileSystemRouter({ isDevMode: true, debug: true });
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error: the configResolved method could be either a function or an object.
plugin.configResolved({
Expand Down

0 comments on commit 210fae2

Please sign in to comment.