-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--debug
(-d
) option to CLI interface
- Loading branch information
Showing
5 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters