From b072f4915c7e417f9c06b3981e644024503ca120 Mon Sep 17 00:00:00 2001 From: AnnAngela Date: Thu, 18 Jan 2024 16:13:36 +0800 Subject: [PATCH] ci: Add postcss test script --- .github/workflows/postCommit.yaml | 2 + scripts/postcss/index.js | 61 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 scripts/postcss/index.js diff --git a/.github/workflows/postCommit.yaml b/.github/workflows/postCommit.yaml index 27684e6f..62b2ee13 100644 --- a/.github/workflows/postCommit.yaml +++ b/.github/workflows/postCommit.yaml @@ -164,6 +164,8 @@ jobs: run: npx stylelint --cache --cache-strategy content --cache-location ".cache/" --formatter github --max-warnings 0 "src/**/*.css" - name: Run v8r run: npx v8r + - name: Run postcss test + run: node scripts/postcss/index.js - name: Check .mailmap if: needs.postCommit.result == 'success' run: node scripts/emailmapChecker/index.js diff --git a/scripts/postcss/index.js b/scripts/postcss/index.js new file mode 100644 index 00000000..2a7a7a23 --- /dev/null +++ b/scripts/postcss/index.js @@ -0,0 +1,61 @@ +import { exec } from "node:child_process"; +import { warning, startGroup, endGroup } from "@actions/core"; +import console from "../modules/console.js"; + +console.info("Start to run postcss..."); +startGroup("postcss output:"); +/** + * @type { string } + */ +const output = await new Promise((res, rej) => { + const childProcess = exec("npx postcss src/**/*.css --base src/ -d dist/ --verbose", (err, _, stderr) => { + if (err) { + rej(err); + } else { + res(stderr); + } + }); + childProcess.stdout?.pipe(process.stdout); + childProcess.stderr?.pipe(process.stderr); +}); +endGroup(); + +/** + * @type { [string, import("@actions/core").AnnotationProperties][] } + */ +const annotations = []; +/** + * @type { string } + */ +let lastFileName; +for (const line of output.split("\n")) { + if (line.startsWith("Finished")) { + const match = line.match(/Finished (.*?) in/)?.[1]; + if (match) { + lastFileName = match; + } + } + if (line.includes("⚠")) { + /** + * @type { import("@actions/core").AnnotationProperties } + */ + const annotationProperties = { + title: "PostCSS Annotation", + file: lastFileName, + startLine: +line.match(/^(\d+):(\d+)/)?.[1], + startColumn: +line.match(/^(\d+):(\d+)/)?.[2], + }; + const fileLink = `https://github.com/${process.env.GITHUB_REPOSITORY}/blob/${process.env.GITHUB_SHA.slice(0, 7)}/${encodeURI(`${lastFileName}#L${annotationProperties.startLine}`)}`; + const msg = `${line.replace(/^.*?⚠\s*/, "")} @ ${fileLink}`; + annotations.push([msg, annotationProperties]); + } +} + +if (annotations.length > 0) { + console.warn("PostCSS has some warnings:"); + for (const [msg, annotationProperties] of annotations) { + warning(msg, annotationProperties); + } +} else { + console.info("PostCSS has no warnings."); +}