-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
50 lines (42 loc) · 1.36 KB
/
main.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
import {Command} from 'commander'
import fs from 'fs'
import {readFile} from 'fs/promises'
const program = new Command()
const version = JSON.parse(
await readFile(new URL('./package.json', import.meta.url))
).version
program
.name('mdjl')
.description('Replace JIRA Issues with a link to them in a markdown file')
.version(version)
.command('replace', {isDefault: true})
.description('Replace JIRA Issues with a link to them in a markdown file')
.argument('<file>', 'The .md File')
.argument('<baseUrl>', 'The base issue url of your jira ')
.action(async (file, baseUrl) => {
if (!fs.existsSync(file)) {
console.log(`${file}does not exists.`)
process.exit(1)
}
const contents = await readFile(file, 'utf-8')
const jiraRegex = /([A-Z][A-Z0-9]+-[0-9]+)/g
const links = []
const newContents = contents
.split(/\r?\n/)
.map((line) => {
const matches = [...line.matchAll(jiraRegex)]
if (!matches.length) {
return line;
}
const issue = matches[0][0]
const trimmedUrl = baseUrl.replace(/\/$/, '')
links.push(`[${issue}]:${trimmedUrl}/${issue}`)
return line.replace(issue, '[' + issue + ']')
})
.join('\n')
.concat('\n')
.concat(links.join('\n'))
fs.unlinkSync(file)
fs.writeFileSync(file, newContents)
})
program.parseAsync()