|
| 1 | +/** |
| 2 | + * This script checks for any prettier updates and then automatically |
| 3 | + * publishes a new version of the plugin if so. |
| 4 | + */ |
| 5 | +import * as path from "https://deno.land/[email protected]/path/mod.ts"; |
| 6 | +import * as semver from "https://deno.land/x/[email protected]/mod.ts"; |
| 7 | + |
| 8 | +const rootDirPath = path.dirname(path.dirname(path.fromFileUrl(import.meta.url))); |
| 9 | + |
| 10 | +console.log("Upgrading prettier..."); |
| 11 | +const npmCommand = Deno.build.os === "windows" ? "npm.cmd" : "npm"; |
| 12 | +await runCommand(`${npmCommand} install --save prettier`.split(" "), { |
| 13 | + cwd: path.join(rootDirPath, "./js/node"), |
| 14 | +}); |
| 15 | + |
| 16 | +if (!await hasFileChanged("./js/node/package.json")) { |
| 17 | + console.log("No changes."); |
| 18 | + Deno.exit(0); |
| 19 | +} |
| 20 | + |
| 21 | +console.log("Found changes. Bumping version..."); |
| 22 | +const newVersion = await bumpMinorVersion(); |
| 23 | + |
| 24 | +// run the tests |
| 25 | +console.log("Running tests..."); |
| 26 | +await runCommand("cargo test".split(" ")); |
| 27 | + |
| 28 | +// release |
| 29 | +console.log(`Committing and publishing ${newVersion}...`); |
| 30 | +await runCommand("git add .".split(" ")); |
| 31 | +await runCommand(`git commit -m ${newVersion}`.split(" ")); |
| 32 | +await runCommand(`git push origin main`.split(" ")); |
| 33 | +await runCommand(`git tag ${newVersion}`.split(" ")); |
| 34 | +await runCommand(`git push origin ${newVersion}`.split(" ")); |
| 35 | + |
| 36 | +async function bumpMinorVersion() { |
| 37 | + const projectFile = path.join(rootDirPath, "./Cargo.toml"); |
| 38 | + const text = await Deno.readTextFile(projectFile); |
| 39 | + const versionRegex = /^version = "([0-9]+\.[0-9]+\.[0-9]+)"/m; |
| 40 | + const currentVersion = text.match(versionRegex)?.[1]; |
| 41 | + if (currentVersion == null) { |
| 42 | + throw new Error("Could not find version."); |
| 43 | + } |
| 44 | + const newVersion = semver.parse(currentVersion)!.inc("minor").toString(); |
| 45 | + const newText = text.replace(versionRegex, `version = "${newVersion}"`); |
| 46 | + await Deno.writeTextFile(projectFile, newText); |
| 47 | + return newVersion; |
| 48 | +} |
| 49 | + |
| 50 | +async function hasFileChanged(file: string) { |
| 51 | + try { |
| 52 | + await runCommand(["git", "diff", "--exit-code", file]); |
| 53 | + return false; |
| 54 | + } catch { |
| 55 | + return true; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function runCommand(cmd: string[], opts?: { |
| 60 | + cwd?: string; |
| 61 | +}) { |
| 62 | + const p = Deno.run({ |
| 63 | + cmd, |
| 64 | + cwd: opts?.cwd ?? rootDirPath, |
| 65 | + }); |
| 66 | + const status = await p.status(); |
| 67 | + p.close(); |
| 68 | + if (status.code !== 0) { |
| 69 | + throw new Error("Failed."); |
| 70 | + } |
| 71 | +} |
0 commit comments