|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import pathlib from 'path'; |
| 3 | +import * as core from '@actions/core'; |
| 4 | +import { getExecOutput } from '@actions/exec'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Recursively locates the packages present in a directory, up to an optional max depth |
| 8 | + */ |
| 9 | +async function findPackages(directory: string, maxDepth?: number) { |
| 10 | + async function* recurser(currentDir: string, currentDepth: number): AsyncGenerator<string> { |
| 11 | + if (maxDepth !== undefined && currentDepth >= maxDepth) return; |
| 12 | + |
| 13 | + const items = await fs.readdir(currentDir, { withFileTypes: true }); |
| 14 | + for (const item of items) { |
| 15 | + const fullPath = pathlib.join(currentDir, item.name); |
| 16 | + if (item.isFile()) { |
| 17 | + if (item.name === 'package.json') { |
| 18 | + yield fullPath; |
| 19 | + } |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + if (item.isDirectory() && item.name !== 'node_modules') { |
| 24 | + yield* recurser(fullPath, currentDepth + 1); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return Array.fromAsync(recurser(directory, 0)); |
| 30 | +} |
| 31 | + |
| 32 | +async function main() { |
| 33 | + const { stdout } = await getExecOutput('git', ['rev-parse', '--show-toplevel']); |
| 34 | + const gitRoot = stdout.trim(); |
| 35 | + |
| 36 | + const packageType = core.getInput('type', { required: true }); |
| 37 | + |
| 38 | + let dirPath: string; |
| 39 | + switch (packageType) { |
| 40 | + case 'lib': { |
| 41 | + dirPath = pathlib.join(gitRoot, 'lib'); |
| 42 | + break; |
| 43 | + } |
| 44 | + case 'bundles': { |
| 45 | + dirPath = pathlib.join(gitRoot, 'src', 'bundles'); |
| 46 | + break; |
| 47 | + } |
| 48 | + case 'tabs': { |
| 49 | + dirPath = pathlib.join(gitRoot, 'src', 'tabs'); |
| 50 | + break; |
| 51 | + } |
| 52 | + default: { |
| 53 | + core.error(`Invalid package type: ${packageType}. Must be lib, bundles or tabs`); |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + const packages = await findPackages(dirPath); |
| 59 | + core.setOutput('packages', packages); |
| 60 | +} |
| 61 | + |
| 62 | +try { |
| 63 | + await main(); |
| 64 | +} catch (error: any) { |
| 65 | + core.setFailed(error.message); |
| 66 | +} |
0 commit comments