Skip to content

Commit 26bd4ef

Browse files
committed
Add first version of code, docs and tests
1 parent 9639d7e commit 26bd4ef

24 files changed

+7809
-0
lines changed

.commitlintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["bloq"]
3+
}

.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": ["bloq", "bloq/node", "prettier"],
3+
"overrides": [
4+
{
5+
"files": ["bin/*"],
6+
"rules": {
7+
"no-console": "off",
8+
"no-process-exit": "off"
9+
}
10+
},
11+
{
12+
"extends": ["bloq/mocha", "prettier"],
13+
"files": ["*.test.js"]
14+
}
15+
],
16+
"root": true
17+
}

.github/workflows/js-checks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: JS Checks
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
9+
cancel-in-progress: true
10+
11+
jobs:
12+
run-checks-and-tests:
13+
uses: bloq/actions/.github/workflows/js-checks.yml@v1
14+
with:
15+
node-versions: '["20","22","24"]'

.github/workflows/npm-publish.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: NPM Publish
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
jobs:
9+
publish-to-npm:
10+
permissions:
11+
contents: read
12+
id-token: write
13+
uses: bloq/actions/.github/workflows/npm-publish.yml@v1
14+
secrets:
15+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.eslintcache
2+
node_modules

.husky/commit-msg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
commitlint --edit $1

.husky/pre-commit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
lint-staged

.husky/pre-push

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
npm test

.knip.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignoreDependencies": ["commitlint-config-bloq", "eslint-config-prettier"]
3+
}

.lintstagedrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"!(.github/workflows/*.yml|*.js|package.json)": [
3+
"prettier --ignore-unknown --write"
4+
],
5+
".github/workflows/*.yml": ["npx .", "prettier --write"],
6+
"*.js": ["eslint --cache --fix --quiet", "prettier --write"],
7+
"package.json": ["better-sort-package-json", "prettier --write"]
8+
}

.nvmrc

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

.prettierrc.json

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

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# better-sort-github-actions-files
2+
3+
Sorts GitHub Actions workflow or composite action files following these rules:
4+
5+
1. The known properties are sorted following the order in the [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions) or [Metadata syntax for GitHub Actions](https://docs.github.com/en/actions/sharing-automations/creating-actions/metadata-syntax-for-github-actions).
6+
1. All other properties are sorted in alphabetical order.
7+
8+
## Motivation and prior art
9+
10+
Having a consistent properties order in the files helps with the readability and maintainability but following the exact order of the docs can be tricky when done manually. We resolved the same problem in the past for [package.json](https://github.com/hemilabs/better-sort-package-json) files and now was the turn of these YAML files.
11+
12+
## CLI
13+
14+
This package can be installed globally, then used to sort GitHub Actions files:
15+
16+
```sh
17+
npm install --global better-sort-github-actions
18+
better-sort-github-actions <path-to-a-github-actions-file-to-sort>
19+
```
20+
21+
In addition, it can be used without installing it:
22+
23+
```sh
24+
npx better-sort-github-actions <path-to-a-github-actions-file-to-sort>
25+
```
26+
27+
## API
28+
29+
Install the package:
30+
31+
```sh
32+
npm install better-sort-github-actions
33+
```
34+
35+
Then use it to sort the contents of a YAML file:
36+
37+
```js
38+
const fs = require("node:fs");
39+
const { sort } = require("better-sort-github-actions");
40+
41+
fs.writeFileSync(path, sort(fs.readFileSync(path, "utf8")));
42+
```
43+
44+
## Automatically sort on commit
45+
46+
To let [`lint-staged`](https://github.com/lint-staged/lint-staged) take care of sorting the GitHub Actions files automatically use:
47+
48+
```json
49+
{
50+
".github/workflows/*.yml": ["better-sort-github-actions"]
51+
}
52+
```

bin/cli.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env node
2+
3+
"use strict";
4+
5+
const { readFileSync, writeFileSync } = require("node:fs");
6+
7+
const { sort } = require("..");
8+
9+
const files = process.argv.slice(2);
10+
if (!files.length) {
11+
console.error("Usage: better-sort-github-actions <file> [<file>...]");
12+
process.exit(1);
13+
}
14+
15+
// TODO check how globs are handled
16+
17+
try {
18+
files.forEach(function (file) {
19+
writeFileSync(file, sort(readFileSync(file, "utf8")));
20+
});
21+
} catch (error) {
22+
console.error(`Error: ${error.message}`);
23+
process.exit(1);
24+
}

0 commit comments

Comments
 (0)