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