Skip to content

Commit

Permalink
feat: improve cli output with consola
Browse files Browse the repository at this point in the history
  • Loading branch information
KoichiKiyokawa committed Mar 12, 2023
1 parent db81a6d commit f41ee70
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-workspace-root-check=true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"chokidar": "^3.5.3",
"consola": "2.15.3",
"defu": "6.1.2",
"fast-glob": "3.2.12",
"jiti": "^1.17.1",
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

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

11 changes: 6 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env node

import sade from "sade";
import consola from "consola";
import { version } from "../package.json";
import kleur from "kleur";
import { autoDetectConfig } from "./config/detect";
Expand Down Expand Up @@ -40,7 +41,7 @@ prog
};

if (opts.watch) {
console.log(
consola.info(
`Watching ${kleur.bold(
kleur.green(
(resolvedConfig.routeDir.endsWith("/")
Expand All @@ -55,18 +56,18 @@ prog
cwd: resolvedConfig.routeDir,
});
watcher.on("all", async (event, path) => {
console.log(event, path);
consola.info(event, path);
await run();
console.log(kleur.green("Regenerated path helper"));
consola.success(kleur.green("Regenerated path helper"));
});
} else {
await run();
console.log(
consola.success(
`Path helper has been generated to ${kleur.bold(kleur.green(resolvedConfig.output))}`,
);
}
} catch (e) {
console.log(kleur.red((e as Error).message));
consola.error(e);
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/config/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import kleur from "kleur";
import { removePathExtension, removeSuffix } from "../utils";
import { defaultFilePathToRoutePath } from "./default";
import consola from "consola";

export function autoDetectConfig(): Partial<Omit<Config, "output">> {
if (fs.existsSync("next.config.js")) {
Expand Down Expand Up @@ -63,5 +64,5 @@ export function autoDetectConfig(): Partial<Omit<Config, "output">> {
}

function showDetectedResult(framework: string) {
console.log(`Detected framework: ${kleur.green(framework)}`);
consola.info(`Detected framework: ${kleur.green(framework)}`);
}
38 changes: 19 additions & 19 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,36 @@ export async function generate({
// This file is auto generated. DO NOT EDIT
type PathToParams = {
${(
await Promise.all(
pathList.filter((p) => !ignorePathList.includes(p)).map((p) => createTypeDefinitionRow(p)),
)
).join(",\n ")}
${(
await Promise.all(
pathList.filter((p) => !ignorePathList.includes(p)).map((p) => createTypeDefinitionRow(p)),
)
).join(",\n ")}
}
/**
* @example
* buildPath('/posts/[id]', { id: 1 }) // => '/posts/1'
*/
* @example
* buildPath('/posts/[id]', { id: 1 }) // => '/posts/1'
*/
export function buildPath<Path extends keyof PathToParams>(
path: Path,
args: PathToParams[Path],
): string {
return (
path.replace(${new RegExp(dynamicSegmentRegex, "g")}, (_, key) => ((args as any).params)[key]) +
(args.query
? '?' + new URLSearchParams(args.query as any).toString()
: '') +
(args.hash ? '#' + args.hash : '')
)
return (
path.replace(${new RegExp(dynamicSegmentRegex, "g")}, (_, key) => ((args as any).params)[key]) +
(args.query
? '?' + new URLSearchParams(args.query as any).toString()
: '') +
(args.hash ? '#' + args.hash : '')
)
}
/**
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
*/
* @example
* echoPath('/posts/[id]') // => '/posts/[id]'
*/
export function echoPath<Path extends keyof PathToParams>(path: Path): string {
return path
return path
}
`;
}

0 comments on commit f41ee70

Please sign in to comment.