-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·103 lines (82 loc) · 2.81 KB
/
index.js
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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { readPackageUpSync } from 'read-pkg-up';
import githubUrlFromGit from 'github-url-from-git';
import open from 'open';
import timestamp from 'time-stamp';
import startServer from './server/index.js';
import githubNumberToLink from './lib/github-number-to-link.js';
import textToHeading from './lib/text-to-heading.js';
const pkg = readPackageUpSync();
const github = githubUrlFromGit(pkg.packageJson.repository.url);
if (!github) {
console.error('Error: Must have GitHub repository in package.json');
process.exit(1);
}
// TODO: Allow HISTORY.md, etc.
const changelogPath = path.join(pkg.path, '../CHANGELOG.md');
let changelogContents = '';
try {
changelogContents = fs.readFileSync(changelogPath, 'utf8');
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(err);
process.exit(1);
}
}
const headingType = getHeadingType(changelogContents);
const port = startServer({ handleData, json2markdown });
const url = `http://localhost:${port}/#${pkg.packageJson.version}`;
console.log(`Server running on ${url}`);
console.log(
"Your browser should open shortly; if it doesn't, click on the link above",
);
open(url);
function handleData(data) {
console.log();
if (!changelogContents) console.log(`Creating new file: ${changelogPath}`);
else console.log(`Prepending to ${changelogPath}`);
console.log();
console.log(ensureTrailingNewline(data));
fs.writeFile(
changelogPath,
ensureTrailingNewline(data + changelogContents),
(err) => err && console.error(err),
);
}
function json2markdown(data) {
const { changes, version } = data;
const heading = textToHeading(`${version} / ${timestamp()}`, headingType);
const list = !changes.length
? ''
: `- ${changes.map(changeToString).join('\n- ')}\n\n`;
return heading + list;
}
function getHeadingType(markdown) {
let headingType = { level: 1, style: 'normal' };
const lines = markdown.split('\n');
// match pound sign headings
const normalHeadingMatch = lines[0].match(
/(#{1,6}) \d+(\.\d+){2}.* \/ \d{4}(-\d{2}){2}/,
);
if (normalHeadingMatch) headingType.level = normalHeadingMatch[1].length;
// match underline headings
if (lines[0].match(/\d+(\.\d+){2}.* \/ \d{4}(-\d{2}){2}/)) {
if (lines[1].indexOf('=') === 0) headingType.style = 'alt';
if (lines[1].indexOf('-') === 0) headingType = { level: 2, style: 'alt' };
}
return headingType;
}
function changeToString(change) {
let str = change.description.trim();
const arr = [];
if (change.issue) arr.push(githubNumberToLink(github, 'issue', change.issue));
if (change.pr) arr.push(githubNumberToLink(github, 'pr', change.pr));
const numbers = arr.join(', ');
if (numbers) str += ` (${numbers})`;
return str;
}
function ensureTrailingNewline(text) {
return `${text.trim()}\n`;
}