Skip to content

Commit 21f4560

Browse files
committed
Updated formatting configuration
1 parent ea8e01f commit 21f4560

File tree

4 files changed

+138
-126
lines changed

4 files changed

+138
-126
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v5
13-
- run: npm install
13+
- run: npm clean-install
1414
- name: Check formatting with js-beautify
1515
run: |
16-
npx js-beautify -r -n "**/*.js"
17-
npx js-beautify -r -n "package.json"
16+
npm run format
1817
git diff --exit-code
1918
- run: npm run build
2019
- name: Run action with inclusive upper bound

.jsbeautifyrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"end_with_newline" : true,
3+
"indent_size": 2,
4+
"indent_char": " ",
5+
"max_preserve_newlines": 2,
6+
"preserve_newlines": true,
7+
"space_in_paren": false,
8+
"space_in_empty_paren": false,
9+
"js": {
10+
"indent_size": 2
11+
}
12+
}

package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
2-
"name": "get-supported-ghc",
3-
"version": "0.0.9",
4-
"main": "index.js",
5-
"scripts": {
6-
"build": "ncc build src/index.js -o ."
7-
},
8-
"dependencies": {
9-
"@actions/core": "^1.10.0",
10-
"js-yaml": "^4.1.0"
11-
},
12-
"devDependencies": {
13-
"@vercel/ncc": "^0.38.4",
14-
"js-beautify": "^1.15.4"
15-
}
2+
"name": "get-supported-ghc",
3+
"version": "0.0.9",
4+
"main": "index.js",
5+
"scripts": {
6+
"build": "ncc build src/index.js -o .",
7+
"format": "js-beautify -r '**/*.js' 'package.json'"
8+
},
9+
"dependencies": {
10+
"@actions/core": "^1.10.0",
11+
"js-yaml": "^4.1.0"
12+
},
13+
"devDependencies": {
14+
"@vercel/ncc": "^0.38.4",
15+
"js-beautify": "^1.15.4"
16+
}
1617
}

src/index.js

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,148 @@
11
const {
2-
exec
2+
exec
33
} = require("child_process");
44
const fs = require("fs");
55
const path = require("path");
66
const yaml = require("js-yaml");
77
const githubCore = require("@actions/core");
88

