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