From f1fcf3aeb9fe10616404bc4cbff4c50785331bc2 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 24 Aug 2023 14:31:04 +0900 Subject: [PATCH] Replace is-docker to is-inside-container --- jest.config.js | 1 + package.json | 2 +- src/config.ts | 4 ++-- src/converter.ts | 4 ++-- src/marp-cli.ts | 4 ++-- src/utils/container.ts | 6 ++++++ src/utils/docker.ts | 4 ---- src/utils/puppeteer.ts | 6 +++--- test/marp-cli.ts | 8 ++++---- test/utils/puppeteer.ts | 8 ++++---- 10 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 src/utils/container.ts delete mode 100644 src/utils/docker.ts diff --git a/jest.config.js b/jest.config.js index 1a16dd9e..f507fcea 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,6 +8,7 @@ const esModules = [ 'find-up', 'globby', 'import-meta-resolve', + 'is-inside-container', 'is-docker', 'lighthouse-logger', 'locate-path', diff --git a/package.json b/package.json index a2f31900..073e5df4 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,7 @@ "image-size": "^1.0.2", "import-from": "^4.0.0", "import-meta-resolve": "^3.0.0", - "is-docker": "^3.0.0", + "is-inside-container": "^1.0.0", "jest": "^29.6.2", "jest-environment-jsdom": "^29.6.2", "jest-junit": "^16.0.0", diff --git a/src/config.ts b/src/config.ts index ab9bc69c..2f59a70c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,7 +10,7 @@ import { keywordsAsArray } from './engine/meta-plugin' import { error, isError } from './error' import { TemplateOption } from './templates' import { Theme, ThemeSet } from './theme' -import { isOfficialImage } from './utils/docker' +import { isOfficialDockerImage } from './utils/container' type Overwrite = Omit> & U @@ -132,7 +132,7 @@ export class MarpCLIConfig { const preview = (() => { const p = this.args.preview ?? this.conf.preview ?? false - if (p && isOfficialImage()) { + if (p && isOfficialDockerImage()) { warn( `Preview window cannot show within an official docker image. Preview option was ignored.` ) diff --git a/src/converter.ts b/src/converter.ts index 196314e8..ec912104 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -27,7 +27,7 @@ import templates, { TemplateResult, } from './templates/' import { ThemeSet } from './theme' -import { isOfficialImage } from './utils/docker' +import { isOfficialDockerImage } from './utils/container' import { pdfLib, setOutline } from './utils/pdf' import { generatePuppeteerDataDirPath, @@ -545,7 +545,7 @@ export class Converter { // directory so always create tmp file to home directory if in Linux. // (There is an exception for an official docker image) return baseFile.saveTmpFile({ - home: process.platform === 'linux' && !isOfficialImage(), + home: process.platform === 'linux' && !isOfficialDockerImage(), extension: '.html', }) })() diff --git a/src/marp-cli.ts b/src/marp-cli.ts index 76da36d1..49839ae7 100644 --- a/src/marp-cli.ts +++ b/src/marp-cli.ts @@ -9,7 +9,7 @@ import { File, FileType } from './file' import { Preview, fileToURI } from './preview' import { Server } from './server' import templates from './templates' -import { isOfficialImage } from './utils/docker' +import { isOfficialDockerImage } from './utils/container' import { resetExecutablePath } from './utils/puppeteer' import version from './version' import watcher, { Watcher, notifier } from './watcher' @@ -109,7 +109,7 @@ export const marpCli = async ( preview: { alias: 'p', describe: 'Open preview window', - hidden: isOfficialImage(), + hidden: isOfficialDockerImage(), group: OptionGroup.Basic, type: 'boolean', }, diff --git a/src/utils/container.ts b/src/utils/container.ts new file mode 100644 index 00000000..9aa65ac8 --- /dev/null +++ b/src/utils/container.ts @@ -0,0 +1,6 @@ +import _isInsideContainer from 'is-inside-container' + +export const isInsideContainer = () => + isOfficialDockerImage() || _isInsideContainer() + +export const isOfficialDockerImage = () => !!process.env.MARP_USER diff --git a/src/utils/docker.ts b/src/utils/docker.ts deleted file mode 100644 index 4a10aa9e..00000000 --- a/src/utils/docker.ts +++ /dev/null @@ -1,4 +0,0 @@ -import _isDocker from 'is-docker' - -export const isDocker = () => isOfficialImage() || _isDocker() -export const isOfficialImage = () => !!process.env.MARP_USER diff --git a/src/utils/puppeteer.ts b/src/utils/puppeteer.ts index ae98f7b0..3324dde2 100644 --- a/src/utils/puppeteer.ts +++ b/src/utils/puppeteer.ts @@ -5,8 +5,8 @@ import { launch } from 'puppeteer-core' import macDockIcon from '../assets/mac-dock-icon.png' import { warn } from '../cli' import { CLIErrorCode, error, isError } from '../error' -import { isDocker } from '../utils/docker' import { findChromeInstallation } from './chrome-finder' +import { isInsideContainer } from './container' import { findEdgeInstallation } from './edge-finder' import { isWSL, resolveWindowsEnv } from './wsl' @@ -76,11 +76,11 @@ export const generatePuppeteerLaunchArgs = async () => { const args = new Set(['--export-tagged-pdf', '--test-type']) // Docker environment and WSL environment need to disable sandbox. :( - if (isDocker() || isWSL()) args.add('--no-sandbox') + if (isInsideContainer() || isWSL()) args.add('--no-sandbox') // Workaround for Chrome 73 in docker and unit testing with CircleCI // https://github.com/GoogleChrome/puppeteer/issues/3774 - if (isDocker() || process.env.CI) + if (isInsideContainer() || process.env.CI) args.add('--disable-features=VizDisplayCompositor') // Enable View transitions API diff --git a/test/marp-cli.ts b/test/marp-cli.ts index 61b6563b..185a28d5 100644 --- a/test/marp-cli.ts +++ b/test/marp-cli.ts @@ -20,7 +20,7 @@ import { import { Preview } from '../src/preview' import { Server } from '../src/server' import { ThemeSet } from '../src/theme' -import * as docker from '../src/utils/docker' +import * as container from '../src/utils/container' import * as version from '../src/version' import { Watcher } from '../src/watcher' @@ -224,7 +224,7 @@ describe('Marp CLI', () => { describe('when CLI is running in an official Docker image', () => { it('does not output help about --preview option', async () => { const isOfficialImage = jest - .spyOn(docker, 'isOfficialImage') + .spyOn(container, 'isOfficialDockerImage') .mockReturnValue(true) try { @@ -482,7 +482,7 @@ describe('Marp CLI', () => { describe('when CLI is running in an official Docker image', () => { it('ignores --preview option with warning', async () => { const isOfficialImage = jest - .spyOn(docker, 'isOfficialImage') + .spyOn(container, 'isOfficialDockerImage') .mockReturnValue(true) const warn = jest.spyOn(cli, 'warn').mockImplementation() @@ -1187,7 +1187,7 @@ describe('Marp CLI', () => { describe('when CLI is running in an official Docker image', () => { it('ignores --preview option with warning', async () => { const isOfficialImage = jest - .spyOn(docker, 'isOfficialImage') + .spyOn(container, 'isOfficialDockerImage') .mockReturnValue(true) try { diff --git a/test/utils/puppeteer.ts b/test/utils/puppeteer.ts index fde1ddc2..8bc6bee4 100644 --- a/test/utils/puppeteer.ts +++ b/test/utils/puppeteer.ts @@ -17,8 +17,8 @@ const puppeteerUtils = (): typeof import('../../src/utils/puppeteer') => const chromeFinder = (): typeof import('../../src/utils/chrome-finder') => require('../../src/utils/chrome-finder') -const docker = (): typeof import('../../src/utils/docker') => - require('../../src/utils/docker') +const container = (): typeof import('../../src/utils/container') => + require('../../src/utils/container') const edgeFinder = (): typeof import('../../src/utils/edge-finder') => require('../../src/utils/edge-finder') @@ -152,8 +152,8 @@ describe('#generatePuppeteerLaunchArgs', () => { } }) - it('uses specific settings if running within a Docker container', async () => { - jest.spyOn(docker(), 'isDocker').mockImplementation(() => true) + it('uses specific settings if running within a container image', async () => { + jest.spyOn(container(), 'isInsideContainer').mockImplementation(() => true) jest .spyOn(chromeFinder(), 'findChromeInstallation') .mockResolvedValue('/usr/bin/chromium')