From 80f090368ea60bdcb30d85c83f8ac8faf782ee70 Mon Sep 17 00:00:00 2001 From: Shigma Date: Mon, 25 Dec 2023 00:50:35 +0800 Subject: [PATCH] refa: drop ctx.yakumo.targets --- packages/core/readme.md | 4 ---- packages/core/src/index.ts | 36 ++++------------------------ packages/core/src/plugins/publish.ts | 2 ++ packages/core/src/plugins/upgrade.ts | 19 ++++++++------- 4 files changed, 17 insertions(+), 44 deletions(-) diff --git a/packages/core/readme.md b/packages/core/readme.md index 6a06e4e..e4f0828 100644 --- a/packages/core/readme.md +++ b/packages/core/readme.md @@ -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 | diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 16648ca..0dfc3b2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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' @@ -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 {} @@ -62,7 +51,7 @@ export interface Options extends yargs.Options { export type DependencyType = 'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies' -export interface PackageJson extends Partial>> { +export interface PackageJson extends Partial>> { name: string type?: 'module' | 'commonjs' main?: string @@ -74,7 +63,7 @@ export interface PackageJson extends Partial + peerDependenciesMeta?: Dict<{ optional?: boolean }> } export namespace PackageJson { @@ -105,15 +94,13 @@ export default class Yakumo { cwd: string argv: Arguments manager: Manager - /** @deprecated */ - targets: Record - workspaces: Record + workspaces: Dict 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 @@ -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 || [], { @@ -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[] { diff --git a/packages/core/src/plugins/publish.ts b/packages/core/src/plugins/publish.ts index 00fe052..6356413 100644 --- a/packages/core/src/plugins/publish.ts +++ b/packages/core/src/plugins/publish.ts @@ -121,5 +121,7 @@ export default function apply(ctx: Context) { } else { spinner.succeed(`Published ${total} packages.`) } + }, { + boolean: ['debug'], }) } diff --git a/packages/core/src/plugins/upgrade.ts b/packages/core/src/plugins/upgrade.ts index 8163a57..5c0c16b 100644 --- a/packages/core/src/plugins/upgrade.ts +++ b/packages/core/src/plugins/upgrade.ts @@ -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>>> = {} - 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() { @@ -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) } @@ -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) }