Skip to content

Commit

Permalink
Update module for getting stdin, and remove get-stdin package
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Feb 3, 2025
1 parent c308ea4 commit fa99481
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 57 deletions.
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
"eslint-plugin-unicorn": "^56.0.1",
"express": "^4.21.2",
"fast-plist": "^0.1.3",
"get-stdin": "^9.0.0",
"globals": "^15.14.0",
"globby": "^14.0.2",
"image-size": "^1.2.0",
Expand Down
32 changes: 1 addition & 31 deletions src/file.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
/* eslint-disable @typescript-eslint/no-namespace */
import fs from 'node:fs'
import path from 'node:path'
import { setTimeout as setTimeoutPromise } from 'node:timers/promises'
import * as url from 'node:url'
import chalk from 'chalk'
import _getStdin from 'get-stdin'
import { globby, Options as GlobbyOptions } from 'globby'
import { info } from './cli'
import { debug } from './utils/debug'
import { getStdin } from './utils/stdin'
import { generateTmpName } from './utils/tmp'

export const markdownExtensions = ['md', 'mdown', 'markdown', 'markdn']

const STDIN_NOTICE_DELAY = 3000

const getStdin = async () => {
const delayedNoticeController = new AbortController()

setTimeoutPromise(STDIN_NOTICE_DELAY, null, {
ref: false,
signal: delayedNoticeController.signal,
})
.then(() => {
info(
`Currently waiting data from stdin stream. Conversion will start after finished reading. (Pass ${chalk.yellow`--no-stdin`} option if it was not intended)`
)
})
.catch(() => {
// No ops
})

debug('Reading stdin stream...')
const buf = await _getStdin.buffer()

debug('Read from stdin: %d bytes', buf.length)
delayedNoticeController.abort()

return buf
}

interface GenerateTmpFileInterfaceOptions {
extension?: `.${string}`
}
Expand Down
34 changes: 34 additions & 0 deletions src/utils/stdin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import streamConsumers from 'node:stream/consumers'
import { setTimeout as setTimeoutPromise } from 'node:timers/promises'
import chalk from 'chalk'
import { info } from '../cli'
import { debug } from './debug'

const STDIN_NOTICE_DELAY = 3000

export const getStdin = async (): Promise<Buffer> => {
if (process.stdin.isTTY) return Buffer.alloc(0)

const delayedNoticeController = new AbortController()

setTimeoutPromise(STDIN_NOTICE_DELAY, null, {
ref: false,
signal: delayedNoticeController.signal,
})
.then(() => {
info(
`Currently waiting data from stdin stream. Conversion will start after finished reading. (Pass ${chalk.yellow`--no-stdin`} option if it was not intended)`
)
})
.catch(() => {
// No ops
})

debug('Reading stdin stream...')
const buf = await streamConsumers.buffer(process.stdin)

debug('Read from stdin: %d bytes', buf.length)
delayedNoticeController.abort()

return buf
}
5 changes: 0 additions & 5 deletions test/__mocks__/get-stdin.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import getStdin from 'get-stdin'
import api, { CLIError } from '../src/index'
import * as marpCli from '../src/marp-cli'
import * as stdin from '../src/utils/stdin'

afterEach(() => {
jest.clearAllMocks()
Expand All @@ -27,7 +27,7 @@ describe('Marp CLI API interface', () => {
it('does not read input from stdin if called API', async () => {
jest.spyOn(console, 'log').mockImplementation()

const stdinBuffer = jest.spyOn(getStdin, 'buffer')
const stdinBuffer = jest.spyOn(stdin, 'getStdin')

await marpCli.cliInterface([])
expect(stdinBuffer).toHaveBeenCalled()
Expand Down
6 changes: 2 additions & 4 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'node:path'
import { version as coreVersion } from '@marp-team/marp-core/package.json'
import { version as marpitVersion } from '@marp-team/marpit/package.json'
import * as cosmiconfigExplorer from 'cosmiconfig/dist/Explorer' // eslint-disable-line import-x/namespace
import getStdin from 'get-stdin'
import stripAnsi from 'strip-ansi'
import { version as cliVersion } from '../package.json'
import { defaultFinders } from '../src/browser/finder'
Expand All @@ -22,6 +21,7 @@ import { Preview } from '../src/preview'
import { Server } from '../src/server'
import { ThemeSet } from '../src/theme'
import * as container from '../src/utils/container'
import * as stdin from '../src/utils/stdin'
import * as version from '../src/version'
import { Watcher } from '../src/watcher'

Expand Down Expand Up @@ -1752,9 +1752,7 @@ describe('Marp CLI', () => {
cliInfo = jest.spyOn(cli, 'info').mockImplementation()
stdout = jest.spyOn(process.stdout, 'write').mockImplementation()

jest
.spyOn(getStdin, 'buffer')
.mockResolvedValue(Buffer.from('# markdown'))
jest.spyOn(stdin, 'getStdin').mockResolvedValue(Buffer.from('# markdown'))

// reset cached stdin buffer
;(File as any).stdinBuffer = undefined
Expand Down

0 comments on commit fa99481

Please sign in to comment.