|
| 1 | +import fs from "node:fs" |
| 2 | +import path from "node:path" |
| 3 | +import process from "node:process" |
| 4 | +import { findUp } from "find-up" |
| 5 | +import yaml from "js-yaml" |
1 | 6 | import type { CommandModule } from "yargs" |
2 | | -import { createGitHubService } from "../../github" |
3 | | -import { getGroupedRepos } from "../../github/util" |
4 | | -import { createCacheProvider, createConfig, createReporter } from "../util" |
| 7 | +import { DefinitionFile } from "../../definition" |
| 8 | +import { createReporter } from "../util" |
| 9 | + |
| 10 | +const CALS_YAML = ".cals.yaml" |
| 11 | + |
| 12 | +interface CalsManifest { |
| 13 | + version: 2 |
| 14 | + githubOrganization: string |
| 15 | + resourcesDefinition: { |
| 16 | + path: string |
| 17 | + } |
| 18 | +} |
5 | 19 |
|
6 | 20 | const command: CommandModule = { |
7 | 21 | command: "groups", |
8 | | - describe: "List available repository groups in a GitHub organization", |
9 | | - builder: (yargs) => |
10 | | - yargs.options("org", { |
11 | | - alias: "o", |
12 | | - default: "capralifecycle", |
13 | | - requiresArg: true, |
14 | | - describe: "GitHub organization", |
15 | | - type: "string", |
16 | | - }), |
17 | | - handler: async (argv) => { |
18 | | - const config = createConfig() |
| 22 | + describe: "List available project groups from the definition file", |
| 23 | + builder: (yargs) => yargs, |
| 24 | + handler: async () => { |
19 | 25 | const reporter = createReporter() |
20 | | - const github = await createGitHubService({ |
21 | | - cache: createCacheProvider(config, argv), |
22 | | - }) |
23 | 26 |
|
24 | | - const repos = await github.getOrgRepoList({ org: argv.org as string }) |
25 | | - const groups = getGroupedRepos(repos) |
| 27 | + const manifestPath = await findUp(CALS_YAML) |
| 28 | + if (manifestPath === undefined) { |
| 29 | + reporter.error(`File ${CALS_YAML} not found`) |
| 30 | + process.exitCode = 1 |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + const manifest: CalsManifest = yaml.load( |
| 35 | + fs.readFileSync(manifestPath, "utf-8"), |
| 36 | + ) as CalsManifest |
| 37 | + |
| 38 | + const definitionPath = path.resolve( |
| 39 | + path.dirname(manifestPath), |
| 40 | + manifest.resourcesDefinition.path, |
| 41 | + ) |
| 42 | + |
| 43 | + if (!fs.existsSync(definitionPath)) { |
| 44 | + reporter.error(`Definition file not found: ${definitionPath}`) |
| 45 | + process.exitCode = 1 |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + const definition = await new DefinitionFile(definitionPath).getDefinition() |
| 50 | + const projectNames = definition.projects |
| 51 | + .map((p) => p.name) |
| 52 | + .sort((a, b) => a.localeCompare(b)) |
26 | 53 |
|
27 | | - for (const group of groups) { |
28 | | - reporter.log(group.name) |
| 54 | + for (const name of projectNames) { |
| 55 | + reporter.log(name) |
29 | 56 | } |
30 | 57 | }, |
31 | 58 | } |
|
0 commit comments