Skip to content

Commit

Permalink
refactor version check
Browse files Browse the repository at this point in the history
  • Loading branch information
gyroflaw committed Jul 31, 2023
1 parent f4be12a commit 62677d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 68 deletions.
43 changes: 0 additions & 43 deletions src/commands/fetch-versions.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/commands/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export default class Node extends Command {
});

let rpcStarted = false;
let speculativeStarted = false;
let restStarted = false;
let eventStreamStarted = false;

Expand Down Expand Up @@ -152,6 +153,18 @@ export default class Node extends Command {
kleur.green(`Started event stream server at http://127.0.0.1:9999`)
);
}

if (
data.includes("started speculative execution server") &&
!speculativeStarted
) {
speculativeStarted = true;
console.info(
kleur.green(
`Started speculative execution server at http://127.0.0.1:7778`
)
);
}
});

// log pid for further stop
Expand Down
48 changes: 24 additions & 24 deletions src/utils/check-version.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
/* eslint-disable camelcase */
import * as fs from "node:fs";
import * as path from "node:path";
import axios from "axios";

import FetchVersion from "../commands/fetch-versions";
import { WORK_DIR, githubTag } from "../config";

import { WORK_DIR } from "../config";

interface Props {
forceDownloadTags?: boolean;
export interface Tag {
name: string;
zipball_url: string;
tarball_url: string;
commit: Commit;
node_id: string;
}

export const getVersions = async (props?: Props): Promise<string[]> => {
const { forceDownloadTags = false } = props || {};
const workDir = path.resolve(__dirname, "../..", WORK_DIR);
const versionFilePath = path.resolve(workDir, "versions.json");
export interface Commit {
sha: string;
url: string;
}

// Save file tags if version file doesn't exist
if (!fs.existsSync(versionFilePath) || forceDownloadTags) {
await FetchVersion.run();
}
export const getVersions = async (): Promise<string[]> => {
const { data } = await axios.get<Tag[]>(githubTag);
const versions = data.map((tag) => tag.name);

// load version file
const { versions }: { fetchedAt: number; versions: string[] } = JSON.parse(
fs.readFileSync(versionFilePath, { encoding: "utf-8" })
);
return versions;
};

export const checkVersion = async (
version: string,
props?: Props
): Promise<boolean> => {
export const checkVersion = async (version: string): Promise<boolean> => {
if (version === "dev") return true;
const workDir = path.resolve(__dirname, "../..", WORK_DIR);

// Return true for downloaded version
if (fs.existsSync(path.resolve(workDir, version))) return true;

const versions = await getVersions(props);
const versions = await getVersions();
return versions.includes(version);
};

export const fetchLatestVersion = async (props?: Props): Promise<string> => {
const versions = await getVersions(props);
export const fetchLatestVersion = async (): Promise<string> => {
const versions = await getVersions();
return versions[0];
};
2 changes: 1 addition & 1 deletion src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function download(
const response = await fetch(url);

if (!response.ok) {
throw new Error("Unable to access");
throw new Error(`Unable to access to ${url}`);
}

return new Promise<void>((resolve) => {
Expand Down

0 comments on commit 62677d2

Please sign in to comment.