|
| 1 | +// src/info/index.ts |
| 2 | +import fs from "fs/promises"; |
| 3 | +import pathlib from "path"; |
| 4 | +import * as core from "@actions/core"; |
| 5 | +import { getExecOutput } from "@actions/exec"; |
| 6 | +async function findPackages(directory, maxDepth) { |
| 7 | + const output = []; |
| 8 | + async function* recurser(currentDir, currentDepth) { |
| 9 | + if (maxDepth !== void 0 && currentDepth >= maxDepth) return; |
| 10 | + const items = await fs.readdir(currentDir, { withFileTypes: true }); |
| 11 | + for (const item of items) { |
| 12 | + const fullPath = pathlib.join(currentDir, item.name); |
| 13 | + if (item.isFile()) { |
| 14 | + if (item.name === "package.json") { |
| 15 | + try { |
| 16 | + const { default: { name } } = await import(fullPath, { with: { type: "json" } }); |
| 17 | + if (name) { |
| 18 | + yield { name, directory: currentDir }; |
| 19 | + return; |
| 20 | + } |
| 21 | + ; |
| 22 | + } catch { |
| 23 | + } |
| 24 | + } |
| 25 | + continue; |
| 26 | + } |
| 27 | + if (item.isDirectory() && item.name !== "node_modules" && !item.name.startsWith("__")) { |
| 28 | + yield* recurser(fullPath, currentDepth + 1); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + for await (const each of recurser(directory, 0)) { |
| 33 | + output.push(each); |
| 34 | + } |
| 35 | + return output; |
| 36 | +} |
| 37 | +async function main() { |
| 38 | + const { stdout } = await getExecOutput("git", ["rev-parse", "--show-toplevel"]); |
| 39 | + const gitRoot = stdout.trim(); |
| 40 | + const packageType = core.getInput("type", { required: true }); |
| 41 | + let dirPath; |
| 42 | + switch (packageType) { |
| 43 | + case "libs": { |
| 44 | + dirPath = pathlib.join(gitRoot, "lib"); |
| 45 | + break; |
| 46 | + } |
| 47 | + case "bundles": { |
| 48 | + dirPath = pathlib.join(gitRoot, "src", "bundles"); |
| 49 | + break; |
| 50 | + } |
| 51 | + case "tabs": { |
| 52 | + dirPath = pathlib.join(gitRoot, "src", "tabs"); |
| 53 | + break; |
| 54 | + } |
| 55 | + default: { |
| 56 | + core.error(`Invalid package type: ${packageType}. Must be lib, bundles or tabs`); |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + const packages = await findPackages(dirPath); |
| 61 | + core.setOutput(packageType, packages); |
| 62 | + core.summary.addHeading(`Found ${packageType}`); |
| 63 | + core.summary.addList(packages.map(({ name }) => name)); |
| 64 | + core.summary.write(); |
| 65 | +} |
| 66 | +try { |
| 67 | + await main(); |
| 68 | +} catch (error2) { |
| 69 | + core.setFailed(error2.message); |
| 70 | +} |
0 commit comments