Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): support versions manage #16

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
isExecFile,
mkdir,
pRetry,
readFile,
request,
rm,
sleep,
tryParseJSON,
unzip,
writeFile,
} from "./utils";
import { Logger } from "./logger";
import os from "node:os";
Expand Down Expand Up @@ -70,15 +73,38 @@ export class DarwinOVM {
]);
}

private async matchVersions() {
const versionFilePath = path.join(this.options.targetDir, "versions.json");

if (!await existsFile(versionFilePath)) {
return (_key) => false;
}

const versionFileContent = await readFile(versionFilePath);
const versions = tryParseJSON<OVMDarwinOptions["versions"]>(versionFileContent);

if (!versions) {
return (_key) => false;
}

return (key: string) => {
return versions[key] === this.options.versions[key];
};
}

private async overridePath(forceOverride: boolean): Promise<void> {
await mkdir(this.options.targetDir);

const matchVersion = await this.matchVersions();

await Promise.all(Object.keys(this.options.originPath)
.map(async (k) => {
const filename = path.basename(this.options.originPath[k]);
const isZIP = filename.toLowerCase().endsWith(".zip");
const targetPath = path.join(this.options.targetDir, isZIP ? filename.slice(0, -4) : filename);

if (forceOverride || !await existsFile(targetPath)) {
const shouldOverride = forceOverride || !await existsFile(targetPath) || !matchVersion(k);
if (shouldOverride) {
if (isZIP) {
await unzip(this.options.originPath[k], this.options.targetDir);
} else {
Expand All @@ -88,6 +114,8 @@ export class DarwinOVM {

this.path[k] = targetPath;
}));

await writeFile(path.join(this.options.targetDir, "versions.json"), this.options.versions);
}

private async initPath() {
Expand Down
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface OVMDarwinOptions {
targetDir: string;
socketDir: string;
logDir: string;
versions: OVMDarwinOptions["originPath"]
}

export interface OVMEventData {
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ export const mkdir = async (p: string): Promise<void> => {
}
};

export const readFile = async (p: string): Promise<string> => {
const raw = await fs.readFile(p, "utf8");
return raw.trim();
};

export const writeFile = async (p: string, data: string | Record<string, string>): Promise<void> => {
const d = typeof data === "string" ? data : JSON.stringify(data, null, 2);
await fs.writeFile(p, d, "utf8");
};

export const tryParseJSON = <T>(raw: string): T | false => {
const r = raw.trim();
if (!r.startsWith("{") || !r.endsWith("}")) {
return false;
}

try {
return JSON.parse(r) as T;
} catch (_error) {
return false;
}
};

export const rm = (p: string): Promise<void> => {
return fs.rm(p, { force: true });
};
Expand Down