prev-cli 0.14.1 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +86 -18
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { parseArgs } from "util";
5
- import path7 from "path";
6
- import { existsSync as existsSync5, mkdirSync as mkdirSync2, writeFileSync as writeFileSync3, rmSync as rmSync2, readFileSync as readFileSync4 } from "fs";
5
+ import path8 from "path";
6
+ import { existsSync as existsSync6, mkdirSync as mkdirSync2, writeFileSync as writeFileSync3, rmSync as rmSync3, readFileSync as readFileSync4 } from "fs";
7
7
  import { fileURLToPath as fileURLToPath3 } from "url";
8
8
 
9
9
  // src/vite/start.ts
@@ -896,6 +896,9 @@ async function findAvailablePort(minPort, maxPort) {
896
896
  }
897
897
 
898
898
  // src/vite/start.ts
899
+ import { exec as exec2 } from "child_process";
900
+ import { existsSync as existsSync5, rmSync as rmSync2 } from "fs";
901
+ import path7 from "path";
899
902
  function printWelcome(type) {
900
903
  console.log();
901
904
  console.log(" ✨ prev");
@@ -906,12 +909,69 @@ function printWelcome(type) {
906
909
  console.log(" Previewing your production build:");
907
910
  }
908
911
  }
912
+ function printShortcuts() {
913
+ console.log();
914
+ console.log(" Shortcuts:");
915
+ console.log(" o → open in browser");
916
+ console.log(" c → clear cache");
917
+ console.log(" h → show this help");
918
+ console.log(" q → quit");
919
+ console.log();
920
+ }
909
921
  function printReady() {
910
922
  console.log();
911
923
  console.log(" Edit your .md/.mdx files and see changes instantly.");
912
- console.log(" Press Ctrl+C to stop.");
924
+ console.log(" Press h for shortcuts.");
913
925
  console.log();
914
926
  }
