Skip to content

Commit

Permalink
feat(core): support list command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 25, 2023
1 parent 80f0903 commit 7d64f84
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { manager, spawnAsync } from './utils'
import { red } from 'kleur'
import { promises as fsp, readFileSync } from 'fs'
import { Dict, makeArray } from 'cosmokit'
import list from './plugins/list'
import prepare from './plugins/prepare'
import publish from './plugins/publish'
import test from './plugins/test'
Expand Down Expand Up @@ -45,9 +46,7 @@ export interface Arguments extends yargs.Arguments {
_: string[]
}

export interface Options extends yargs.Options {
manual?: boolean
}
export interface Options extends yargs.Options {}

export type DependencyType = 'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies'

Expand Down Expand Up @@ -105,6 +104,7 @@ export default class Yakumo {
this.cwd = cwd
this.manager = manager

ctx.plugin(list)
ctx.plugin(prepare)
ctx.plugin(publish)
ctx.plugin(test)
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/plugins/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Dict } from 'cosmokit'
import { Context } from '..'

interface Node {
name: string
path: string
children: Node[]
tree: Dict<Node>
}

export default function apply(ctx: Context) {
function createNode(path: string): Node {
return { name: ctx.yakumo.workspaces[path].name, path, children: [], tree: {} }
}

function findParent(root: Node, path: string) {
for (const prefix in root.tree) {
if (path.startsWith(prefix)) return findParent(root.tree[prefix], path)
}
return root
}

function printNode(node: Node, indent: boolean[] = []) {
const prefix = indent.map((isLastItem, index) => {
const isLastIndent = index === indent.length - 1
return isLastIndent
? isLastItem ? '└── ' : '├── '
: isLastItem ? ' ' : '│ '
}).join('')
console.log(`${prefix}${node.name}${node.path ? ` (${node.path.slice(1)})` : ''}`)
node.children.forEach((child, index) => {
const isLast = index === node.children.length - 1
printNode(child, [...indent, isLast])
})
}

ctx.register('list', async () => {
const paths = Object.keys(ctx.yakumo.workspaces).sort()
const root: Node = createNode(paths.shift()!)
const total = paths.length
let workspaces = 1
for (const path of paths) {
const node = createNode(path)
const parent = findParent(root, path)
parent.children.push(node)
if (ctx.yakumo.workspaces[path].workspaces) {
parent.tree[path] = node
workspaces++
}
}
printNode(root)
console.log(`${total} packages, ${workspaces} workspaces`)
})
}
1 change: 0 additions & 1 deletion packages/core/src/plugins/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default function apply(ctx: Context) {
await ctx.yakumo.install()
}
}, {
manual: true,
alias: {
clean: ['c'],
},
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/plugins/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,5 @@ export default function apply(ctx: Context) {
process.on('SIGINT', () => {
child.kill('SIGINT')
})
}, {
manual: true,
})
}
2 changes: 0 additions & 2 deletions packages/core/src/plugins/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,5 @@ export default function apply(ctx: Context, config: Config = {}) {
}
}
}
}, {
manual: true,
})
}

0 comments on commit 7d64f84

Please sign in to comment.