@mermaid-js/mermaid-cli 9.3.0 → 9.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mermaid-js/mermaid-cli",
3
- "version": "9.3.0",
3
+ "version": "9.4.0",
4
4
  "description": "Command-line interface for mermaid",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:mermaid-js/mermaid-cli.git",
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "chalk": "^5.0.1",
25
- "commander": "^9.0.0",
25
+ "commander": "^10.0.0",
26
26
  "puppeteer": "^19.0.0"
27
27
  },
28
28
  "devDependencies": {
package/src/index.js CHANGED
@@ -18,7 +18,7 @@ const error = message => {
18
18
  }
19
19
 
20
20
  const warn = message => {
21
- console.log(chalk.yellow(`\n${message}\n`))
21
+ console.warn(chalk.yellow(`\n${message}\n`))
22
22
  }
23
23
 
24
24
  const checkConfigFile = file => {
@@ -27,8 +27,6 @@ const checkConfigFile = file => {
27
27
  }
28
28
  }
29
29
 
30
- const inputPipedFromStdin = () => fs.fstatSync(0).isFIFO()
31
-
32
30
  const getInputData = async inputFile => new Promise((resolve, reject) => {
33
31
  // if an input file has been specified using '-i', it takes precedence over
34
32
  // piping from stdin
@@ -84,7 +82,7 @@ async function cli () {
84
82
  .addOption(new Option('-t, --theme [theme]', 'Theme of the chart').choices(['default', 'forest', 'dark', 'neutral']).default('default'))
85
83
  .addOption(new Option('-w, --width [width]', 'Width of the page').argParser(parseCommanderInt).default(800))
86
84
  .addOption(new Option('-H, --height [height]', 'Height of the page').argParser(parseCommanderInt).default(600))
87
- .option('-i, --input <input>', 'Input mermaid file. Files ending in .md will be treated as Markdown and all charts (e.g. ```mermaid (...)```) will be extracted and generated. Required.')
85
+ .option('-i, --input <input>', 'Input mermaid file. Files ending in .md will be treated as Markdown and all charts (e.g. ```mermaid (...)```) will be extracted and generated. Use `-` to read from stdin.')
88
86
  .option('-o, --output [output]', 'Output file. It should be either md, svg, png or pdf. Optional. Default: input + ".svg"')
89
87
  .addOption(new Option('-e, --outputFormat [format]', 'Output format for the generated image.').choices(['svg', 'png', 'pdf']).default(null, 'Loaded from the output file extension'))
90
88
  .addOption(new Option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'.').default('white'))
@@ -101,12 +99,15 @@ async function cli () {
101
99
  let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, puppeteerConfigFile, scale, pdfFit, quiet } = options
102
100
 
103
101
  // check input file
104
- if (!(input || inputPipedFromStdin())) {
105
- console.error(chalk.red('\nPlease specify input file: -i <input>\n'))
106
- // Log to stderr, and return with error exitCode
107
- commander.help({ error: true })
108
- }
109
- if (input && !fs.existsSync(input)) {
102
+ if (!input) {
103
+ warn('No input file specfied, reading from stdin. ' +
104
+ 'If you want to specify an input file, please use `-i <input>.` ' +
105
+ 'You can use `-i -` to read from stdin and to suppress this warning.'
106
+ )
107
+ } else if (input === '-') {
108
+ // `--input -` means read from stdin, but suppress the above warning
109
+ input = undefined
110
+ } else if (!fs.existsSync(input)) {
110
111
  error(`Input file "${input}" doesn't exist`)
111
112
  }
112
113
 
@@ -121,8 +122,8 @@ async function cli () {
121
122
  output = input ? (`${input}.svg`) : 'out.svg'
122
123
  }
123
124
  }
124
- if (!/\.(?:svg|png|pdf|md)$/.test(output)) {
125
- error('Output file must end with ".md", ".svg", ".png" or ".pdf"')
125
+ if (!/\.(?:svg|png|pdf|md|markdown)$/.test(output)) {
126
+ error('Output file must end with ".md"/".markdown", ".svg", ".png" or ".pdf"')
126
127
  }
127
128
  const outputDir = path.dirname(output)
128
129
  if (!fs.existsSync(outputDir)) {
@@ -346,10 +347,11 @@ function markdownImage ({ url, title, alt }) {
346
347
  /**
347
348
  * Renders a mermaid diagram or mermaid markdown file.
348
349
  *
349
- * @param {`${string}.md` | string} [input] - If this ends with `.md`, path to a markdown file containing mermaid.
350
+ * @param {`${string}.${"md" | "markdown"}` | string} [input] - If this ends with `.md`/`.markdown`,
351
+ * path to a markdown file containing mermaid.
350
352
  * If this is a string, loads the mermaid definition from the given file.
351
353
  * If this is `undefined`, loads the mermaid definition from stdin.
352
- * @param {`${string}.${"md" | "svg" | "png" | "pdf"}`} output - Path to the output file.
354
+ * @param {`${string}.${"md" | "markdown" | "svg" | "png" | "pdf"}`} output - Path to the output file.
353
355
  * @param {Object} [opts] - Options
354
356
  * @param {puppeteer.LaunchOptions} [opts.puppeteerConfig] - Puppeteer launch options.
355
357
  * @param {boolean} [opts.quiet] - If set, suppress log output.
@@ -372,7 +374,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
372
374
  if (!outputFormat) {
373
375
  outputFormat = path.extname(output).replace('.', '')
374
376
  }
375
- if (outputFormat === 'md') {
377
+ if (outputFormat === 'md' || outputFormat === 'markdown') {
376
378
  // fallback to svg in case no outputFormat is given and output file is MD
377
379
  outputFormat = 'svg'
378
380
  }
@@ -381,7 +383,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
381
383
  }
382
384
 
383
385
  const definition = await getInputData(input)
384
- if (/\.md$/.test(input)) {
386
+ if (/\.(md|markdown)$/.test(input)) {
385
387
  const imagePromises = []
386
388
  for (const mermaidCodeblockMatch of definition.matchAll(mermaidChartsInMarkdownRegexGlobal)) {
387
389
  const mermaidDefinition = mermaidCodeblockMatch[1]
@@ -391,7 +393,10 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
391
393
  // I.e. if "out.png", use "out-1.png", "out-2.png", etc
392
394
  // If it is an output `.md` file, use that to base .svg numbered diagrams on
393
395
  // I.e. if "out.md". use "out-1.svg", "out-2.svg", etc
394
- const outputFile = output.replace(/(\.(md|png|svg|pdf))$/, `-${imagePromises.length + 1}$1`).replace(/(\.md)$/, `.${outputFormat}`)
396
+ const outputFile = output.replace(
397
+ /(\.(md|markdown|png|svg|pdf))$/,
398
+ `-${imagePromises.length + 1}$1`
399
+ ).replace(/\.(md|markdown)$/, `.${outputFormat}`)
395
400
  const outputFileRelative = `./${path.relative(path.dirname(path.resolve(output)), path.resolve(outputFile))}`
396
401
 
397
402
  const imagePromise = (async () => {
@@ -416,7 +421,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
416
421
 
417
422
  const images = await Promise.all(imagePromises)
418
423
 
419
- if (/\.md$/.test(output)) {
424
+ if (/\.(md|markdown)$/.test(output)) {
420
425
  const outDefinition = definition.replace(mermaidChartsInMarkdownRegexGlobal, (_mermaidMd) => {
421
426
  // pop first image from front of array
422
427
  const { url, title, alt } = images.shift()