Skip to content

Commit

Permalink
Merge pull request #310 from marp-team/reset-chrome-path-cache-in-api
Browse files Browse the repository at this point in the history
Reset cached executable Chrome path if using Marp CLI through API interface
  • Loading branch information
yhatt authored Dec 5, 2020
2 parents 5891d05 + 24c9151 commit 4a65f0c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- Upgrade Node.js to 14 ([#309](https://github.com/marp-team/marp-cli/pull/309))
- Upgrade dependent packages to the latest version ([#309](https://github.com/marp-team/marp-cli/pull/309))

### Fixed

- Reset cached executable Chrome path if using Marp CLI through API interface ([#310](https://github.com/marp-team/marp-cli/pull/310))

## v0.22.1 - 2020-11-28

### Added
Expand Down
8 changes: 6 additions & 2 deletions src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { File, FileType } from './file'
import { Preview, fileToURI } from './preview'
import { Server } from './server'
import templates from './templates'
import { resetExecutablePath } from './utils/puppeteer'
import version from './version'
import watcher, { Watcher, notifier } from './watcher'

Expand Down Expand Up @@ -384,11 +385,14 @@ export const waitForObservation = () =>
resolversForObservation.push(res)
})

export const apiInterface = async (argv: string[] = []) =>
marpCli(argv, {
export const apiInterface = (argv: string[] = []) => {
resetExecutablePath()

return marpCli(argv, {
stdin: false,
throwErrorAlways: true,
})
}

export const cliInterface = (argv: string[] = []) =>
marpCli(argv, {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ export const launchPuppeteer = (
os.arch = arch
}
}

export const resetExecutablePath = () => {
executablePath = false
}
15 changes: 15 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { buffer as stdinBuffer } from 'get-stdin'
import api, { CLIError } from '../src/index'
import * as marpCli from '../src/marp-cli'
import * as puppeteerUtil from '../src/utils/puppeteer'

afterEach(() => {
jest.restoreAllMocks()
Expand Down Expand Up @@ -41,4 +42,18 @@ describe('Marp CLI API interface', () => {
expect(await marpCli.cliInterface(['-c', fakePath])).toBeGreaterThan(0)
await expect(api(['-c', fakePath])).rejects.toBeInstanceOf(CLIError)
})

it('resets cached Chrome path every time', async () => {
const resetExecutablePathSpy = jest.spyOn(
puppeteerUtil,
'resetExecutablePath'
)
jest.spyOn(marpCli, 'marpCli').mockResolvedValue(0)

await api([])
expect(resetExecutablePathSpy).toHaveBeenCalledTimes(1)

await api([])
expect(resetExecutablePathSpy).toHaveBeenCalledTimes(2)
})
})

0 comments on commit 4a65f0c

Please sign in to comment.