Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply lazy resolution for engine's package.json #503

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Upgrade Marp Core to [v3.5.0](https://github.com/marp-team/marp-core/releases/v3.5.0) ([#502](https://github.com/marp-team/marp-cli/pull/502))
- Upgrade Node.js and dependent packages ([#502](https://github.com/marp-team/marp-cli/pull/502))

### Fixed

- Apply lazy resolution for engine's `package.json` ([#503](https://github.com/marp-team/marp-cli/pull/503))

## v2.3.0 - 2023-01-08

### Breaking
Expand Down
26 changes: 15 additions & 11 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ const delayedEngineResolver = (

export class ResolvedEngine {
klass: Engine
package?: Record<string, any>

private _cachedPackage?: Record<string, any> | null

private static _defaultEngine: ResolvedEngine | undefined

static async resolve(
engine: ResolvableEngine | ResolvableEngine[],
from?: string
): Promise<ResolvedEngine> {
const resolvedEngine = new ResolvedEngine(
await ResolvedEngine.resolveModule(engine, from)
)

await resolvedEngine.resolvePackage()
return resolvedEngine
return new ResolvedEngine(await ResolvedEngine.resolveModule(engine, from))
}

static async resolveDefaultEngine(): Promise<ResolvedEngine> {
Expand All @@ -51,6 +47,13 @@ export class ResolvedEngine {
return ResolvedEngine._defaultEngine
}

async getPackage() {
if (this._cachedPackage === undefined) {
this._cachedPackage = await this.resolvePackage()
}
return this._cachedPackage
}

private static async resolveModule(
engine: ResolvableEngine | ResolvableEngine[],
from?: string
Expand Down Expand Up @@ -83,14 +86,15 @@ export class ResolvedEngine {
this.klass = klass
}

private async resolvePackage(): Promise<void> {
private async resolvePackage() {
const classPath = this.findClassPath(this.klass)
if (!classPath) return
if (!classPath) return null

const pkgPath = await pkgUp({ cwd: path.dirname(classPath) })
if (!pkgPath) return
if (!pkgPath) return null

this.package = require(pkgPath)
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(pkgPath) as Record<string, any>
}

// NOTE: It cannot test because of overriding `require` in Jest context.
Expand Down
12 changes: 7 additions & 5 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { MarpCLIConfig } from './config'
import { ResolvedEngine } from './engine'

export const isMarpCore = async (engine: ResolvedEngine): Promise<boolean> =>
engine.package?.name === '@marp-team/marp-core' ||
(await engine.getPackage())?.name === '@marp-team/marp-core' ||
engine === (await ResolvedEngine.resolveDefaultEngine())

export default async function outputVersion(config: MarpCLIConfig): Promise<0> {
let engineVer = ''

const { engine } = config
const enginePackage = await engine.getPackage()

if (await isMarpCore(engine)) {
engineVer = `@marp-team/marp-core v${bundledCoreVer}`

if (engine.package && engine.package.version !== bundledCoreVer) {
engineVer = `user-installed @marp-team/marp-core v${engine.package.version}`
if (enginePackage && enginePackage.version !== bundledCoreVer) {
engineVer = `user-installed @marp-team/marp-core v${enginePackage.version}`
}
} else if (engine.package?.name && engine.package.version) {
engineVer = `customized engine in ${engine.package.name} v${engine.package.version}`
} else if (enginePackage?.name && enginePackage.version) {
engineVer = `customized engine in ${enginePackage.name} v${enginePackage.version}`
} else {
engineVer = `customized engine`
}
Expand Down