Skip to content

Commit

Permalink
feat(core): support *-suffixed alias
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 13, 2024
1 parent 80ef61d commit 5bd357d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
"@types/yargs-parser": "^21.0.3"
},
"peerDependencies": {
"cordis": "^3.14.0"
"cordis": "^3.17.9"
},
"dependencies": {
"cordis": "^3.14.0",
"cordis": "^3.17.9",
"cosmokit": "^1.6.2",
"detect-indent": "^6.1.0",
"execa": "^5.1.1",
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
}

resolveIntercept(): Yakumo.Intercept {
const caller = this[Context.current]
let result = this.config
let intercept = caller[Context.intercept]
let intercept = this.ctx[Context.intercept]
while (intercept) {
result = {
...result,
Expand All @@ -168,7 +167,7 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
}

locate(name: string | string[], options: LocateOptions = {}): string[] {
const { alias, exclude } = this.resolveIntercept()
const { alias = {}, exclude } = this.resolveIntercept()
const defaultFilter = options.filter || ((meta) => options.includeRoot || !meta.workspaces)
const filter = (meta: PackageJson, path: string) => {
return defaultFilter(meta, path) && !exclude?.some((pattern) => {
Expand All @@ -185,7 +184,7 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
}
}

if (alias?.[name]) {
if (alias[name]) {
return makeArray(alias[name]).map((path) => {
if (!this.workspaces[path]) {
throw new Error(`cannot find workspace ${path} resolved by ${name}`)
Expand All @@ -194,6 +193,15 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
})
}

for (const key in alias) {
if (!key.endsWith('*')) continue
if (!name.startsWith(key.slice(0, -1))) continue
const results = makeArray(alias[key])
.map((path) => path.slice(0, -1) + name.slice(key.length - 1))
.filter((path) => this.workspaces[path])
if (results.length) return results
}

const targets = Object.keys(this.workspaces).filter((folder) => {
if (!filter(this.workspaces[folder], folder)) return
return folder.endsWith('/' + name)
Expand All @@ -217,7 +225,7 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
await this.ctx.events.flush()
if (!this.commands[name]) {
if (builtin.includes(name)) {
await this.ctx.loader.create({
await this.ctx.get('loader')?.create({
name: 'yakumo/' + name,
})
return this.execute(name, ...args)
Expand All @@ -234,7 +242,7 @@ export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
}

async start() {
if (this.ctx.get('loader')?.options.name !== 'yakumo') return
if (this.ctx.get('loader')?.config.name !== 'yakumo') return
const [name, ...args] = process.argv.slice(2)
if (!name) {
console.log('yakumo')
Expand Down

0 comments on commit 5bd357d

Please sign in to comment.