Skip to content

Commit

Permalink
refa: drop ctx.yakumo.targets
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 24, 2023
1 parent ed02dcc commit 80f0903
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 44 deletions.
4 changes: 0 additions & 4 deletions packages/core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ The name comes from the Japanese word "八雲". In Touhou Project, Yakumo Yukari
| Extension | Version | Description |
| ------ | ------ | ----------- |
| [yakumo-esbuild](./packages/esbuild/) | [![npm](https://img.shields.io/npm/v/yakumo-esbuild?style=flat-square)](https://www.npmjs.com/package/yakumo-esbuild) | execute [esbuild](https://esbuild.github.io) |
| [yakumo-mocha](./packages/mocha/) | [![npm](https://img.shields.io/npm/v/yakumo-mocha?style=flat-square)](https://www.npmjs.com/package/yakumo-mocha) | execute [mocha](https://mochajs.org) |
| [yakumo-publish](./packages/publish/) | [![npm](https://img.shields.io/npm/v/yakumo-publish?style=flat-square)](https://www.npmjs.com/package/yakumo-publish) | publish packages |
| [yakumo-tsc](./packages/tsc/) | [![npm](https://img.shields.io/npm/v/yakumo-tsc?style=flat-square)](https://www.npmjs.com/package/yakumo-tsc) | compile [TypeScript](https://www.typescriptlang.org) |
| [yakumo-upgrade](./packages/upgrade/) | [![npm](https://img.shields.io/npm/v/yakumo-upgrade?style=flat-square)](https://www.npmjs.com/package/yakumo-upgrade) | upgrade package dependencies |
| [yakumo-version](./packages/version/) | [![npm](https://img.shields.io/npm/v/yakumo-version?style=flat-square)](https://www.npmjs.com/package/yakumo-version) | update package versions |
36 changes: 5 additions & 31 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import detect from 'detect-indent'
import { manager, spawnAsync } from './utils'
import { red } from 'kleur'
import { promises as fsp, readFileSync } from 'fs'
import { Module } from 'module'
import { Dict, makeArray, pick } from 'cosmokit'
import { Dict, makeArray } from 'cosmokit'
import prepare from './plugins/prepare'
import publish from './plugins/publish'
import test from './plugins/test'
Expand All @@ -25,16 +24,6 @@ export const cwd = process.cwd()
const content = readFileSync(`${cwd}/package.json`, 'utf8')
export const meta: PackageJson = JSON.parse(content)

export const configRequire = Module.createRequire(cwd + '/package.json')

export function requireSafe(id: string) {
try {
return configRequire(id)
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e
}
}

export interface Commands {}

export interface PackageConfig {}
Expand Down Expand Up @@ -62,7 +51,7 @@ export interface Options extends yargs.Options {

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

export interface PackageJson extends Partial<Record<DependencyType, Record<string, string>>> {
export interface PackageJson extends Partial<Record<DependencyType, Dict<string>>> {
name: string
type?: 'module' | 'commonjs'
main?: string
Expand All @@ -74,7 +63,7 @@ export interface PackageJson extends Partial<Record<DependencyType, Record<strin
version?: string
workspaces?: string[]
yakumo?: PackageConfig
peerDependenciesMeta?: Record<string, { optional?: boolean }>
peerDependenciesMeta?: Dict<{ optional?: boolean }>
}

export namespace PackageJson {
Expand Down Expand Up @@ -105,15 +94,13 @@ export default class Yakumo {
cwd: string
argv: Arguments
manager: Manager
/** @deprecated */
targets: Record<string, PackageJson>
workspaces: Record<string, PackageJson>
workspaces: Dict<PackageJson>
indent = detect(content).indent
commands: Commands = {}

constructor(ctx: Context, public config: ProjectConfig) {
ctx.provide('yakumo', this, true)
ctx.root.mixin('yakumo', ['register'])
ctx.mixin('yakumo', ['register'])
ctx.plugin(logger)
this.cwd = cwd
this.manager = manager
Expand Down Expand Up @@ -141,10 +128,6 @@ export default class Yakumo {
this.commands[name] = [callback, options]
}

require(id: string) {
return configRequire(id)
}

async initialize(argv: Arguments) {
this.argv = argv
const folders = await globby(meta.workspaces || [], {
Expand All @@ -160,15 +143,6 @@ export default class Yakumo {
return [path, require(`${cwd}${path}/package.json`)] as [string, PackageJson]
} catch {}
}).filter(Boolean))

if (!argv._.length || argv.config.manual) {
this.targets = { ...this.workspaces }
return
}

this.targets = pick(this.workspaces, argv._.flatMap((name: string) => {
return this.locate(name)
}))
}

locate(name: string | string[], options: LocateOptions = {}): string[] {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/plugins/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,7 @@ export default function apply(ctx: Context) {
} else {
spinner.succeed(`Published ${total} packages.`)
}
}, {
boolean: ['debug'],
})
}
19 changes: 10 additions & 9 deletions packages/core/src/plugins/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ declare module '..' {

export default function apply(ctx: Context, config: Config = {}) {
ctx.register('upgrade', async () => {
const { targets, manager } = ctx.yakumo
const paths = ctx.yakumo.locate(ctx.yakumo.argv._)
const { manager } = ctx.yakumo
const { concurrency = 10 } = config || {}
const deps: Record<string, Record<string, Partial<Record<DependencyType, string[]>>>> = {}
for (const path in targets) {
load(path, targets[path])
for (const path of paths) {
load(path, ctx.yakumo.workspaces[path])
}

const output: string[] = []
const requests = Object.keys(deps)
const names = Object.values(targets).map(p => p.name)
const names = paths.map(path => ctx.yakumo.workspaces[path].name)
const spinner = ora(`progress: 0/${requests.length}`).start()
let progress = 0
function updateProgress() {
Expand All @@ -52,18 +53,18 @@ export default function apply(ctx: Context, config: Config = {}) {
const newRange = oldRange[0] + newVersion
output.push(`- ${yellow(dep)}: ${cyan(oldVersion)} -> ${green(newVersion)}${newVersion === lastestVersion ? '' : ` (latest: ${lastestVersion})`}`)
for (const name in deps[request]) {
Object.defineProperty(targets[name], '$dirty', { value: true })
Object.defineProperty(ctx.yakumo.workspaces[name], '$dirty', { value: true })
for (const type in deps[request][name]) {
for (const key of deps[request][name][type]) {
targets[name][type][key] = targets[name][type][key].slice(0, -oldRange.length) + newRange
ctx.yakumo.workspaces[name][type][key] = ctx.yakumo.workspaces[name][type][key].slice(0, -oldRange.length) + newRange
}
}
}
}, { concurrency })
spinner.succeed()

for (const path in targets) {
if (!targets[path].$dirty) continue
for (const path of paths) {
if (!ctx.yakumo.workspaces[path].$dirty) continue
await ctx.yakumo.save(path)
}

Expand All @@ -82,7 +83,7 @@ export default function apply(ctx: Context, config: Config = {}) {
const value = meta[type][key]
const prefix = /^(npm:.+@)?/.exec(value)[0]
const range = value.slice(prefix.length)
if (targets[key] || !'^~'.includes(range[0])) continue
if (ctx.yakumo.workspaces[key] || !'^~'.includes(range[0])) continue
const request = (prefix ? prefix.slice(4, -1) : key) + ':' + range
;(((deps[request] ||= {})[path] ||= {})[type] ||= []).push(key)
}
Expand Down

0 comments on commit 80f0903

Please sign in to comment.