Skip to content

Commit

Permalink
feat(core): separate plugin entries
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 4, 2024
1 parent 27240d1 commit 1623516
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 38 deletions.
16 changes: 14 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
{
"name": "yakumo",
"description": "Manage complex workspaces with ease",
"version": "1.0.0-beta.9",
"version": "1.0.0-beta.11",
"type": "module",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"bin": "lib/cli.js",
"exports": {
".": "./lib/index.js",
"./cli": "./lib/cli.js",
"./utils": "./lib/utils.js",
"./list": "./lib/plugins/list.js",
"./prepare": "./lib/plugins/prepare.js",
"./publish": "./lib/plugins/publish.js",
"./test": "./lib/plugins/test.js",
"./upgrade": "./lib/plugins/upgrade.js",
"./version": "./lib/plugins/version.js",
"./package.json": "./package.json"
},
"engines": {
"node": "^18.0.0 || >=20.0.0"
},
Expand Down Expand Up @@ -36,7 +48,7 @@
"@types/yargs-parser": "^21.0.3"
},
"dependencies": {
"cordis": "^3.8.0",
"cordis": "^3.13.2",
"cosmokit": "^1.5.2",
"detect-indent": "^6.1.0",
"execa": "^5.1.1",
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ for (let i = 2; i < process.argv.length; i++) {
--i
}

await start({ name: 'yakumo' })
await start({
name: 'yakumo',
fallback: {
config: [
{ name: 'yakumo' },
{ name: 'yakumo/list' },
{ name: 'yakumo/prepare' },
{ name: 'yakumo/publish' },
{ name: 'yakumo/test' },
{ name: 'yakumo/upgrade' },
{ name: 'yakumo/version' },
],
},
})
54 changes: 26 additions & 28 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@ import { manager, spawnAsync } from './utils.js'
import kleur from 'kleur'
import { promises as fs, readFileSync } from 'node:fs'
import { Dict, makeArray } from 'cosmokit'
import list from './plugins/list.js'
import prepare from './plugins/prepare.js'
import publish from './plugins/publish.js'
import test from './plugins/test.js'
import upgrade from './plugins/upgrade.js'
import version from './plugins/version.js'

export * from './plugins/prepare.js'
export * from './plugins/publish.js'
export * from './plugins/test.js'
export * from './plugins/upgrade.js'
export * from './plugins/version.js'

export * from './utils.js'

export const cwd = process.cwd()
Expand Down Expand Up @@ -73,8 +62,9 @@ export interface Context {
}

export class Context extends cordis.Context {
constructor(config: any) {
constructor(config?: any) {
super(config)
this.provide('yakumo', undefined, true)
this.plugin(Yakumo, config)
}
}
Expand All @@ -96,7 +86,16 @@ export namespace Yakumo {
}
}

export default class Yakumo {
const builtin = [
'list',
'prepare',
'publish',
'test',
'upgrade',
'version',
]

export default class Yakumo extends cordis.Service<Yakumo.Config, Context> {
cwd: string
args = process.argv.slice(2)
argv!: Arguments
Expand All @@ -105,20 +104,12 @@ export default class Yakumo {
indent = detect(content).indent
commands: Dict = {}

constructor(ctx: Context, public config: Yakumo.Config) {
ctx.provide('yakumo', undefined, true)
constructor(public ctx: Context, public config: Yakumo.Config) {
super(ctx, 'yakumo', true)
ctx.mixin('yakumo', ['register'])
ctx.yakumo = this
this.cwd = cwd
this.manager = manager

ctx.plugin(list)
ctx.plugin(prepare)
ctx.plugin(publish)
ctx.plugin(test)
ctx.plugin(upgrade)
ctx.plugin(version)

for (const name in config.pipeline || {}) {
this.register(name, async () => {
const tasks = config.pipeline![name]
Expand All @@ -128,8 +119,6 @@ export default class Yakumo {
}
})
}

ctx.on('ready', () => this.start())
}

register(name: string, callback: () => void, options: Options = {}) {
Expand All @@ -156,7 +145,7 @@ export default class Yakumo {
}

resolveIntercept(): Yakumo.Intercept {
const caller: Context = this[Context.current]
const caller = this[Context.current]
let result = this.config
let intercept = caller[Context.intercept]
while (intercept) {
Expand Down Expand Up @@ -224,7 +213,16 @@ export default class Yakumo {
}

async execute(name: string, ...args: string[]) {
await this.ctx.events.flush()
if (!this.commands[name]) {
if (builtin.includes(name)) {
this.ctx.loader.config.push({
name: 'yakumo/' + name,
} as any)
await this.ctx.loader.writeConfig()
await this.ctx.loader.start()
return this.execute(name, ...args)
}
console.error(kleur.red(`unknown command: ${name}`))
process.exit(1)
}
Expand All @@ -241,7 +239,7 @@ export default class Yakumo {
console.log('yakumo')
process.exit(0)
}
await this.execute(this.args[0])
this.execute(this.args[0])
}

async install() {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ interface Node {
tree: Dict<Node>
}

export default function apply(ctx: Context) {
export const inject = ['yakumo']

export function apply(ctx: Context) {
function createNode(path: string): Node {
return { name: ctx.yakumo.workspaces[path].name, path, children: [], tree: {} }
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Context } from '../index.js'
import picomatch from 'picomatch'

export default function apply(ctx: Context) {
export const inject = ['yakumo']

export function apply(ctx: Context) {
ctx.register('prepare', async () => {
const { workspaces } = ctx.yakumo.workspaces['']
const current = new Set(workspaces)
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async function serial<S, T>(list: S[], fn: (item: S) => Promise<T>) {
for (const item of list) await fn(item)
}

export default function apply(ctx: Context) {
export const inject = ['yakumo']

export function apply(ctx: Context) {
ctx.register('publish', async () => {
const { argv } = ctx.yakumo
const spinner = ora()
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import parse from 'yargs-parser'
import unparse from 'yargs-unparser'
import globby from 'globby'

export default function apply(ctx: Context) {
export const inject = ['yakumo']

export function apply(ctx: Context) {
ctx.register('test', async () => {
function getFiles(names: string[]) {
if (!names.length) return ['**/tests/*.spec.ts']
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ declare module '../index.js' {
}
}

export default function apply(ctx: Context, config: Config = {}) {
export const inject = ['yakumo']

export function apply(ctx: Context, config: Config = {}) {
ctx.register('upgrade', async () => {
const paths = ctx.yakumo.locate(ctx.yakumo.argv._, { includeRoot: true })
const { manager } = ctx.yakumo
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ class Graph {
}
}

export default function apply(ctx: Context) {
export const inject = ['yakumo']

export function apply(ctx: Context) {
ctx.register('version', async () => {
if (!ctx.yakumo.argv._.length) {
const yes = await confirm('You did not specify any packages to bump. Do you want to bump all the packages?')
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"yakumo": ["packages/core/src"],
"yakumo": [
"packages/core/src",
"packages/core/src/plugins",
],
"yakumo-*": ["packages/*/src"],
"@yakumojs/*": ["packages/*/src"],
},
Expand Down

0 comments on commit 1623516

Please sign in to comment.