From 509d5aee41a0929186ad8fb701693828546ea88a Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 19 Feb 2023 04:06:30 +0900 Subject: [PATCH 1/2] Refactor ResolvedEngine class to find its package.json when needed --- src/engine.ts | 26 +++++++++++++++----------- src/version.ts | 12 +++++++----- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/engine.ts b/src/engine.ts index 01bcd4f0..bbbfe220 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -19,7 +19,8 @@ const delayedEngineResolver = ( export class ResolvedEngine { klass: Engine - package?: Record + + private _cachedPackage?: Record | null private static _defaultEngine: ResolvedEngine | undefined @@ -27,12 +28,7 @@ export class ResolvedEngine { engine: ResolvableEngine | ResolvableEngine[], from?: string ): Promise { - 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 { @@ -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 @@ -83,14 +86,15 @@ export class ResolvedEngine { this.klass = klass } - private async resolvePackage(): Promise { + 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 } // NOTE: It cannot test because of overriding `require` in Jest context. diff --git a/src/version.ts b/src/version.ts index 7eee137a..94768ce5 100644 --- a/src/version.ts +++ b/src/version.ts @@ -4,21 +4,23 @@ import { MarpCLIConfig } from './config' import { ResolvedEngine } from './engine' export const isMarpCore = async (engine: ResolvedEngine): Promise => - 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` } From e1d16d3c1f6b232d68ceb7b9a5cc082f44800d60 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 19 Feb 2023 07:39:23 +0900 Subject: [PATCH 2/2] [ci skip] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca114652..216227e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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