pi-rtk-optimizer 0.3.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.
@@ -0,0 +1,84 @@
1
+ interface WindowsBashCompatibilityResult {
2
+ command: string;
3
+ applied: string[];
4
+ }
5
+
6
+ const PYTHON_UTF8_ENV_PREFIX = "PYTHONIOENCODING=utf-8";
7
+
8
+ function normalizeWindowsPathForBash(rawPath: string): string {
9
+ const trimmed = rawPath.trim();
10
+ const unquoted =
11
+ (trimmed.startsWith('"') && trimmed.endsWith('"')) ||
12
+ (trimmed.startsWith("'") && trimmed.endsWith("'"))
13
+ ? trimmed.slice(1, -1)
14
+ : trimmed;
15
+ return unquoted.replace(/\\/g, "/");
16
+ }
17
+
18
+ function quoteForBash(value: string): string {
19
+ const escaped = value.replace(/"/g, '\\"');
20
+ return `"${escaped}"`;
21
+ }
22
+
23
+ function rewriteLeadingCdSlashD(command: string): { command: string; changed: boolean } {
24
+ const withTailMatch = command.match(/^\s*cd\s+\/d\s+(.+?)\s*&&\s*([\s\S]+)$/i);
25
+ if (withTailMatch) {
26
+ const rawPath = withTailMatch[1] ?? "";
27
+ const tail = withTailMatch[2] ?? "";
28
+ const normalizedPath = quoteForBash(normalizeWindowsPathForBash(rawPath));
29
+ return {
30
+ command: `cd ${normalizedPath} && ${tail}`,
31
+ changed: true,
32
+ };
33
+ }
34
+
35
+ const onlyCdMatch = command.match(/^\s*cd\s+\/d\s+(.+)$/i);
36
+ if (onlyCdMatch) {
37
+ const rawPath = onlyCdMatch[1] ?? "";
38
+ const normalizedPath = quoteForBash(normalizeWindowsPathForBash(rawPath));
39
+ return {
40
+ command: `cd ${normalizedPath}`,
41
+ changed: true,
42
+ };
43
+ }
44
+
45
+ return { command, changed: false };
46
+ }
47
+
48
+ function ensurePythonUtf8(command: string): { command: string; changed: boolean } {
49
+ if (/\bPYTHONIOENCODING\s*=/.test(command)) {
50
+ return { command, changed: false };
51
+ }
52
+
53
+ if (!/(^|[;&|]\s*|&&\s*|\|\|\s*)python(?:3(?:\.\d+)?)?\b/i.test(command)) {
54
+ return { command, changed: false };
55
+ }
56
+
57
+ return {
58
+ command: `${PYTHON_UTF8_ENV_PREFIX} ${command}`,
59
+ changed: true,
60
+ };
61
+ }
62
+
63
+ export function applyWindowsBashCompatibilityFixes(command: string): WindowsBashCompatibilityResult {
64
+ if (process.platform !== "win32") {
65
+ return { command, applied: [] };
66
+ }
67
+
68
+ let nextCommand = command;
69
+ const applied: string[] = [];
70
+
71
+ const cdFix = rewriteLeadingCdSlashD(nextCommand);
72
+ if (cdFix.changed) {
73
+ nextCommand = cdFix.command;
74
+ applied.push("cd-/d");
75
+ }
76
+
77
+ const pythonFix = ensurePythonUtf8(nextCommand);
78
+ if (pythonFix.changed) {
79
+ nextCommand = pythonFix.command;
80
+ applied.push("python-utf8");
81
+ }
82
+
83
+ return { command: nextCommand, applied };
84
+ }