-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.ts
More file actions
121 lines (107 loc) · 3.68 KB
/
utils.ts
File metadata and controls
121 lines (107 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { info, warning, debug } from "@actions/core";
import { getExecOutput } from "@actions/exec";
import { existsSync, readdirSync } from "node:fs";
import { isAbsolute, join, basename } from "node:path";
import { LockFileType } from "./types.js";
import type { LockFileInfo } from "./types.js";
export function getVitePlusHome(): string {
const home = process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME;
if (!home) throw new Error("Could not determine home directory");
return join(home, ".vite-plus");
}
export function getWorkspaceDir(): string {
return process.env.GITHUB_WORKSPACE || process.cwd();
}
export function resolveWorkspacePath(filePath: string): string {
return isAbsolute(filePath) ? filePath : join(getWorkspaceDir(), filePath);
}
// Lock file patterns in priority order
const LOCK_FILES: Array<{ filename: string; type: LockFileType }> = [
{ filename: "pnpm-lock.yaml", type: LockFileType.Pnpm },
{ filename: "package-lock.json", type: LockFileType.Npm },
{ filename: "npm-shrinkwrap.json", type: LockFileType.Npm },
{ filename: "yarn.lock", type: LockFileType.Yarn },
];
/**
* Detect lock file in the workspace
*/
export function detectLockFile(explicitPath?: string): LockFileInfo | undefined {
const workspace = getWorkspaceDir();
// If explicit path provided, use it
if (explicitPath) {
const fullPath = resolveWorkspacePath(explicitPath);
if (existsSync(fullPath)) {
const filename = basename(fullPath);
const lockInfo = LOCK_FILES.find((l) => l.filename === filename);
if (lockInfo) {
return {
type: lockInfo.type,
path: fullPath,
filename,
};
}
// Unknown lock file type - try to infer from name
return inferLockFileType(fullPath, filename);
}
return undefined;
}
// Auto-detect: search for lock files in workspace root
const workspaceContents = readdirSync(workspace);
for (const lockInfo of LOCK_FILES) {
if (workspaceContents.includes(lockInfo.filename)) {
const fullPath = join(workspace, lockInfo.filename);
info(`Auto-detected lock file: ${lockInfo.filename}`);
return {
type: lockInfo.type,
path: fullPath,
filename: lockInfo.filename,
};
}
}
return undefined;
}
function inferLockFileType(fullPath: string, filename: string): LockFileInfo {
// Infer type from filename patterns
if (filename.includes("pnpm")) {
return { type: LockFileType.Pnpm, path: fullPath, filename };
}
if (filename.includes("yarn")) {
return { type: LockFileType.Yarn, path: fullPath, filename };
}
// Default to npm
return { type: LockFileType.Npm, path: fullPath, filename };
}
/**
* Get cache directories based on package manager type
*/
export async function getCacheDirectories(lockType: LockFileType): Promise<string[]> {
switch (lockType) {
case LockFileType.Npm:
case LockFileType.Pnpm:
case LockFileType.Yarn:
return getViteCacheDir();
default:
return [];
}
}
async function getCommandOutput(command: string, args: string[]): Promise<string | undefined> {
const cmdStr = `${command} ${args.join(" ")}`;
try {
const result = await getExecOutput(command, args, {
silent: true,
ignoreReturnCode: true,
});
if (result.exitCode === 0) {
return result.stdout.trim();
}
debug(`Command "${cmdStr}" exited with code ${result.exitCode}`);
return undefined;
} catch (error) {
warning(`Failed to run "${cmdStr}": ${String(error)}`);
return undefined;
}
}
async function getViteCacheDir(): Promise<string[]> {
const cacheDir = await getCommandOutput("vp", ["pm", "cache", "dir"]);
return cacheDir ? [cacheDir] : [];
}