|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | + |
| 3 | +import * as fs from "node:fs"; |
| 4 | +import * as path from "node:path"; |
| 5 | + |
| 6 | +import * as core from "@actions/core"; |
| 7 | + |
| 8 | +import { runCommand, runGit } from "./command"; |
| 9 | +import { LIB_ROOT, PR_CHECKS_DIR, REPO_ROOT } from "./config"; |
| 10 | +import { getErrorMessage } from "./util"; |
| 11 | + |
| 12 | +function main() { |
| 13 | + // Sanity check that repo is clean to start with |
| 14 | + try { |
| 15 | + runGit(["diff", "--exit-code"], { allowNonZeroExitCode: false }); |
| 16 | + console.info("Repository is clean."); |
| 17 | + } catch (err) { |
| 18 | + // If we get a fail here then this workflow needs attention... |
| 19 | + console.error(getErrorMessage(err)); |
| 20 | + console.error("Failed: Repo should be clean before testing!"); |
| 21 | + return -1; |
| 22 | + } |
| 23 | + |
| 24 | + // Wipe the lib directory in case there are extra unnecessary files in there |
| 25 | + console.info(`Removing ${LIB_ROOT}...`); |
| 26 | + fs.rmSync(LIB_ROOT, { recursive: true, force: true }); |
| 27 | + |
| 28 | + // Generate the JavaScript files |
| 29 | + runCommand("npm", ["run", "build"], { execOptions: { cwd: REPO_ROOT } }); |
| 30 | + |
| 31 | + // Check that repo is still clean |
| 32 | + try { |
| 33 | + runGit(["diff", "--exit-code"], { allowNonZeroExitCode: false }); |
| 34 | + console.info("Repository is clean."); |
| 35 | + } catch (err) { |
| 36 | + // If we get a fail here then the PR needs attention |
| 37 | + console.error(getErrorMessage(err)); |
| 38 | + console.error("Failed: JavaScript files are not up to date."); |
| 39 | + console.error("Run 'rm -rf lib && npm run build' to update."); |
| 40 | + |
| 41 | + const diffFile = path.join( |
| 42 | + process.env["RUNNER_TEMP"] ?? PR_CHECKS_DIR, |
| 43 | + "js.diff", |
| 44 | + ); |
| 45 | + runCommand("git", ["status"], { execOptions: { cwd: REPO_ROOT } }); |
| 46 | + runCommand("git", ["diff", `--output=${diffFile}`], { |
| 47 | + execOptions: { cwd: REPO_ROOT }, |
| 48 | + }); |
| 49 | + |
| 50 | + core.summary.addHeading("Transpiled JS diff", 3); |
| 51 | + core.summary.addCodeBlock(fs.readFileSync(diffFile, "utf-8"), "diff"); |
| 52 | + |
| 53 | + fs.rmSync(diffFile); |
| 54 | + |
| 55 | + // Reset bundled files to allow other checks to test for changes |
| 56 | + runCommand("git", ["checkout", "lib"], { execOptions: { cwd: REPO_ROOT } }); |
| 57 | + |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + console.info("Success: JavaScript files are up to date"); |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +if (require.main === module) { |
| 66 | + process.exit(main()); |
| 67 | +} |
0 commit comments