Skip to content

Commit

Permalink
centralize systemVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Oct 24, 2024
1 parent 9d868a9 commit 38bbbab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 55 deletions.
31 changes: 4 additions & 27 deletions cli/src/commands/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import os from "node:os";
import * as vi from "../../util/versioninfo.js";
import { SDK } from "../../custom/globals.js";
import { getHeader } from "../../custom/header.js";
import { execFileWithExitCode } from "../../util/cp.js";
import { getGoVersion, getNPMVersion, getTinyGoVersion } from "../../util/systemVersions.js";

export default class InfoCommand extends Command {
static args = {};
Expand Down Expand Up @@ -45,9 +45,9 @@ export default class InfoCommand extends Command {
"Operating System": `${os.type()} ${os.release()}`,
"Platform Architecture": process.arch,
"Node.js Version": process.version,
"NPM Version": await this.getNPMVersion(),
"Go Version": await this.getGoVersion(),
"TinyGo Version": await this.getTinyGoVersion(),
"NPM Version": await getNPMVersion() || "Not installed",
"Go Version": await getGoVersion() || "Not installed",
"TinyGo Version": await getTinyGoVersion() || "Not installed",
};
}

Expand Down Expand Up @@ -99,27 +99,4 @@ export default class InfoCommand extends Command {

this.log();
}

async getNPMVersion(): Promise<string> {
const { stdout } = await execFileWithExitCode("npm", ["--version"], { shell: true });
return stdout.trim();
}

async getGoVersion(): Promise<string> {
try {
const { stdout } = await execFileWithExitCode("go", ["version"], { shell: true });
return stdout.trim().split(" ")[2];
} catch (error) {
return "Not installed";
}
}

async getTinyGoVersion(): Promise<string> {
try {
const { stdout } = await execFileWithExitCode("tinygo", ["version"], { shell: true });
return stdout.trim().split(" ")[2];
} catch (error) {
return "Not installed";
}
}
}
32 changes: 4 additions & 28 deletions cli/src/commands/new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { extract } from "../../util/tar.js";
import SDKInstallCommand from "../sdk/install/index.js";
import { getHeader } from "../../custom/header.js";
import * as inquirer from "@inquirer/prompts";
import { getGoVersion, getTinyGoVersion } from "../../util/systemVersions.js";

const MODUS_DEFAULT_TEMPLATE_NAME = "default";

Expand Down Expand Up @@ -102,13 +103,15 @@ export default class NewCommand extends Command {
}

const defaultAppName = getDefaultAppNameBySdk(sdk);
const name =
let name =
flags.name ||
(await inquirer.input({
message: "Pick a name for your app:",
default: defaultAppName,
}));

name = toValidAppName(name);

const dir = flags.dir || "." + path.sep + name;

if (!flags.force && (await fs.exists(dir))) {
Expand Down Expand Up @@ -317,33 +320,6 @@ export default class NewCommand extends Command {
}
}

async function getGoVersion(): Promise<string | undefined> {
try {
const result = await execFile("go", ["version"], {
shell: true,
cwd: ModusHomeDir,
env: process.env,
});
const parts = result.stdout.split(" ");
const str = parts.length > 2 ? parts[2] : undefined;
if (str?.startsWith("go")) {
return str.slice(2);
}
} catch {}
}

async function getTinyGoVersion(): Promise<string | undefined> {
try {
const result = await execFile("tinygo", ["version"], {
shell: true,
cwd: ModusHomeDir,
env: process.env,
});
const parts = result.stdout.split(" ");
return parts.length > 2 ? parts[2] : undefined;
} catch {}
}

function toValidAppName(input: string): string {
// Remove any characters that aren't alphanumeric, spaces, or a few other valid characters.
// Replace spaces with hyphens.
Expand Down
37 changes: 37 additions & 0 deletions cli/src/util/systemVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { execFile } from "./cp.js";
import { ModusHomeDir } from "../custom/globals.js";

export async function getGoVersion(): Promise<string | undefined> {
try {
const result = await execFile("go", ["version"], {
shell: true,
cwd: ModusHomeDir,
env: process.env,
});
const parts = result.stdout.split(" ");
const str = parts.length > 2 ? parts[2] : undefined;
if (str?.startsWith("go")) {
return str.slice(2);
}
} catch {}
}

export async function getTinyGoVersion(): Promise<string | undefined> {
try {
const result = await execFile("tinygo", ["version"], {
shell: true,
cwd: ModusHomeDir,
env: process.env,
});
const parts = result.stdout.split(" ");
return parts.length > 2 ? parts[2] : undefined;
} catch {}
}

export async function getNPMVersion(): Promise<string | undefined> {
try {
const result = await execFile("npm", ["--version"], { shell: true });
const parts = result.stdout.split(" ");
return parts.length > 0 ? parts[0].trim() : undefined;
} catch {}
}

0 comments on commit 38bbbab

Please sign in to comment.