Skip to content

Commit

Permalink
feat: add --no-cache flag to refresh command
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Apr 24, 2024
1 parent 4d6ce9c commit f943e55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ Open a repository in github.
```
USAGE
$ multi open REPO [-f <value> | -t actions|discussions|issues|pulls|pulse|security|settings|wiki]
$ multi open REPO [-f <value> | -t
actions|discussions|issues|pulls|pulse|security|settings|wiki]

ARGUMENTS
REPO [default: .] Name of repository.
Expand Down Expand Up @@ -340,15 +341,11 @@ Show all repositories in the org. Requires GH_TOKEN to be set in the environment
```
USAGE
$ multi org list ORGS... [--no-archived] [--no-private]
$ multi org list ORGS...

ARGUMENTS
ORGS... Github org

FLAGS
--no-archived Do not include archived repositories
--no-private Do not include private repositories

DESCRIPTION
Show all repositories in the org. Requires GH_TOKEN to be set in the environment.

Expand Down
9 changes: 9 additions & 0 deletions src/commands/refresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ type Props = {
readonly concurrency?: number
readonly dryRun: boolean
readonly header: string
readonly noCache?: boolean
readonly orgs: string[]
}

class RefreshTaskTracker extends TaskTracker<Props> {
async componentDidMount(): Promise<void> {
const repos = await new Repos().init()

if (this.props.noCache) {
await repos.hydrateCache()
}

const orgs = this.props.all ? repos.getOrgs() : this.props.orgs

const repositories = sortBy(
Expand Down Expand Up @@ -76,6 +81,9 @@ export class Refresh extends BaseCommand {
char: 'd',
description: 'Show what would be done without doing it.',
}),
'no-cache': Flags.boolean({
description: 'Find repos by looking at configured repos directory instead of using the cached repos.json file.',
}),
org: Flags.string({
char: 'o',
description: 'Github org to refresh.',
Expand All @@ -92,6 +100,7 @@ export class Refresh extends BaseCommand {
concurrency={flags.concurrency}
dryRun={flags['dry-run']}
header="Refreshing repositories"
noCache={flags['no-cache']}
orgs={[...new Set(flags.org ?? [])]}
/>,
)
Expand Down
28 changes: 27 additions & 1 deletion src/repos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Errors} from '@oclif/core'
import makeDebug from 'debug'
import {mkdir, rm} from 'node:fs/promises'
import {mkdir, readdir, rm} from 'node:fs/promises'
import path from 'node:path'

import {Aliases} from './aliases.js'
Expand Down Expand Up @@ -91,6 +91,32 @@ export class Repos extends ConfigFile<RepoIndex> {
return Object.values(this.getContents()).filter((r) => r.org === org && (includeArchived ? true : !r.archived))
}

public async hydrateCache(): Promise<void> {
const directory = this.config.get('directory')
const dirContents = await readdir(directory, {withFileTypes: true})
const orgs = dirContents.filter((d) => d.isDirectory()).map((d) => d.name)

const repos = await Promise.all(
orgs.map(async (org) => {
const orgDir = path.join(directory, org)
return (await readdir(orgDir, {withFileTypes: true}))
.filter((r) => r.isDirectory())
.map((r) => ({
fullName: `${org}/${r.name}`,
location: path.join(orgDir, r.name),
name: r.name,
org,
}))
}),
)

for (const orgRepos of repos) {
for (const repo of orgRepos) {
this.set(repo.fullName, repo as Repository)
}
}
}

public async init() {
await this.initSuper()
this.config = await new Config().init()
Expand Down

0 comments on commit f943e55

Please sign in to comment.