99
async function runCommand(cmd) {
10-
return new Promise((resolve, reject) => {
11-
exec(cmd, (error, stdout, stderr) => {
12-
if (error) reject(error);
13-
else resolve(stdout.trim());
14-
});
10+
return new Promise((resolve, reject) => {
11+
exec(cmd, (error, stdout, stderr) => {
12+
if (error) reject(error);
13+
else resolve(stdout.trim());
1514
});
15+
});
1616
}
1717

1818
function getBaseUpperBound(baseBound) {
19-
const baseBoundMatch = baseBound.match(/(<=|<)\s*((\d+)(\.(\d+))?(\.(\d+))?)/);
20-
if (!baseBoundMatch) return null;
19+
const baseBoundMatch = baseBound.match(/(<=|<)\s*((\d+)(\.(\d+))?(\.(\d+))?)/);
20+
if (!baseBoundMatch) return null;
2121

22-
const operator = baseBoundMatch[1];
23-
const version = baseBoundMatch[2];
24-
const inclusive = operator === "<=";
22+
const operator = baseBoundMatch[1];
23+
const version = baseBoundMatch[2];
24+
const inclusive = operator === "<=";
2525

26-
return {
27-
inclusive,
28-
version
29-
};
26+
return {
27+
inclusive,
28+
version
29+
};
3030
}
3131

3232
function normalizeVersion(version, segments = 3) {
33-
const parts = version.split('.').map(Number);
34-
while (parts.length < segments) parts.push(0);
35-
return parts.slice(0, segments).join('.');
33+
const parts = version.split('.').map(Number);
34+
while (parts.length < segments) parts.push(0);
35+
return parts.slice(0, segments).join('.');
3636
}
3737

3838
function compareVersions(a, b) {
39-
const pa = normalizeVersion(a).split('.').map(Number);
40-
const pb = normalizeVersion(b).split('.').map(Number);
41-
const len = Math.max(pa.length, pb.length);
42-
43-
for (let i = 0; i < len; i++) {
44-
const na = pa[i] || 0;
45-
const nb = pb[i] || 0;
46-
if (na > nb) return 1;
47-
if (na < nb) return -1;
48-
}
49-
return 0;
39+
const pa = normalizeVersion(a).split('.').map(Number);
40+
const pb = normalizeVersion(b).split('.').map(Number);
41+
const len = Math.max(pa.length, pb.length);
42+
43+
for (let i = 0; i < len; i++) {
44+
const na = pa[i] || 0;
45+
const nb = pb[i] || 0;
46+
if (na > nb) return 1;
47+
if (na < nb) return -1;
48+
}
49+
return 0;
5050
}
5151

5252
function versionLess(baseVersion, bound) {
53-
const cmp = compareVersions(baseVersion, bound.version);
54-
return cmp < 0 || (cmp === 0 && bound.inclusive);
53+
const cmp = compareVersions(baseVersion, bound.version);
54+
return cmp < 0 || (cmp === 0 && bound.inclusive);
5555
}
5656

5757
function parseBaseUpperBound(packageYamlPath) {
58-
const fileContent = fs.readFileSync(packageYamlPath, "utf-8");
59-
const parsed = yaml.load(fileContent);
58+
const fileContent = fs.readFileSync(packageYamlPath, "utf-8");
59+
const parsed = yaml.load(fileContent);
60+
61+
const deps = parsed.dependencies;
62+
if (!deps || !Array.isArray(deps)) {
63+
throw new Error("dependencies not found or invalid in package.yaml");
64+
}
65+
66+
const baseDep = deps.find(dep => {
67+
if (typeof dep === "string") {
68+
return dep.startsWith("base");
69+
} else if (typeof dep === "object" && dep.name) {
70+
return dep.name === "base";
71+
}
72+
return false;
73+
});
74+
75+
if (!baseDep) {
76+
githubCore.setFailed("No base dependency found in package.yaml");
77+
return;
78+
}
79+
80+
let versionConstraint;
81+
if (typeof baseDep === "string") {
82+
versionConstraint = getBaseUpperBound(baseDep);
83+
} else if (typeof baseDep === "object") {
84+
versionConstraint = getBaseUpperBound(baseDep.version);
85+
}
86+
87+
if (!versionConstraint) {
88+
githubCore.setFailed("No upper bound for base found in package.yaml");
89+
return;
90+
}
91+
92+
return versionConstraint;
93+
}
6094

61-
const deps = parsed.dependencies;
62-
if (!deps || !Array.isArray(deps)) {
63-
throw new Error("dependencies not found or invalid in package.yaml");
95+
async function main() {
96+
try {
97+
const packageYamlPath = githubCore.getInput("package-yaml-path") || path.join(process.cwd(), "package.yaml");
98+
const baseUpperBound = parseBaseUpperBound(packageYamlPath);
99+
100+
const ghcupListStr = await runCommand("ghcup list -t ghc -r");
101+
const lines = ghcupListStr.split("\n").filter(Boolean);
102+
103+
const ghcupList = lines.map(line => {
104+
const match = line.match(/^ghc\s([^\s]+)\s.*?base-([0-9.]+)/);
105+
if (!match) return null;
106+
return {
107+
version: match[1],
108+
base: match[2]
109+
};
110+
}).filter(Boolean);
111+
112+
if (ghcupList.length > 0) {
113+
githubCore.info(`Found ${ghcupList.length} GHC versions`);
114+
} else {
115+
githubCore.setFailed('Failed to get GHC versions from GHCup');
64116
}
65117

66-
const baseDep = deps.find(dep => {
67-
if (typeof dep === "string") {
68-
return dep.startsWith("base");
69-
} else if (typeof dep === "object" && dep.name) {
70-
return dep.name === "base";
71-
}
72-
return false;
118+
const validVersions = ghcupList.filter(ghcEntry => {
119+
return versionLess(ghcEntry.base, baseUpperBound);
73120
});
74121

75-
if (!baseDep) {
76-
githubCore.setFailed("No base dependency found in package.yaml");
77-
return;
122+
if (validVersions.length === 0) {
123+
throw new Error(`No GHC version found with base <${baseUpperBound.inclusive ? "=" : ""} ${baseUpperBound.version}`);
78124
}
79125

80-
let versionConstraint;
81-
if (typeof baseDep === "string") {
82-
versionConstraint = getBaseUpperBound(baseDep);
83-
} else if (typeof baseDep === "object") {
84-
versionConstraint = getBaseUpperBound(baseDep.version);
85-
}
126+
validVersions.sort((a, b) => {
127+
const aVer = a.version.split('.').map(Number);
128+
const bVer = b.version.split('.').map(Number);
129+
for (let i = 0; i < Math.max(aVer.length, bVer.length); i++) {
130+
const n1 = aVer[i] || 0;
131+
const n2 = bVer[i] || 0;
132+
if (n1 > n2) return -1;
133+
if (n1 < n2) return 1;
134+
}
135+
return 0;
136+
});
86137

87-
if (!versionConstraint) {
88-
githubCore.setFailed("No upper bound for base found in package.yaml");
89-
return;
90-
}
138+
const latestGhc = validVersions[0].version;
91139

92-
return versionConstraint;
93-
}
140+
githubCore.info(`Latest GHC under base < ${baseUpperBound.version}: ${latestGhc}`);
94141

95-
async function main() {
96-
try {
97-
const packageYamlPath = githubCore.getInput("package-yaml-path") || path.join(process.cwd(), "package.yaml");
98-
const baseUpperBound = parseBaseUpperBound(packageYamlPath);
99-
100-
const ghcupListStr = await runCommand("ghcup list -t ghc -r");
101-
const lines = ghcupListStr.split("\n").filter(Boolean);
102-
103-
const ghcupList = lines.map(line => {
104-
const match = line.match(/^ghc\s([^\s]+)\s.*?base-([0-9.]+)/);
105-
if (!match) return null;
106-
return {
107-
version: match[1],
108-
base: match[2]
109-
};
110-
}).filter(Boolean);
111-
112-
if (ghcupList.length > 0) {
113-
githubCore.info(`Found ${ghcupList.length} GHC versions`);
114-
} else {
115-
githubCore.setFailed('Failed to get GHC versions from GHCup');
116-
}
117-
118-
const validVersions = ghcupList.filter(ghcEntry => {
119-
return versionLess(ghcEntry.base, baseUpperBound);
120-
});
121-
122-
if (validVersions.length === 0) {
123-
throw new Error(`No GHC version found with base <${baseUpperBound.inclusive ? "=" : ""} ${baseUpperBound.version}`);
124-
}
125-
126-
validVersions.sort((a, b) => {
127-
const aVer = a.version.split('.').map(Number);
128-
const bVer = b.version.split('.').map(Number);
129-
for (let i = 0; i < Math.max(aVer.length, bVer.length); i++) {
130-
const n1 = aVer[i] || 0;
131-
const n2 = bVer[i] || 0;
132-
if (n1 > n2) return -1;
133-
if (n1 < n2) return 1;
134-
}
135-
return 0;
136-
});
137-
138-
const latestGhc = validVersions[0].version;
139-
140-
githubCore.info(`Latest GHC under base < ${baseUpperBound.version}: ${latestGhc}`);
141-
142-
githubCore.setOutput('ghc-version', latestGhc);
143-
} catch (err) {
144-
githubCore.setFailed(err.message);
145-
}
142+
githubCore.setOutput('ghc-version', latestGhc);
143+
} catch (err) {
144+
githubCore.setFailed(err.message);
145+
}
146146
}
147147

148148
main();

0 commit comments

Comments
 (0)