prev-cli 0.14.0 → 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.
- package/dist/cli.js +125 -10
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
4
|
import { parseArgs } from "util";
|
|
5
|
-
import
|
|
6
|
-
import { existsSync as
|
|
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
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
7
8
|
|
|
8
9
|
// src/vite/start.ts
|
|
9
10
|
import { createServer as createServer2, build as build2, preview } from "vite";
|
|
@@ -895,6 +896,9 @@ async function findAvailablePort(minPort, maxPort) {
|
|
|
895
896
|
}
|
|
896
897
|
|
|
897
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";
|
|
898
902
|
function printWelcome(type) {
|
|
899
903
|
console.log();
|
|
900
904
|
console.log(" ✨ prev");
|
|
@@ -905,12 +909,69 @@ function printWelcome(type) {
|
|
|
905
909
|
console.log(" Previewing your production build:");
|
|
906
910
|
}
|
|
907
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
|
+
}
|
|
908
921
|
function printReady() {
|
|
909
922
|
console.log();
|
|
910
923
|
console.log(" Edit your .md/.mdx files and see changes instantly.");
|
|
911
|
-
console.log(" Press
|
|
924
|
+
console.log(" Press h for shortcuts.");
|
|
912
925
|
console.log();
|
|
913
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
|
+
}
|
|
914
975
|
async function startDev(rootDir, options = {}) {
|
|
915
976
|
const port = options.port ?? await getRandomPort();
|
|
916
977
|
const config = await createViteConfig({
|
|
@@ -921,9 +982,17 @@ async function startDev(rootDir, options = {}) {
|
|
|
921
982
|
});
|
|
922
983
|
const server = await createServer2(config);
|
|
923
984
|
await server.listen();
|
|
985
|
+
const actualPort = server.config.server.port || port;
|
|
986
|
+
const url = `http://localhost:${actualPort}/`;
|
|
924
987
|
printWelcome("dev");
|
|
925
988
|
server.printUrls();
|
|
926
989
|
printReady();
|
|
990
|
+
setupKeyboardShortcuts(rootDir, url, async () => {
|
|
991
|
+
console.log(`
|
|
992
|
+
Shutting down...`);
|
|
993
|
+
await server.close();
|
|
994
|
+
process.exit(0);
|
|
995
|
+
});
|
|
927
996
|
return server;
|
|
928
997
|
}
|
|
929
998
|
async function buildSite(rootDir, options = {}) {
|
|
@@ -960,6 +1029,21 @@ async function previewSite(rootDir, options = {}) {
|
|
|
960
1029
|
}
|
|
961
1030
|
|
|
962
1031
|
// src/cli.ts
|
|
1032
|
+
function getVersion() {
|
|
1033
|
+
try {
|
|
1034
|
+
let dir = path8.dirname(fileURLToPath3(import.meta.url));
|
|
1035
|
+
for (let i = 0;i < 5; i++) {
|
|
1036
|
+
const pkgPath = path8.join(dir, "package.json");
|
|
1037
|
+
if (existsSync6(pkgPath)) {
|
|
1038
|
+
const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
1039
|
+
if (pkg.name === "prev-cli")
|
|
1040
|
+
return pkg.version;
|
|
1041
|
+
}
|
|
1042
|
+
dir = path8.dirname(dir);
|
|
1043
|
+
}
|
|
1044
|
+
} catch {}
|
|
1045
|
+
return "unknown";
|
|
1046
|
+
}
|
|
963
1047
|
var { values, positionals } = parseArgs({
|
|
964
1048
|
args: process.argv.slice(2),
|
|
965
1049
|
options: {
|
|
@@ -967,12 +1051,13 @@ var { values, positionals } = parseArgs({
|
|
|
967
1051
|
days: { type: "string", short: "d" },
|
|
968
1052
|
cwd: { type: "string", short: "c" },
|
|
969
1053
|
include: { type: "string", short: "i", multiple: true },
|
|
970
|
-
help: { type: "boolean", short: "h" }
|
|
1054
|
+
help: { type: "boolean", short: "h" },
|
|
1055
|
+
version: { type: "boolean", short: "v" }
|
|
971
1056
|
},
|
|
972
1057
|
allowPositionals: true
|
|
973
1058
|
});
|
|
974
1059
|
var command = positionals[0] || "dev";
|
|
975
|
-
var rootDir =
|
|
1060
|
+
var rootDir = path8.resolve(values.cwd || positionals[1] || ".");
|
|
976
1061
|
function printHelp() {
|
|
977
1062
|
console.log(`
|
|
978
1063
|
prev - Zero-config documentation site generator
|
|
@@ -982,7 +1067,8 @@ Usage:
|
|
|
982
1067
|
prev build [options] Build for production
|
|
983
1068
|
prev preview [options] Preview production build
|
|
984
1069
|
prev create [name] Create preview in previews/<name>/ (default: "example")
|
|
985
|
-
prev
|
|
1070
|
+
prev clearcache Clear Vite cache (.vite directory)
|
|
1071
|
+
prev clean [options] Remove old prev-cli caches
|
|
986
1072
|
|
|
987
1073
|
Options:
|
|
988
1074
|
-c, --cwd <path> Set working directory
|
|
@@ -990,6 +1076,7 @@ Options:
|
|
|
990
1076
|
-i, --include <dir> Include dot-prefixed directory (can use multiple times)
|
|
991
1077
|
-d, --days <days> Cache age threshold for clean (default: 30)
|
|
992
1078
|
-h, --help Show this help message
|
|
1079
|
+
-v, --version Show version number
|
|
993
1080
|
|
|
994
1081
|
Previews:
|
|
995
1082
|
Previews must be in the previews/ directory at your project root.
|
|
@@ -1033,9 +1120,30 @@ Examples:
|
|
|
1033
1120
|
prev clean -d 7 Remove caches older than 7 days
|
|
1034
1121
|
`);
|
|
1035
1122
|
}
|
|
1123
|
+
function clearViteCache(rootDir2) {
|
|
1124
|
+
const viteCacheDir = path8.join(rootDir2, ".vite");
|
|
1125
|
+
const nodeModulesVite = path8.join(rootDir2, "node_modules", ".vite");
|
|
1126
|
+
let cleared = 0;
|
|
1127
|
+
if (existsSync6(viteCacheDir)) {
|
|
1128
|
+
rmSync3(viteCacheDir, { recursive: true });
|
|
1129
|
+
cleared++;
|
|
1130
|
+
console.log(` ✓ Removed .vite/`);
|
|
1131
|
+
}
|
|
1132
|
+
if (existsSync6(nodeModulesVite)) {
|
|
1133
|
+
rmSync3(nodeModulesVite, { recursive: true });
|
|
1134
|
+
cleared++;
|
|
1135
|
+
console.log(` ✓ Removed node_modules/.vite/`);
|
|
1136
|
+
}
|
|
1137
|
+
if (cleared === 0) {
|
|
1138
|
+
console.log(" No Vite cache found");
|
|
1139
|
+
} else {
|
|
1140
|
+
console.log(`
|
|
1141
|
+
Cleared ${cleared} cache director${cleared === 1 ? "y" : "ies"}`);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1036
1144
|
function createPreview(rootDir2, name) {
|
|
1037
|
-
const previewDir =
|
|
1038
|
-
if (
|
|
1145
|
+
const previewDir = path8.join(rootDir2, "previews", name);
|
|
1146
|
+
if (existsSync6(previewDir)) {
|
|
1039
1147
|
console.error(`Preview "${name}" already exists at: ${previewDir}`);
|
|
1040
1148
|
process.exit(1);
|
|
1041
1149
|
}
|
|
@@ -1160,8 +1268,8 @@ export default function App() {
|
|
|
1160
1268
|
.dark\\:text-white { color: #fff; }
|
|
1161
1269
|
}
|
|
1162
1270
|
`;
|
|
1163
|
-
writeFileSync3(
|
|
1164
|
-
writeFileSync3(
|
|
1271
|
+
writeFileSync3(path8.join(previewDir, "App.tsx"), appTsx);
|
|
1272
|
+
writeFileSync3(path8.join(previewDir, "styles.css"), stylesCss);
|
|
1165
1273
|
console.log(`
|
|
1166
1274
|
✨ Created preview: previews/${name}/
|
|
1167
1275
|
|
|
@@ -1178,6 +1286,10 @@ export default function App() {
|
|
|
1178
1286
|
`);
|
|
1179
1287
|
}
|
|
1180
1288
|
async function main() {
|
|
1289
|
+
if (values.version) {
|
|
1290
|
+
console.log(`prev-cli v${getVersion()}`);
|
|
1291
|
+
process.exit(0);
|
|
1292
|
+
}
|
|
1181
1293
|
if (values.help) {
|
|
1182
1294
|
printHelp();
|
|
1183
1295
|
process.exit(0);
|
|
@@ -1204,6 +1316,9 @@ async function main() {
|
|
|
1204
1316
|
const previewName = positionals[1] || "example";
|
|
1205
1317
|
createPreview(rootDir, previewName);
|
|
1206
1318
|
break;
|
|
1319
|
+
case "clearcache":
|
|
1320
|
+
clearViteCache(rootDir);
|
|
1321
|
+
break;
|
|
1207
1322
|
default:
|
|
1208
1323
|
console.error(`Unknown command: ${command}`);
|
|
1209
1324
|
printHelp();
|