Skip to content

Commit 4687994

Browse files
authored
feat: automatically release when prettier updates (dprint#29)
1 parent ad49e14 commit 4687994

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

.github/workflows/check_updates.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: check_updates
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
# do this three times a week
7+
- cron: "0 7 * * 1,3,5"
8+
9+
jobs:
10+
build:
11+
name: check updates
12+
if: github.repository == 'dprint/dprint-plugin-prettier'
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 30
15+
16+
env:
17+
CARGO_INCREMENTAL: 0
18+
RUST_BACKTRACE: full
19+
CFG_RELEASE_CHANNEL: nightly
20+
21+
steps:
22+
- name: Clone repository
23+
uses: actions/checkout@v2
24+
with:
25+
token: ${{ secrets.GH_DPRINTBOT_PAT }}
26+
27+
- uses: denoland/setup-deno@v1
28+
- uses: actions/checkout@v2
29+
- uses: dtolnay/rust-toolchain@stable
30+
31+
- name: Run script
32+
run: |
33+
git config user.email "[email protected]"
34+
git config user.name "dprintbot"
35+
deno run -A ./scripts/update.ts

.rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tab_spaces = 2
2+
edition = "2021"

dprint.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"incremental": true,
33
"indentWidth": 2,
4-
"rustfmt": {
5-
"imports_granularity": "Item"
4+
"exec": {
5+
"associations": "**/*.rs",
6+
"rustfmt": "rustfmt"
67
},
78
"includes": ["**/*.{ts,tsx,js,jsx,json,md,rs}"],
89
"excludes": [
@@ -16,6 +17,6 @@
1617
"https://plugins.dprint.dev/typescript-0.65.1.wasm",
1718
"https://plugins.dprint.dev/json-0.7.2.wasm",
1819
"https://plugins.dprint.dev/markdown-0.12.2.wasm",
19-
"https://plugins.dprint.dev/rustfmt-0.6.0.exe-plugin@8b65ed724170bd227e92a2f01d867e452ef7f26e78dc691999ffa37a276df27c"
20+
"https://plugins.dprint.dev/exec-0.3.1.json@9351b67ec7a6b58a69201c2834cba38cb3d191080aefc6422fb1320f03c8fc4d"
2021
]
2122
}

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "1.60.0"
2+
channel = "1.62.0"

scripts/update.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)