Skip to content

Commit 360a407

Browse files
authored
feat: create and update tags for pre-commit hooks (#19)
* feat: update tags * refactor: use node * feat: add dependabot updates
1 parent 1986585 commit 360a407

File tree

7 files changed

+184
-16
lines changed

7 files changed

+184
-16
lines changed

.github/dependabot.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: daily
7+
groups:
8+
github-actions:
9+
patterns: ["*"]
10+
11+
- package-ecosystem: npm
12+
directory: /
13+
schedule:
14+
interval: daily
15+
groups:
16+
npm:
17+
patterns: ["*"]

.github/workflows/dependabot-pr.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: dependabot-pr
2+
on: pull_request
3+
4+
jobs:
5+
dependabot-metadata:
6+
if: github.actor == 'dependabot[bot]'
7+
runs-on: ubuntu-latest
8+
permissions:
9+
pull-requests: read
10+
outputs:
11+
dependency-names: ${{ steps.dependabot-metadata.outputs.dependency-names }}
12+
steps:
13+
- id: dependabot-metadata
14+
uses: dependabot/fetch-metadata@v2
15+
16+
biome-update:
17+
needs: dependabot-metadata
18+
if: ${{ contains(needs.dependabot-metadata.outputs.dependency-names, 'biome') }}
19+
permissions:
20+
contents: write
21+
uses: ./.github/workflows/update.yml

.github/workflows/update.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env node
2+
3+
const util = require("util");
4+
const exec = util.promisify(require("child_process").exec);
5+
const fs = require("fs").promises;
6+
const path = require("path");
7+
8+
const { DEFAULT_BRANCH, GITHUB_ACTOR, GITHUB_ACTOR_ID } = process.env;
9+
const REPO_DIR = path.resolve(__dirname, "..", "..");
10+
const PACKAGE_NAME = "@biomejs/biome";
11+
12+
async function main() {
13+
const missingVersions = await getMissingVersions();
14+
if (!missingVersions.length) {
15+
console.log("No new versions found");
16+
return;
17+
}
18+
19+
await setGitConfig();
20+
for (const version of missingVersions) {
21+
console.log(`Updating to ${PACKAGE_NAME} ${version}`);
22+
await updateFiles(version);
23+
await commitAndPushTag(version);
24+
}
25+
26+
await git("push", "origin", `HEAD:refs/heads/${DEFAULT_BRANCH}`);
27+
}
28+
29+
async function getMissingVersions() {
30+
const allVersions = await getAllVersions();
31+
const existingVersions = await getExistingVersions();
32+
return allVersions.filter((v) => !existingVersions.includes(v)).sort();
33+
}
34+
35+
async function getAllVersions() {
36+
const allVersions = await getNodePackageVersions(PACKAGE_NAME);
37+
return allVersions.filter((v) => !v.includes("nightly")).sort();
38+
}
39+
40+
async function getExistingVersions() {
41+
const existingTags = await git("tag", "--list");
42+
return existingTags
43+
.split("\n")
44+
.map((t) => t.replace("v", "", 1))
45+
.sort();
46+
}
47+
48+
async function setGitConfig() {
49+
const email = `${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com`;
50+
await git("config", "user.name", GITHUB_ACTOR);
51+
await git("config", "user.email", email);
52+
await git("checkout", DEFAULT_BRANCH);
53+
}
54+
55+
async function getNodePackageVersions(packageName) {
56+
const { stdout } = await exec(`npm view ${packageName} --json`);
57+
const output = JSON.parse(stdout);
58+
return output.versions;
59+
}
60+
61+
async function updateFiles(version) {
62+
await updateReadme(version);
63+
await updatePackageJson(version);
64+
await exec("npm update --package-lock-only");
65+
}
66+
67+
async function updateReadme(version) {
68+
const readmeFile = path.join(REPO_DIR, "README.md");
69+
const readme = await fs.readFile(readmeFile, "utf8");
70+
const newReadme = readme.replace(/rev:\s+\S+/i, `rev: v${version}`);
71+
await fs.writeFile(readmeFile, newReadme, "utf8");
72+
}
73+
74+
async function updatePackageJson(version) {
75+
const packageJsonFile = path.join(REPO_DIR, "package.json");
76+
const packageJson = JSON.parse(await fs.readFile(packageJsonFile, "utf8"));
77+
packageJson.dependencies[PACKAGE_NAME] = version;
78+
await fs.writeFile(
79+
packageJsonFile,
80+
JSON.stringify(packageJson, null, 2),
81+
"utf8"
82+
);
83+
}
84+
85+
async function commitAndPushTag(version) {
86+
const tag = `v${version}`;
87+
await git("add", "README.md", "package.json", "package-lock.json");
88+
await git("commit", "-m", `"MAINT: upgrade to ${PACKAGE_NAME} ${version}"`);
89+
await git("tag", tag);
90+
await git("push", "origin", tag);
91+
}
92+
93+
async function git(...cmd) {
94+
const { stdout, stderr } = await exec(
95+
["git", "-C", REPO_DIR, ...cmd].join(" ")
96+
);
97+
if (stderr) console.log(stderr);
98+
return stdout;
99+
}
100+
101+
main();

.github/workflows/update.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: update
2+
on:
3+
schedule:
4+
- cron: "45 1 * * *"
5+
workflow_call:
6+
workflow_dispatch:
7+
8+
jobs:
9+
update:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
steps:
14+
- uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- uses: actions/setup-node@v4
19+
20+
- name: update tags & commits
21+
run: node .github/workflows/update.js
22+
env:
23+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
24+
GITHUB_ACTOR: github-actions[bot]
25+
GITHUB_ACTOR_ID: 41898282

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@ The following section assumes that you [installed pre-commit](https://pre-commit
88

99
Biome provides four hooks:
1010

11-
| hook `id` | description |
12-
| --------- | ----------- |
13-
| `biome-ci` | Check formatting, check if imports are organized, and lints |
14-
| `biome-check` | Format, organize imports, lint, and apply safe fixes to the committed files |
15-
| `biome-format` | Format the committed files |
16-
| `biome-lint` | Lint and apply safe fixes to the committed files |
11+
| hook `id` | description |
12+
| -------------- | --------------------------------------------------------------------------- |
13+
| `biome-ci` | Check formatting, check if imports are organized, and lints |
14+
| `biome-check` | Format, organize imports, lint, and apply safe fixes to the committed files |
15+
| `biome-format` | Format the committed files |
16+
| `biome-lint` | Lint and apply safe fixes to the committed files |
1717

1818
For example, if you want to use the `biome-check` hook,
1919
add the following pre-commit configuration to the root of your project in a file named `.pre-commit-config.yaml`:
2020

2121
```yaml
2222
repos:
23-
- repo: https://github.com/biomejs/pre-commit
24-
rev: "" # Use the sha / tag you want to point at
23+
- repo: https://github.com/biomejs/pre-commit
24+
rev: v1.9.4 # Use the sha / tag you want to point at
2525
hooks:
26-
- id: biome-check
27-
additional_dependencies: ["@biomejs/[email protected]"]
26+
- id: biome-check
2827
```
2928
30-
Note that you must specify which version of Biome to use thanks to the `additional_dependencies` option.
31-
3229
## Using biome with a local pre-commit hook
3330
3431
If Biome is already installed as a `npm` package in your local repository,

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
2-
"name": "@biomejs/pre-commit",
3-
"description": "Biome pre-commit node package",
4-
"version": "0.0.0"
5-
}
2+
"name": "@biomejs/pre-commit",
3+
"description": "Biome pre-commit node package",
4+
"version": "0.0.0",
5+
"bin": {
6+
"biome": "./node_modules/@biomejs/biome/bin/biome"
7+
},
8+
"dependencies": {
9+
"@biomejs/biome": "1.9.4"
10+
}
11+
}

0 commit comments

Comments
 (0)