|
| 1 | +import { exec } from "./deps.ts"; |
| 2 | +import { fs } from "./deps.ts"; |
| 3 | + |
| 4 | +const version = Deno.env.get("BUMP_REQUEST_VERSION")!; |
| 5 | + |
| 6 | +if (checkCratePackage()) { |
| 7 | + changeCrateVersion(version); |
| 8 | +} else if (checkGemPackage()) { |
| 9 | + changeGemVersion(version); |
| 10 | +} else if (checkNpmPackage()) { |
| 11 | + changeNpmVersion(version); |
| 12 | +} else if (checkPlainPackage()) { |
| 13 | + changePlainVersion(version); |
| 14 | +} else { |
| 15 | + throw new Error("Unsupported package type."); |
| 16 | +} |
| 17 | + |
| 18 | +function checkCratePackage() { |
| 19 | + return fs.existsSync("Cargo.toml"); |
| 20 | +} |
| 21 | + |
| 22 | +function checkGemPackage() { |
| 23 | + return fs.expandGlobSync("lib/**/version.rb").next().value !== undefined; |
| 24 | +} |
| 25 | + |
| 26 | +function checkNpmPackage() { |
| 27 | + return fs.existsSync("package.json"); |
| 28 | +} |
| 29 | + |
| 30 | +function checkPlainPackage() { |
| 31 | + return fs.existsSync("VERSION"); |
| 32 | +} |
| 33 | + |
| 34 | +function changeCrateVersion(version: string) { |
| 35 | + replaceContent( |
| 36 | + "Cargo.toml", |
| 37 | + /version = "[^"]+"/, |
| 38 | + `version = "${version}"`, |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function changeGemVersion(version: string) { |
| 43 | + replaceContent( |
| 44 | + fs.expandGlobSync("lib/**/version.rb").next().value.path, |
| 45 | + /VERSION = "[^"]+"/, |
| 46 | + `VERSION = "${version}"`, |
| 47 | + ); |
| 48 | + exec.exec("bundle", ["install"]); |
| 49 | +} |
| 50 | + |
| 51 | +function changeNpmVersion(version: string) { |
| 52 | + exec.exec("npm", [ |
| 53 | + "version", |
| 54 | + "--no-git-commit-hooks", |
| 55 | + "--no-git-tag-version", |
| 56 | + version, |
| 57 | + ]); |
| 58 | +} |
| 59 | + |
| 60 | +function changePlainVersion(version: string) { |
| 61 | + replaceContent( |
| 62 | + "VERSION", |
| 63 | + /.+/, |
| 64 | + version, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function replaceContent( |
| 69 | + filePath: string, |
| 70 | + pattern: RegExp, |
| 71 | + replacement: string, |
| 72 | +) { |
| 73 | + const content = Deno.readTextFileSync(filePath); |
| 74 | + const updatedContent = content.replace(pattern, replacement); |
| 75 | + Deno.writeTextFileSync(filePath, updatedContent); |
| 76 | +} |
0 commit comments