Skip to content

Commit

Permalink
feat(core): add filter for locate API
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 19, 2023
1 parent 756e34f commit 44df890
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
22 changes: 20 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface Manager {

export interface Arguments extends yargs.Arguments {
config: Options
_: string[]
}

export interface Options extends yargs.Options {
Expand Down Expand Up @@ -95,10 +96,16 @@ export class Context extends cordis.Context {
}
}

export interface LocateOptions {
includeRoot?: boolean
filter?(meta: PackageJson, path: string): boolean
}

export default class Yakumo {
cwd: string
argv: Arguments
manager: Manager
/** @deprecated */
targets: Record<string, PackageJson>
workspaces: Record<string, PackageJson>
indent = detect(content).indent
Expand Down Expand Up @@ -164,7 +171,18 @@ export default class Yakumo {
}))
}

locate(name: string, { includeRoot }: { includeRoot?: boolean } = {}) {
locate(name: string | string[], options: LocateOptions = {}): string[] {
const filter = options.filter || ((meta) => options.includeRoot || !meta.workspaces)
if (Array.isArray(name)) {
if (!name.length) {
return Object.keys(this.workspaces).filter((folder) => {
return filter(this.workspaces[folder], folder)
})
} else {
return name.flatMap((name) => this.locate(name, options))
}
}

if (this.config.alias?.[name]) {
return makeArray(this.config.alias[name]).map((path) => {
if (!this.workspaces[path]) {
Expand All @@ -175,7 +193,7 @@ export default class Yakumo {
}

const targets = Object.keys(this.workspaces).filter((folder) => {
if (!includeRoot && this.workspaces[folder].workspaces) return
if (!filter(this.workspaces[folder], folder)) return
const [last] = folder.split('/').reverse()
return name === last
})
Expand Down
44 changes: 24 additions & 20 deletions packages/core/src/plugins/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,47 +55,51 @@ async function serial<S, T>(list: S[], fn: (item: S) => Promise<T>) {

export default function apply(ctx: Context) {
ctx.register('publish', async () => {
const { argv, targets } = ctx.yakumo
const { argv } = ctx.yakumo
const spinner = ora()
if (argv._.length) {
const pending = Object.keys(targets).filter(path => targets[path].private)
let paths = ctx.yakumo.locate(argv._, {
filter: (meta) => {
// 1. workspace roots are always private
// 2. ignore private packages if not explicitly specified
return argv._.length ? !meta.workspaces : !meta.private
},
})

if (argv._.length) {
const pending = paths.filter(path => ctx.yakumo.workspaces[path].private)
if (pending.length) {
const paths = pending.map(path => targets[path].name).join(', ')
const paths = pending.map(path => ctx.yakumo.workspaces[path].name).join(', ')
const { value } = await prompts({
name: 'value',
type: 'confirm',
message: `workspace ${paths} ${pending.length > 1 ? 'are' : 'is'} private, switch to public?`,
})
if (!value) exit('operation cancelled.')

await Promise.all(pending.map(async (path) => {
delete targets[path].private
delete ctx.yakumo.workspaces[path].private
await ctx.yakumo.save(path)
}))
}
} else {
const entries = Object.entries(ctx.yakumo.targets)
let progress = 0
spinner.start(`Loading workspaces (0/${entries.length})`)
await Promise.all(entries.map(async ([path, meta]) => {
spinner.text = `Loading workspaces (${++progress}/${entries.length})`
if (!meta.private) {
const version = await getVersion(meta.name, isNext(meta.version))
if (gt(meta.version, version)) return
}
delete targets[path]
}))
spinner.start(`Loading workspaces (0/${paths.length})`)
paths = (await Promise.all(paths.map(async (path) => {
const meta = ctx.yakumo.workspaces[path]
spinner.text = `Loading workspaces (${++progress}/${paths.length})`
const version = await getVersion(meta.name, isNext(meta.version))
if (gt(meta.version, version)) return path
}))).filter(Boolean)
spinner.succeed()
}

const total = Object.keys(targets).length
const total = paths.length
if (!argv.debug) spinner.start(`Publishing packages (0/${total})`)

let completed = 0, failed = 0
await (argv.debug ? serial : parallel)(Object.entries(targets), async ([path, meta]) => {
await (argv.debug ? serial : parallel)(paths, async (path) => {
const meta = ctx.yakumo.workspaces[path]
try {
await ctx.parallel('publish/before', path, targets[path])
await ctx.parallel('publish/before', path, meta)
const args = [
'--tag', argv.tag ?? (isNext(meta.version) ? 'next' : 'latest'),
'--access', argv.access ?? 'public',
Expand All @@ -104,7 +108,7 @@ export default function apply(ctx: Context) {
if (argv.otp) args.push('--otp', argv.otp)
const code = await publish(ctx.yakumo.manager, path, meta, args, argv)
assert(!code)
await ctx.parallel('publish/after', path, targets[path])
await ctx.parallel('publish/after', path, meta)
} catch (e) {
failed++
} finally {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/plugins/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export default function apply(ctx: Context) {
}

const graph = new Graph(ctx.yakumo)
for (const path in ctx.yakumo.targets) {
const paths = ctx.yakumo.locate(ctx.yakumo.argv._)
for (const path of paths) {
graph.bump(graph.nodes[path], flag)
}

Expand Down
10 changes: 6 additions & 4 deletions packages/esbuild/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ const yamlPlugin = (options: yaml.LoadOptions = {}): Plugin => ({

export function apply(ctx: Context) {
ctx.register('esbuild', async () => {
await Promise.all(Object.entries(ctx.yakumo.targets).map(async ([key, value]) => {
const matrix = await compile(key, value, ctx.yakumo as any)
const paths = ctx.yakumo.locate(ctx.yakumo.argv._)
await Promise.all(paths.map(async (path) => {
const meta = ctx.yakumo.workspaces[path]
const matrix = await compile(path, meta, ctx.yakumo)
await Promise.all(matrix.map(async (options) => {
options.plugins.push(yamlPlugin())
await ctx.parallel('esbuild/before', options, value)
await ctx.parallel('esbuild/before', options, meta)
await bundle(options)
await ctx.parallel('esbuild/after', options, value)
await ctx.parallel('esbuild/after', options, meta)
})).catch(console.error)
}))
if (code) process.exit(code)
Expand Down
2 changes: 1 addition & 1 deletion packages/mocha/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function apply(ctx: Context) {
opts._ = opts._.flatMap((arg: string) => {
const [folder] = arg.split('/', 1)
const name = arg.slice(folder.length + 1) || '*'
return ctx.yakumo.locate(folder).map((path) => {
return ctx.yakumo.locate(folder, { includeRoot: true }).map((path) => {
return `${path}/tests/${name}.spec.ts`.slice(1)
})
})
Expand Down
11 changes: 7 additions & 4 deletions packages/tsc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ async function bundleNodes(nodes: Node[]) {

export function apply(ctx: Context) {
ctx.register('tsc', async () => {
const { targets, argv } = ctx.yakumo
const { argv } = ctx.yakumo
const paths = ctx.yakumo.locate(ctx.yakumo.argv._)

// build clean
if (argv.clean) {
const tasks = Object.keys(targets).map(async (path) => {
const tasks = paths.map(async (path) => {
const fullpath = join(cwd, path)
const tsconfig = await load(fullpath)
await Promise.all([
Expand All @@ -56,8 +57,8 @@ export function apply(ctx: Context) {

// Step 1: initialize nodes
const nodes: Record<string, Node> = {}
for (const path in targets) {
const meta = targets[path]
for (const path of paths) {
const meta = ctx.yakumo.workspaces[path]
if (!meta.main && !meta.exports) continue
const fullpath = join(cwd, path)
try {
Expand Down Expand Up @@ -127,5 +128,7 @@ export function apply(ctx: Context) {
}
await Promise.all(tasks)
}
}, {
boolean: ['clean'],
})
}

0 comments on commit 44df890

Please sign in to comment.