Skip to content

Commit

Permalink
Add --debug (-d) option to CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 27, 2024
1 parent c04622e commit 88ce7ac
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
13 changes: 10 additions & 3 deletions marp-cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/usr/bin/env node

'use strict'
{
const prepare = require('./lib/prepare.js')
const cli = prepare.cliPrepare()

require('./lib/marp-cli.js')
.cliInterface(process.argv.slice(2))
.then((exitCode) => process.on('exit', () => process.exit(exitCode)))
if (cli.debug)
process.env.DEBUG = `${process.env.DEBUG ? `${process.env.DEBUG},` : ''}${cli.debug}`

require('./lib/marp-cli.js')
.cliInterface(cli.args)
.then((exitCode) => process.on('exit', () => process.exit(exitCode)))
}
6 changes: 5 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export default [
},
{
...cli,
input: ['src/marp-cli.ts', 'src/index.ts'],
input: [
'src/index.ts', // Node.js entrypoint
'src/marp-cli.ts', // CLI entrypoint
'src/prepare.ts', // CLI preparation
],
output: { compact, dir: 'lib', exports: 'named', format: 'cjs' },
},
]
30 changes: 24 additions & 6 deletions src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ enum OptionGroup {
Marp = 'Marp / Marpit Options:',
}

export interface MarpCLIInternalOptions {
export interface MarpCLIOptions {
baseUrl?: string
stdin: boolean
throwErrorAlways: boolean
}

export type MarpCLIAPIOptions = Pick<MarpCLIInternalOptions, 'baseUrl'>
export type MarpCLIAPIOptions = Pick<MarpCLIOptions, 'baseUrl'>

export interface MarpCLICLIOptions {
debug?: string | boolean
}

export interface ObservationHelper {
stop: () => void
Expand All @@ -44,7 +48,7 @@ Usage:

export const marpCli = async (
argv: string[],
{ baseUrl, stdin: defaultStdin, throwErrorAlways }: MarpCLIInternalOptions
{ baseUrl, stdin: defaultStdin, throwErrorAlways }: MarpCLIOptions
): Promise<number> => {
let browserManager: BrowserManager | undefined
let server: Server | undefined
Expand All @@ -69,6 +73,14 @@ export const marpCli = async (
group: OptionGroup.Basic,
type: 'boolean',
},
debug: {
// NOTE: This option will not be parsed by yargs because it has been pre-parsed by prepare.ts
alias: 'd',
describe: 'Show debug logs (bool or filter pattern)',
defaultDescription: 'false',
group: OptionGroup.Basic,
type: 'string',
},
output: {
alias: 'o',
describe: 'Output file path (or directory when input-dir is passed)',
Expand Down Expand Up @@ -181,7 +193,7 @@ export const marpCli = async (
},
template: {
describe: 'Choose template',
defaultDescription: 'bespoke',
defaultDescription: '"bespoke"',
group: OptionGroup.Template,
choices: Object.keys(templates),
type: 'string',
Expand Down Expand Up @@ -469,7 +481,13 @@ export const waitForObservation = () =>
export const apiInterface = (argv: string[], opts: MarpCLIAPIOptions = {}) =>
marpCli(argv, { ...opts, stdin: false, throwErrorAlways: true })

export const cliInterface = (argv: string[] = []) =>
marpCli(argv, { stdin: true, throwErrorAlways: false })
export const cliInterface = (argv: string[], opts: MarpCLICLIOptions = {}) => {
if (process.env.DEBUG) {
cli.info(
`Debug logging is enabled. (Filter pattern: ${chalk.yellow(process.env.DEBUG)})`
)
}
return marpCli(argv, { stdin: true, throwErrorAlways: false })
}

export default cliInterface
47 changes: 47 additions & 0 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const defaultDebug = 'marp-cli*'

const debugOptionMatcher = /^(?:-d|--debug)(?:$|=)/

const enableValues = ['true', '1']
const falseValues = ['false', '0']
const starValues = ['all', 'full']

const parseDebugValue = (value: string) => {
const trimmedValue = value.trim()
const normalizedValue = trimmedValue.toLowerCase()

if (starValues.includes(normalizedValue)) return '*'
if (enableValues.includes(normalizedValue)) return defaultDebug
if (falseValues.includes(normalizedValue)) return false

return trimmedValue || defaultDebug
}

export const cliPrepare = (args = process.argv.slice(2)) => {
// Parse debug option
let debug: string | false = false

const normalizedArgs = [...args]
const dbgIdx = normalizedArgs.findIndex((arg) => debugOptionMatcher.test(arg))

if (dbgIdx >= 0) {
const dbgOption = normalizedArgs[dbgIdx]

if (dbgOption.startsWith('-d=') || dbgOption.startsWith('--debug=')) {
debug = parseDebugValue(dbgOption.slice(dbgOption.indexOf('=') + 1))
normalizedArgs.splice(dbgIdx, 1)
} else {
const nextArg = normalizedArgs[dbgIdx + 1]

if (!nextArg || nextArg.startsWith('-')) {
debug = defaultDebug
normalizedArgs.splice(dbgIdx, 1)
} else {
debug = parseDebugValue(nextArg)
normalizedArgs.splice(dbgIdx, 2)
}
}
}

return { args: normalizedArgs, debug }
}
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Marp CLI API interface', () => {
expect(ret).toBe(0)
expect(marpCliSpy).toHaveBeenCalled()

const opts: marpCli.MarpCLIInternalOptions = marpCliSpy.mock.calls[0][1]
const opts: marpCli.MarpCLIOptions = marpCliSpy.mock.calls[0][1]

expect(opts.baseUrl).toBe('https://example.com/')
expect(opts.stdin).toBe(false)
Expand Down

0 comments on commit 88ce7ac

Please sign in to comment.