Skip to content

Commit

Permalink
feat: introduce addon system, add vscode addon, close #129
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 12, 2024
1 parent d32f50b commit b10999f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/addons/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { addonVSCode } from './vscode';

Check failure on line 1 in src/addons/index.ts

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon

export const builtinAddons = [
addonVSCode,
]
22 changes: 22 additions & 0 deletions src/addons/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Addon } from '../types'

/**
* Sync VS Code engine with the version of `@types/vscode`
*/
export const addonVSCode: Addon = {
beforeWrite(pkg) {
if (!pkg.raw?.engines?.vscode) {
return
}

const version = pkg.raw.dependencies?.['@types/vscode']
|| pkg.raw.devDependencies?.['@types/vscode']
|| pkg.raw.peerDependencies?.['@types/vscode']

if (version && pkg.raw.engines?.vscode !== version) {
// eslint-disable-next-line no-console
console.log(`[addon] Updated VS Code engine field to ${version}`)
pkg.raw.engines.vscode = version
}
},
}
4 changes: 3 additions & 1 deletion src/commands/check/checkGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function checkGlobal(options: CheckOptions) {
])
const pkgs = globalPkgs.flat(1)

const bars = options.loglevel === 'silent' ? null : createMultiProgressBar()
const bars = options.loglevel === 'silent'
? null
: createMultiProgressBar()
await Promise.all(pkgs.map(async (pkg) => {
const depBar = bars?.create(pkg.deps.length, 0, { type: c.green(pkg.agent) })
await resolvePackage(
Expand Down
8 changes: 7 additions & 1 deletion src/commands/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from '../../types'
import { CheckPackages } from '../../api/check'
import { writePackage } from '../../io/packages'
import { builtinAddons } from '../../addons'
import { promptInteractive } from './interactive'
import { outputErr, renderPackages } from './render'

Expand Down Expand Up @@ -96,8 +97,13 @@ export async function check(options: CheckOptions) {
}

if (options.write) {
for (const pkg of resolvePkgs)
for (const pkg of resolvePkgs) {
// TODO: somehow make this field controlable by CLI?
for (const addon of (options.addons || builtinAddons)) {
await addon.postprocess?.(pkg, options)
}
await writePackage(pkg, options)
}
}

// tips
Expand Down
19 changes: 11 additions & 8 deletions src/io/packageJson.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import type { CommonOptions, PackageMeta, RawDep } from '../types'
import { builtinAddons } from '../addons'
import { dumpDependencies, getByPath, parseDependencies, parseDependency, setByPath } from './dependencies'
import { readJSON, writeJSON } from './packages'

Expand Down Expand Up @@ -56,28 +57,30 @@ export async function writePackageJSON(
pkg: PackageMeta,
options: CommonOptions,
) {
const { raw, filepath, resolved } = pkg

let changed = false

depsFields.forEach((key) => {
if (options.depFields?.[key] === false)
return
if (key === 'packageManager') {
const value = Object.entries(dumpDependencies(resolved, 'packageManager'))[0]
const value = Object.entries(dumpDependencies(pkg.resolved, 'packageManager'))[0]
if (value) {
raw.packageManager = `${value[0]}@${value[1].replace('^', '')}`
pkg.raw.packageManager = `${value[0]}@${value[1].replace('^', '')}`
changed = true
}
}
else {
if (getByPath(raw, key)) {
setByPath(raw, key, dumpDependencies(resolved, key))
if (getByPath(pkg.raw, key)) {
setByPath(pkg.raw, key, dumpDependencies(pkg.resolved, key))
changed = true
}
}
})

if (changed)
await writeJSON(filepath, raw)
if (changed) {
for (const addon of (options.addons || builtinAddons)) {
await addon.beforeWrite?.(pkg, options)
}
await writeJSON(pkg.filepath, pkg.raw)
}
}
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export interface CommonOptions {
* Override bumping mode for specific dependencies
*/
packageMode?: { [name: string]: PackageMode }
/**
* Custom addons
*
* @default builtin addons
*/
addons?: Addon[]
}

export interface UsageOptions extends CommonOptions {
Expand Down Expand Up @@ -164,3 +170,15 @@ export interface InteractiveContext {
*/
isChecked: (dep: RawDep) => boolean
}

export interface Addon {
postprocess?: (
pkg: PackageMeta,
options: CheckOptions,
) => void | Promise<void>

beforeWrite?: (
pkg: PackageMeta,
options: CheckOptions,
) => void | Promise<void>
}

0 comments on commit b10999f

Please sign in to comment.