From 38bbbab4a5603993e6eab798761951a866c267b1 Mon Sep 17 00:00:00 2001 From: Pine Date: Thu, 24 Oct 2024 20:51:43 +0200 Subject: [PATCH] centralize systemVersions --- cli/src/commands/info/index.ts | 31 ++++------------------------ cli/src/commands/new/index.ts | 32 ++++------------------------- cli/src/util/systemVersions.ts | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 55 deletions(-) create mode 100644 cli/src/util/systemVersions.ts diff --git a/cli/src/commands/info/index.ts b/cli/src/commands/info/index.ts index 8e6be09a..c8636c45 100644 --- a/cli/src/commands/info/index.ts +++ b/cli/src/commands/info/index.ts @@ -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 = {}; @@ -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", }; } @@ -99,27 +99,4 @@ export default class InfoCommand extends Command { this.log(); } - - async getNPMVersion(): Promise { - const { stdout } = await execFileWithExitCode("npm", ["--version"], { shell: true }); - return stdout.trim(); - } - - async getGoVersion(): Promise { - try { - const { stdout } = await execFileWithExitCode("go", ["version"], { shell: true }); - return stdout.trim().split(" ")[2]; - } catch (error) { - return "Not installed"; - } - } - - async getTinyGoVersion(): Promise { - try { - const { stdout } = await execFileWithExitCode("tinygo", ["version"], { shell: true }); - return stdout.trim().split(" ")[2]; - } catch (error) { - return "Not installed"; - } - } } diff --git a/cli/src/commands/new/index.ts b/cli/src/commands/new/index.ts index 86d39177..12bf50bd 100644 --- a/cli/src/commands/new/index.ts +++ b/cli/src/commands/new/index.ts @@ -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"; @@ -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))) { @@ -317,33 +320,6 @@ export default class NewCommand extends Command { } } -async function getGoVersion(): Promise { - 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 { - 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. diff --git a/cli/src/util/systemVersions.ts b/cli/src/util/systemVersions.ts new file mode 100644 index 00000000..db47eab9 --- /dev/null +++ b/cli/src/util/systemVersions.ts @@ -0,0 +1,37 @@ +import { execFile } from "./cp.js"; +import { ModusHomeDir } from "../custom/globals.js"; + +export async function getGoVersion(): Promise { + 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 { + 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 { + try { + const result = await execFile("npm", ["--version"], { shell: true }); + const parts = result.stdout.split(" "); + return parts.length > 0 ? parts[0].trim() : undefined; + } catch {} +}