|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +import commandLineArgs from "command-line-args"; |
| 4 | +import fs from "fs"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | +import path from "path"; |
| 7 | + |
| 8 | +import { logError, logCommand, printHelp, runTest, sh, logInfo } from "./helper.mjs"; |
| 9 | + |
| 10 | +const FILE_PATH = fileURLToPath(import.meta.url); |
| 11 | +const SRC_DIR = path.dirname(path.dirname(FILE_PATH)); |
| 12 | + |
| 13 | +const optionDefinitions = [ |
| 14 | + { name: "help", alias: "h", description: "Print this help text." }, |
| 15 | + { name: "diff", type: String, description: "A git commit range to determine which builds to run (e.g. main...HEAD)." }, |
| 16 | +]; |
| 17 | + |
| 18 | +const options = commandLineArgs(optionDefinitions); |
| 19 | + |
| 20 | +if ("help" in options) |
| 21 | + printHelp(optionDefinitions); |
| 22 | + |
| 23 | + |
| 24 | +if (options.diff) { |
| 25 | + const { stdoutString } = await sh("git", "diff", "--name-only", options.diff); |
| 26 | + const changedDirs = new Set(); |
| 27 | + const changedFiles = stdoutString.trim().split("\n"); |
| 28 | + for (const file of changedFiles) { |
| 29 | + let currentDir = path.dirname(file) |
| 30 | + while (currentDir !== ".") { |
| 31 | + changedDirs.add(path.join(SRC_DIR, currentDir)); |
| 32 | + currentDir = path.dirname(currentDir); |
| 33 | + } |
| 34 | + } |
| 35 | + options.changedDirs = changedDirs; |
| 36 | +} else { |
| 37 | + options.changedDirs = undefined; |
| 38 | +} |
| 39 | + |
| 40 | +async function findPackageJsonFiles(dir, accumulator=[]) { |
| 41 | + const dirEntries = fs.readdirSync(dir, { withFileTypes: true }); |
| 42 | + for (const dirent of dirEntries) { |
| 43 | + if (dirent.name === "node_modules" || dirent.name === ".git") |
| 44 | + continue; |
| 45 | + const fullPath = path.join(dir, dirent.name); |
| 46 | + if (dirent.isDirectory()) |
| 47 | + findPackageJsonFiles(fullPath, accumulator); |
| 48 | + else if (dirent.name === "package.json") |
| 49 | + accumulator.push(fullPath) |
| 50 | + } |
| 51 | + return accumulator; |
| 52 | +} |
| 53 | + |
| 54 | +async function runBuilds() { |
| 55 | + const packageJsonFiles = await findPackageJsonFiles(SRC_DIR); |
| 56 | + let success = true; |
| 57 | + |
| 58 | + logInfo(`Found ${packageJsonFiles.length} package.json files`); |
| 59 | + let filteredPackageJsonFiles = packageJsonFiles; |
| 60 | + if (options.changedDirs?.size === 0) { |
| 61 | + logInfo("No file changes detected, skipping all"); |
| 62 | + } |
| 63 | + if (options.changedDirs?.size > 0) { |
| 64 | + filteredPackageJsonFiles = packageJsonFiles.filter(file => { |
| 65 | + const dir = path.dirname(file); |
| 66 | + return options.changedDirs.has(dir); |
| 67 | + }); |
| 68 | + logInfo(`Found ${filteredPackageJsonFiles.length} modified package.json files to build`); |
| 69 | + } |
| 70 | + |
| 71 | + for (const file of filteredPackageJsonFiles) { |
| 72 | + const content = fs.readFileSync(file, "utf-8"); |
| 73 | + const packageJson = JSON.parse(content); |
| 74 | + if (!packageJson.scripts?.build) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + const dir = path.dirname(file); |
| 79 | + const relativeDir = path.relative(SRC_DIR, dir); |
| 80 | + const testName = `Building ./${relativeDir}:`; |
| 81 | + |
| 82 | + const buildTask = async () => { |
| 83 | + const oldCWD = process.cwd(); |
| 84 | + try { |
| 85 | + logCommand("cd", dir); |
| 86 | + process.chdir(dir); |
| 87 | + await sh("npm", "ci"); |
| 88 | + await sh("npm", "run", "build"); |
| 89 | + } finally { |
| 90 | + process.chdir(oldCWD); |
| 91 | + await sh("git", "reset", "--hard"); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + success &&= await runTest(testName, buildTask); |
| 96 | + } |
| 97 | + |
| 98 | + if (!success) { |
| 99 | + logError("One or more builds failed."); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +setImmediate(runBuilds); |
0 commit comments