UNPKG

@mesh-tech/mesh-cli

Version:

CLI for Mesh platform development utilities

47 lines (46 loc) 1.51 kB
import * as fs from "fs"; import * as path from "path"; export const MESH_JSON = "mesh.json"; export function isValidTenantName(value) { return /^[a-z][a-z0-9-]*$/.test(value); } export function parseMeshJson(content) { try { const data = JSON.parse(content); if (typeof data?.tenant !== "string" || !isValidTenantName(data.tenant)) return null; const platform = typeof data.platform === "string" && data.platform ? data.platform : "local"; return { tenant: data.tenant, platform }; } catch { return null; } } export function findMeshJson(startDir) { let dir = path.resolve(startDir); while (true) { const candidate = path.join(dir, MESH_JSON); if (fs.existsSync(candidate)) { let content = ""; try { content = fs.readFileSync(candidate, "utf-8"); } catch { return null; } const data = parseMeshJson(content); return data ? { path: candidate, data } : null; } if (fs.existsSync(path.join(dir, ".git"))) return null; const parent = path.dirname(dir); if (parent === dir) return null; dir = parent; } } export function writeMeshJson(rootDir, data) { const target = path.join(rootDir, MESH_JSON); fs.writeFileSync(target, `${JSON.stringify({ tenant: data.tenant, platform: data.platform }, null, 2)}\n`); return target; }