Skip to content

Commit 4e68725

Browse files
committed
add script to draft new changelog based on draftlogs
1 parent 7154fa4 commit 4e68725

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
"stats": "node tasks/stats.js",
2828
"find-strings": "node tasks/find_locale_strings.js",
2929
"preprocess": "node tasks/preprocess.js",
30-
"build": "node tasks/empty_dist.js && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles && npm run stats",
31-
"cibuild": "node tasks/empty_dist.js && npm run preprocess && node tasks/cibundle.js",
30+
"use-draftlogs": "node tasks/use_draftlogs.js",
31+
"empty-draftlogs": "node tasks/empty_draftlogs.js",
32+
"empty-dist": "node tasks/empty_dist.js",
33+
"build": "npm run empty-dist && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles && npm run stats",
34+
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.js",
3235
"watch": "node tasks/watch.js",
3336
"lint": "eslint --version && eslint .",
3437
"lint-fix": "eslint . --fix || true",

tasks/empty_draftlogs.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var constants = require('./util/constants');
2+
var makeEmptyDirectory = require('./util/make_empty_directory');
3+
var emptyDir = makeEmptyDirectory.emptyDir;
4+
var makeDir = makeEmptyDirectory.makeDir;
5+
6+
var pathToDraftlogs = constants.pathToDraftlogs;
7+
8+
var chZero = '0'.charCodeAt(0);
9+
var chNine = '9'.charCodeAt(0);
10+
11+
function startsWithNumber(v) {
12+
var ch = v.charCodeAt(0);
13+
return chZero <= ch && ch <= chNine;
14+
}
15+
16+
// main
17+
emptyDir(pathToDraftlogs, { filter: startsWithNumber });
18+
makeDir(pathToDraftlogs);

tasks/use_draftlogs.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var constants = require('./util/constants');
4+
5+
var pathToDraftlogs = constants.pathToDraftlogs;
6+
var pathToChangelog = constants.pathToChangelog;
7+
8+
var chZero = '0'.charCodeAt(0);
9+
var chNine = '9'.charCodeAt(0);
10+
11+
function startsWithNumber(v) {
12+
var ch = v.charCodeAt(0);
13+
return chZero <= ch && ch <= chNine;
14+
}
15+
16+
var allLogs = fs.readdirSync(pathToDraftlogs).filter(startsWithNumber);
17+
18+
var len = allLogs.length;
19+
if(!len) return;
20+
21+
var writeAfterMe = 'where X.Y.Z is the semver of most recent plotly.js release.';
22+
var changelog = fs.readFileSync(pathToChangelog).toString().split(writeAfterMe);
23+
var head = changelog[0];
24+
var foot = changelog[1];
25+
26+
var all = {
27+
Added: [],
28+
Removed: [],
29+
Deprecated: [],
30+
Changed: [],
31+
Fixed: []
32+
};
33+
34+
var ENTER = '\n';
35+
36+
for(var i = 0; i < len; i++) {
37+
var filename = allLogs[i];
38+
var message = fs.readFileSync(path.join(pathToDraftlogs, filename), { encoding: 'utf-8' }).toString();
39+
// trim empty lines
40+
message = message.split(ENTER).filter(function(e) { return !!e; }).join(ENTER);
41+
42+
if(filename.endsWith('_add.md')) {
43+
all.Added.push(message);
44+
} else if(filename.endsWith('_remove.md')) {
45+
all.Removed.push(message);
46+
} else if(filename.endsWith('_deprecate.md')) {
47+
all.Deprecated.push(message);
48+
} else if(filename.endsWith('_change.md')) {
49+
all.Changed.push(message);
50+
} else if(filename.endsWith('_fix.md')) {
51+
all.Fixed.push(message);
52+
}
53+
}
54+
55+
var draftNewChangelog = [
56+
head + writeAfterMe,
57+
'',
58+
'## [X.Y.Z] -- UNRELEASED'
59+
];
60+
61+
var append = function(key) {
62+
var newMessages = all[key];
63+
if(!newMessages.length) return;
64+
draftNewChangelog.push('');
65+
draftNewChangelog.push('### ' + key);
66+
draftNewChangelog.push(newMessages.join(ENTER));
67+
};
68+
69+
append('Added');
70+
append('Removed');
71+
append('Deprecated');
72+
append('Changed');
73+
append('Fixed');
74+
75+
draftNewChangelog.push(foot);
76+
77+
fs.writeFileSync(pathToChangelog, draftNewChangelog.join(ENTER), { encoding: 'utf-8' });

tasks/util/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var pathToSrc = path.join(pathToRoot, 'src/');
77
var pathToLib = path.join(pathToRoot, 'lib/');
88
var pathToImageTest = path.join(pathToRoot, 'test/image');
99
var pathToStrictD3Module = path.join(pathToRoot, 'test/strict-d3.js');
10+
var pathToDraftlogs = path.join(pathToRoot, 'draftlogs/');
1011
var pathToDist = path.join(pathToRoot, 'dist/');
1112
var pathToBuild = path.join(pathToRoot, 'build/');
1213

@@ -168,6 +169,8 @@ module.exports = {
168169
pathToLib: pathToLib,
169170
pathToBuild: pathToBuild,
170171
pathToDist: pathToDist,
172+
pathToDraftlogs: pathToDraftlogs,
173+
pathToChangelog: path.join(pathToRoot, 'CHANGELOG.md'),
171174

172175
partialBundleTraces: partialBundleTraces,
173176

tasks/util/make_empty_directory.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ module.exports = {
77
makeDir: makeDir
88
};
99

10-
function emptyDir(dir) {
10+
function emptyDir(dir, opts) {
1111
if(common.doesDirExist(dir)) {
1212
console.log('empty ' + dir);
1313
try {
1414
var allFiles = fs.readdirSync(dir);
15+
var hasFilter = false;
16+
if(opts && opts.filter) {
17+
hasFilter = true;
18+
allFiles = allFiles.filter(opts.filter);
19+
}
20+
1521
allFiles.forEach(function(file) {
1622
// remove file
1723
fs.unlinkSync(path.join(dir, file));
1824
});
1925

20-
fs.rmdirSync(dir);
26+
if(!hasFilter) {
27+
fs.rmdirSync(dir);
28+
}
2129
} catch(err) {
2230
console.error(err);
2331
}

0 commit comments

Comments
 (0)