diff --git a/tooling/cli/src/commands/use.test.ts b/tooling/cli/src/commands/use.test.ts index 2afec593..f3362df9 100644 --- a/tooling/cli/src/commands/use.test.ts +++ b/tooling/cli/src/commands/use.test.ts @@ -1,4 +1,9 @@ -import { updateCargoToml, updatePackageJson, updatePubspecYaml } from "./use"; +import { + updateCargoToml, + updateGoMod, + updatePackageJson, + updatePubspecYaml, +} from "./use"; describe("updatePackageJson()", () => { it("updates relevant lines and preserves formatting", () => { @@ -115,3 +120,66 @@ tokio = { version = "1.39.2", features = ["full"] }`; expect(output.updated).toBe(true); }); }); + +describe("updateGoMod()", () => { + it("updates relevant lines", () => { + const input = `module main + +go 1.23.3 + +require github.com/modiimedia/arri v0.66.0 + +require ( + github.com/iancoleman/strcase v0.3.0 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect +) +`; + const expectedOutput = `module main + +go 1.23.3 + +require github.com/modiimedia/arri v1.12.0 + +require ( + github.com/iancoleman/strcase v0.3.0 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect +) +`; + const output = updateGoMod(input, "1.12.0"); + expect(output.content).toBe(expectedOutput); + }); + it("updates relevant lines and preserves comments", () => { + const input = `module main + +go 1.23.3 + +require github.com/modiimedia/arri v0.66.0 // this is a comment + +require ( + github.com/iancoleman/strcase v0.3.0 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect +) +`; + const expectedOutput = `module main + +go 1.23.3 + +require github.com/modiimedia/arri v1.12.0 // this is a comment + +require ( + github.com/iancoleman/strcase v0.3.0 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect +) +`; + const output = updateGoMod(input, "1.12.0"); + expect(output.content).toBe(expectedOutput); + }); +}); diff --git a/tooling/cli/src/commands/use.ts b/tooling/cli/src/commands/use.ts index 0f3ff6b7..b074ae76 100644 --- a/tooling/cli/src/commands/use.ts +++ b/tooling/cli/src/commands/use.ts @@ -110,6 +110,20 @@ export default defineCommand({ } await Promise.all(cargoTomlTasks); + // GO DEPENDENCIES + const goModFiles = await globby(["go.mod", "**/go.mod"]); + const goModFileTasks: Promise[] = []; + for (const file of goModFiles) { + goModFileTasks.push( + readFile(path.resolve(file), "utf8").then((content) => { + const result = updateGoMod(content, version); + if (result.updated) { + fileMap[file] = result.content; + } + }), + ); + } + // Update all affected files const updateTasks: Promise[] = []; for (const key of Object.keys(fileMap)) { @@ -353,3 +367,37 @@ export function updateCargoToml( } return { updated, content: newLines.join("\n") }; } + +export function updateGoMod( + fileContent: string, + version: string, +): { updated: boolean; content: string } { + const lines = fileContent.split("\n"); + const newLines: string[] = []; + let updated = false; + for (const line of lines) { + if (!line.includes("github.com/modiimedia/arri")) { + newLines.push(line); + continue; + } + updated = true; + const lineParts = line.split(" "); + const updatedLineParts: string[] = []; + for (const part of lineParts) { + if (part.startsWith("v") && isNumberChar(part[1] ?? "")) { + updatedLineParts.push(`v${version}`); + continue; + } + updatedLineParts.push(part); + } + newLines.push(updatedLineParts.join(" ")); + } + return { + updated, + content: newLines.join("\n"), + }; +} + +function isNumberChar(char: string) { + return !Number.isNaN(Number(char)); +}