Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["bloq"]
}
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": ["bloq", "bloq/node", "prettier"],
"overrides": [
{
"files": ["bin/*"],
"rules": {
"no-console": "off",
"no-process-exit": "off"
}
},
{
"extends": ["bloq/mocha", "prettier"],
"files": ["*.test.js"]
}
],
"root": true
}
15 changes: 15 additions & 0 deletions .github/workflows/js-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: JS Checks

on:
pull_request:
push:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

jobs:
run-checks-and-tests:
uses: hemilabs/actions/.github/workflows/js-checks.yml@63592c8da7b84d0953f8ed3b7ca89d43142a8605 # PR#22
with:
node-versions: '["20","22","24"]'
15 changes: 15 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: NPM Publish

on:
release:
types:
- published

jobs:
publish-to-npm:
permissions:
contents: read
id-token: write
uses: hemilabs/actions/.github/workflows/npm-publish.yml@63592c8da7b84d0953f8ed3b7ca89d43142a8605 # PR#22
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.eslintcache
node_modules
3 changes: 3 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

commitlint --edit $1
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

lint-staged
3 changes: 3 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

npm test
3 changes: 3 additions & 0 deletions .knip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignoreDependencies": ["eslint-config-prettier"]
}
8 changes: 8 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"!(.github/workflows/*.yml|*.js|package.json)": [
"prettier --ignore-unknown --write"
],
".github/workflows/*.yml": ["npx .", "prettier --write"],
"*.js": ["eslint --cache --fix --quiet", "prettier --write"],
"package.json": ["better-sort-package-json", "prettier --write"]
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# better-sort-github-actions-files

Sorts GitHub Actions workflow or composite action files following these rules:

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).
1. All other properties are sorted in alphabetical order.

## Motivation and prior art

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.

## CLI

This package can be installed globally, then used to sort GitHub Actions files:

```sh
npm install --global better-sort-github-actions
better-sort-github-actions <path-to-a-github-actions-file-to-sort>
```

In addition, it can be used without installing it:

```sh
npx better-sort-github-actions <path-to-a-github-actions-file-to-sort>
```

## API

Install the package:

```sh
npm install better-sort-github-actions
```

Then use it to sort the contents of a YAML file:

```js
const fs = require("node:fs");
const { sort } = require("better-sort-github-actions");

fs.writeFileSync(path, sort(fs.readFileSync(path, "utf8")));
```

## Automatically sort on commit

To let [`lint-staged`](https://github.com/lint-staged/lint-staged) take care of sorting the GitHub Actions files automatically use:

```json
{
".github/workflows/*.yml": ["better-sort-github-actions"]
}
```
22 changes: 22 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

"use strict";

const { readFileSync, writeFileSync } = require("node:fs");

const { sort } = require("..");

const files = process.argv.slice(2);
if (!files.length) {
console.error("Usage: better-sort-github-actions <file> [<file>...]");
process.exit(1);
}

try {
files.forEach(function (file) {
writeFileSync(file, sort(readFileSync(file, "utf8")));
});
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
Loading
Loading