927
+ function openBrowser(url) {
928
+ const platform = process.platform;
929
+ const cmd = platform === "darwin" ? "open" : platform === "win32" ? "start" : "xdg-open";
930
+ exec2(`${cmd} ${url}`);
931
+ console.log(` ↗ Opened ${url}`);
932
+ }
933
+ function clearCache(rootDir) {
934
+ const viteCacheDir = path7.join(rootDir, ".vite");
935
+ const nodeModulesVite = path7.join(rootDir, "node_modules", ".vite");
936
+ let cleared = 0;
937
+ if (existsSync5(viteCacheDir)) {
938
+ rmSync2(viteCacheDir, { recursive: true });
939
+ cleared++;
940
+ }
941
+ if (existsSync5(nodeModulesVite)) {
942
+ rmSync2(nodeModulesVite, { recursive: true });
943
+ cleared++;
944
+ }
945
+ if (cleared === 0) {
946
+ console.log(" No cache to clear");
947
+ } else {
948
+ console.log(` ✓ Cleared Vite cache`);
949
+ }
950
+ }
951
+ function setupKeyboardShortcuts(rootDir, url, quit) {
952
+ if (!process.stdin.isTTY)
953
+ return;
954
+ process.stdin.setRawMode(true);
955
+ process.stdin.resume();
956
+ process.stdin.setEncoding("utf8");
957
+ process.stdin.on("data", (key) => {
958
+ switch (key.toLowerCase()) {
959
+ case "o":
960
+ openBrowser(url);
961
+ break;
962
+ case "c":
963
+ clearCache(rootDir);
964
+ break;
965
+ case "h":
966
+ printShortcuts();
967
+ break;
968
+ case "q":
969
+ case "\x03":
970
+ quit();
971
+ break;
972
+ }
973
+ });
974
+ }
915
975
  async function startDev(rootDir, options = {}) {
916
976
  const port = options.port ?? await getRandomPort();
917
977
  const config = await createViteConfig({
@@ -922,9 +982,17 @@ async function startDev(rootDir, options = {}) {
922
982
  });
923
983
  const server = await createServer2(config);
924
984
  await server.listen();
985
+ const actualPort = server.config.server.port || port;
986
+ const url = `http://localhost:${actualPort}/`;
925
987
  printWelcome("dev");
926
988
  server.printUrls();
927
989
  printReady();
990
+ setupKeyboardShortcuts(rootDir, url, async () => {
991
+ console.log(`
992
+ Shutting down...`);
993
+ await server.close();
994
+ process.exit(0);
995
+ });
928
996
  return server;
929
997
  }
930
998
  async function buildSite(rootDir, options = {}) {
@@ -963,15 +1031,15 @@ async function previewSite(rootDir, options = {}) {
963
1031
  // src/cli.ts
964
1032
  function getVersion() {
965
1033
  try {
966
- let dir = path7.dirname(fileURLToPath3(import.meta.url));
1034
+ let dir = path8.dirname(fileURLToPath3(import.meta.url));
967
1035
  for (let i = 0;i < 5; i++) {
968
- const pkgPath = path7.join(dir, "package.json");
969
- if (existsSync5(pkgPath)) {
1036
+ const pkgPath = path8.join(dir, "package.json");
1037
+ if (existsSync6(pkgPath)) {
970
1038
  const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
971
1039
  if (pkg.name === "prev-cli")
972
1040
  return pkg.version;
973
1041
  }
974
- dir = path7.dirname(dir);
1042
+ dir = path8.dirname(dir);
975
1043
  }
976
1044
  } catch {}
977
1045
  return "unknown";
@@ -989,7 +1057,7 @@ var { values, positionals } = parseArgs({
989
1057
  allowPositionals: true
990
1058
  });
991
1059
  var command = positionals[0] || "dev";
992
- var rootDir = path7.resolve(values.cwd || positionals[1] || ".");
1060
+ var rootDir = path8.resolve(values.cwd || positionals[1] || ".");
993
1061
  function printHelp() {
994
1062
  console.log(`
995
1063
  prev - Zero-config documentation site generator
@@ -1053,16 +1121,16 @@ Examples:
1053
1121
  `);
1054
1122
  }
1055
1123
  function clearViteCache(rootDir2) {
1056
- const viteCacheDir = path7.join(rootDir2, ".vite");
1057
- const nodeModulesVite = path7.join(rootDir2, "node_modules", ".vite");
1124
+ const viteCacheDir = path8.join(rootDir2, ".vite");
1125
+ const nodeModulesVite = path8.join(rootDir2, "node_modules", ".vite");
1058
1126
  let cleared = 0;
1059
- if (existsSync5(viteCacheDir)) {
1060
- rmSync2(viteCacheDir, { recursive: true });
1127
+ if (existsSync6(viteCacheDir)) {
1128
+ rmSync3(viteCacheDir, { recursive: true });
1061
1129
  cleared++;
1062
1130
  console.log(` ✓ Removed .vite/`);
1063
1131
  }
1064
- if (existsSync5(nodeModulesVite)) {
1065
- rmSync2(nodeModulesVite, { recursive: true });
1132
+ if (existsSync6(nodeModulesVite)) {
1133
+ rmSync3(nodeModulesVite, { recursive: true });
1066
1134
  cleared++;
1067
1135
  console.log(` ✓ Removed node_modules/.vite/`);
1068
1136
  }
@@ -1074,8 +1142,8 @@ function clearViteCache(rootDir2) {
1074
1142
  }
1075
1143
  }
1076
1144
  function createPreview(rootDir2, name) {
1077
- const previewDir = path7.join(rootDir2, "previews", name);
1078
- if (existsSync5(previewDir)) {
1145
+ const previewDir = path8.join(rootDir2, "previews", name);
1146
+ if (existsSync6(previewDir)) {
1079
1147
  console.error(`Preview "${name}" already exists at: ${previewDir}`);
1080
1148
  process.exit(1);
1081
1149
  }
@@ -1200,8 +1268,8 @@ export default function App() {
1200
1268
  .dark\\:text-white { color: #fff; }
1201
1269
  }
1202
1270
  `;
1203
- writeFileSync3(path7.join(previewDir, "App.tsx"), appTsx);
1204
- writeFileSync3(path7.join(previewDir, "styles.css"), stylesCss);
1271
+ writeFileSync3(path8.join(previewDir, "App.tsx"), appTsx);
1272
+ writeFileSync3(path8.join(previewDir, "styles.css"), stylesCss);
1205
1273
  console.log(`
1206
1274
  ✨ Created preview: previews/${name}/
1207
1275
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prev-cli",
3
- "version": "0.14.1",
3
+ "version": "0.15.0",
4
4
  "description": "Transform MDX directories into beautiful documentation websites",
5
5
  "type": "module",
6
6
  "license": "MIT",