cli.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const conventionalChangelogWriter = require('./')
  4. const forEach = require('lodash').forEach
  5. const fs = require('fs')
  6. const meow = require('meow')
  7. const path = require('path')
  8. const split = require('split')
  9. const cli = meow(`
  10. Usage
  11. conventional-changelog-writer <path> [<path> ...]
  12. cat <path> | conventional-changelog-writer
  13. ,
  14. Example
  15. conventional-changelog-writer commits.ldjson
  16. cat commits.ldjson | conventional-changelog-writer
  17. ,
  18. Options
  19. -c, --context A filepath of a json that is used to define template variables
  20. -o, --options A filepath of a javascript object that is used to define options
  21. `, {
  22. flags: {
  23. context: {
  24. alias: 'c',
  25. type: 'string'
  26. },
  27. options: {
  28. alias: 'o',
  29. type: 'string'
  30. }
  31. }
  32. })
  33. const filePaths = []
  34. const flags = cli.flags
  35. forEach(cli.input, function (input) {
  36. filePaths.push(input)
  37. })
  38. const length = filePaths.length
  39. let templateContext
  40. const contextPath = flags.context
  41. if (contextPath) {
  42. try {
  43. templateContext = require(path.resolve(process.cwd(), contextPath))
  44. } catch (err) {
  45. console.error('Failed to get context from file ' + contextPath + '\n' + err)
  46. process.exit(1)
  47. }
  48. }
  49. let options
  50. const optionsPath = flags.options
  51. if (optionsPath) {
  52. try {
  53. options = require(path.resolve(process.cwd(), optionsPath))
  54. } catch (err) {
  55. console.error('Failed to get options from file ' + optionsPath + '\n' + err)
  56. process.exit(1)
  57. }
  58. }
  59. let stream
  60. try {
  61. stream = conventionalChangelogWriter(templateContext, options)
  62. } catch (err) {
  63. console.error(err.toString())
  64. process.exit(1)
  65. }
  66. function processFile (fileIndex) {
  67. const filePath = filePaths[fileIndex]
  68. fs.createReadStream(filePath)
  69. .on('error', function (err) {
  70. console.warn('Failed to read file ' + filePath + '\n' + err)
  71. if (++fileIndex < length) {
  72. processFile(fileIndex)
  73. }
  74. })
  75. .pipe(split(JSON.parse))
  76. .on('error', function (err) {
  77. console.warn('Failed to split commits in file ' + filePath + '\n' + err)
  78. })
  79. .pipe(stream)
  80. .on('error', function (err) {
  81. console.warn('Failed to process file ' + filePath + '\n' + err)
  82. if (++fileIndex < length) {
  83. processFile(fileIndex)
  84. }
  85. })
  86. .on('end', function () {
  87. if (++fileIndex < length) {
  88. processFile(fileIndex)
  89. }
  90. })
  91. .pipe(process.stdout)
  92. }
  93. if (!process.stdin.isTTY) {
  94. process.stdin
  95. .pipe(split(JSON.parse))
  96. .on('error', function (err) {
  97. console.error('Failed to split commits\n' + err)
  98. process.exit(1)
  99. })
  100. .pipe(stream)
  101. .on('error', function (err) {
  102. console.error('Failed to process file\n' + err)
  103. process.exit(1)
  104. })
  105. .pipe(process.stdout)
  106. } else if (length === 0) {
  107. console.error('You must specify at least one line delimited json file')
  108. process.exit(1)
  109. } else {
  110. processFile(0)
  111. }