1 | #!/usr/bin/env node
|
2 | 'use strict'
|
3 | const conventionalChangelogWriter = require('./')
|
4 | const fs = require('fs')
|
5 | const meow = require('meow')
|
6 | const path = require('path')
|
7 | const split = require('split')
|
8 |
|
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 |
|
34 | const flags = cli.flags
|
35 | const filePaths = cli.input
|
36 | const length = filePaths.length
|
37 |
|
38 | let templateContext
|
39 | const contextPath = flags.context
|
40 | if (contextPath) {
|
41 | try {
|
42 | templateContext = require(path.resolve(process.cwd(), contextPath))
|
43 | } catch (err) {
|
44 | console.error('Failed to get context from file ' + contextPath + '\n' + err)
|
45 | process.exit(1)
|
46 | }
|
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 |
|
60 | let stream
|
61 | try {
|
62 | stream = conventionalChangelogWriter(templateContext, options)
|
63 | } catch (err) {
|
64 | console.error(err.toString())
|
65 | process.exit(1)
|
66 | }
|
67 |
|
68 | function processFile (fileIndex) {
|
69 | const filePath = filePaths[fileIndex]
|
70 | fs.createReadStream(filePath)
|
71 | .on('error', function (err) {
|
72 | console.warn('Failed to read file ' + filePath + '\n' + err)
|
73 | if (++fileIndex < length) {
|
74 | processFile(fileIndex)
|
75 | }
|
76 | })
|
77 | .pipe(split(JSON.parse))
|
78 | .on('error', function (err) {
|
79 | console.warn('Failed to split commits in file ' + filePath + '\n' + err)
|
80 | })
|
81 | .pipe(stream)
|
82 | .on('error', function (err) {
|
83 | console.warn('Failed to process file ' + filePath + '\n' + err)
|
84 | if (++fileIndex < length) {
|
85 | processFile(fileIndex)
|
86 | }
|
87 | })
|
88 | .on('end', function () {
|
89 | if (++fileIndex < length) {
|
90 | processFile(fileIndex)
|
91 | }
|
92 | })
|
93 | .pipe(process.stdout)
|
94 | }
|
95 |
|
96 | if (!process.stdin.isTTY) {
|
97 | process.stdin
|
98 | .pipe(split(JSON.parse))
|
99 | .on('error', function (err) {
|
100 | console.error('Failed to split commits\n' + err)
|
101 | process.exit(1)
|
102 | })
|
103 | .pipe(stream)
|
104 | .on('error', function (err) {
|
105 | console.error('Failed to process file\n' + err)
|
106 | process.exit(1)
|
107 | })
|
108 | .pipe(process.stdout)
|
109 | } else if (length === 0) {
|
110 | console.error('You must specify at least one line delimited json file')
|
111 | process.exit(1)
|
112 | } else {
|
113 | processFile(0)
|
114 | }
|