From d3399fe1734fb1e8e3419dd32b3b35bafebaca36 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 22 Dec 2024 20:19:26 +0900 Subject: [PATCH] migrate script to generate problem matcher from commonjs to ESM --- .github/workflows/matcher.yaml | 2 +- Makefile | 4 ++-- scripts/generate-actionlint-matcher/README.md | 4 ++-- .../{main.js => main.mjs} | 4 ++-- .../{object.js => object.mjs} | 2 +- .../{test.js => test.mjs} | 19 ++++++++++++------- 6 files changed, 20 insertions(+), 15 deletions(-) rename scripts/generate-actionlint-matcher/{main.js => main.mjs} (80%) rename scripts/generate-actionlint-matcher/{object.js => object.mjs} (97%) rename scripts/generate-actionlint-matcher/{test.js => test.mjs} (74%) diff --git a/.github/workflows/matcher.yaml b/.github/workflows/matcher.yaml index 7ba3b062c..8dc1295fd 100644 --- a/.github/workflows/matcher.yaml +++ b/.github/workflows/matcher.yaml @@ -26,7 +26,7 @@ jobs: - name: Update test data run: make ./scripts/generate-actionlint-matcher/test/* SKIP_GO_GENERATE=true - name: Test actionlint-matcher.json - run: node ./scripts/generate-actionlint-matcher/test.js + run: node ./scripts/generate-actionlint-matcher/test.mjs - name: Ensure .github/actionlint-matcher.json is up-to-date run: | make .github/actionlint-matcher.json diff --git a/Makefile b/Makefile index e77c8fe5c..90803a867 100644 --- a/Makefile +++ b/Makefile @@ -58,8 +58,8 @@ man: man/actionlint.1 bench: go test -bench Lint -benchmem -.github/actionlint-matcher.json: scripts/generate-actionlint-matcher/object.js - node ./scripts/generate-actionlint-matcher/main.js .github/actionlint-matcher.json +.github/actionlint-matcher.json: scripts/generate-actionlint-matcher/object.mjs + node ./scripts/generate-actionlint-matcher/main.mjs .github/actionlint-matcher.json scripts/generate-actionlint-matcher/test/escape.txt: actionlint ./actionlint -color ./testdata/err/one_error.yaml > ./scripts/generate-actionlint-matcher/test/escape.txt || true diff --git a/scripts/generate-actionlint-matcher/README.md b/scripts/generate-actionlint-matcher/README.md index 114815ea5..7e212f012 100644 --- a/scripts/generate-actionlint-matcher/README.md +++ b/scripts/generate-actionlint-matcher/README.md @@ -12,13 +12,13 @@ make .github/actionlint-matcher.json or directly run the script ```sh -node ./scripts/generate-actionlint-matcher/main.js .github/actionlint-matcher.json +node ./scripts/generate-actionlint-matcher/main.mjs .github/actionlint-matcher.json ``` ## Test ```sh -node ./scripts/generate-actionlint-matcher/test.js +node ./scripts/generate-actionlint-matcher/test.mjs ``` The test uses test data at `./scripts/generate-actionlint-matcher/test/*.txt`. They should be updated when actionlint changes diff --git a/scripts/generate-actionlint-matcher/main.js b/scripts/generate-actionlint-matcher/main.mjs similarity index 80% rename from scripts/generate-actionlint-matcher/main.js rename to scripts/generate-actionlint-matcher/main.mjs index 6b51a8f7b..ab268623c 100644 --- a/scripts/generate-actionlint-matcher/main.js +++ b/scripts/generate-actionlint-matcher/main.mjs @@ -1,5 +1,5 @@ -const fs = require('fs').promises; -const object = require('./object.js'); +import { promises as fs } from 'node:fs'; +import object from './object.mjs'; async function main(args) { const json = JSON.stringify(object, null, 2); diff --git a/scripts/generate-actionlint-matcher/object.js b/scripts/generate-actionlint-matcher/object.mjs similarity index 97% rename from scripts/generate-actionlint-matcher/object.js rename to scripts/generate-actionlint-matcher/object.mjs index 2f4af80f9..fd48ba0fc 100644 --- a/scripts/generate-actionlint-matcher/object.js +++ b/scripts/generate-actionlint-matcher/object.mjs @@ -31,4 +31,4 @@ const object = { ], }; -module.exports = object; +export default object; diff --git a/scripts/generate-actionlint-matcher/test.js b/scripts/generate-actionlint-matcher/test.mjs similarity index 74% rename from scripts/generate-actionlint-matcher/test.js rename to scripts/generate-actionlint-matcher/test.mjs index e2d2a50a0..e2076d72d 100644 --- a/scripts/generate-actionlint-matcher/test.js +++ b/scripts/generate-actionlint-matcher/test.mjs @@ -1,15 +1,20 @@ -const path = require('path'); -const fs = require('fs'); -const assert = require('assert').strict; -const pattern = require('./object.js').problemMatcher[0].pattern[0]; +import path from 'node:path'; +import fs from 'node:fs'; +import assert from 'node:assert'; +import { fileURLToPath } from 'node:url'; +import object from './object.mjs'; + +const pattern = object.problemMatcher[0].pattern[0]; const regexp = new RegExp(pattern.regexp); +const dirname = path.dirname(fileURLToPath(import.meta.url)); { - const want = require('./test/want.json')[0]; + const file = path.join(dirname, 'test', 'want.json'); + const want = JSON.parse(fs.readFileSync(file, 'utf8'))[0]; for (const file of ['escape.txt', 'no_escape.txt']) { console.log(`Testing test/${file}`); - const escaped = path.join(__dirname, 'test', file); + const escaped = path.join(dirname, 'test', file); const lines = fs.readFileSync(escaped, 'utf8').split('\n'); const m = lines[0].match(regexp); assert.ok(m); // not null @@ -23,7 +28,7 @@ const regexp = new RegExp(pattern.regexp); } for (const parent of ['examples', 'err']) { - const dir = path.join(__dirname, '..', '..', 'testdata', parent); + const dir = path.join(dirname, '..', '..', 'testdata', parent); for (const name of fs.readdirSync(dir)) { if (!name.endsWith('.out')) { continue;