Generate CHANGELOG.json files and add convert_changelog script

This commit is contained in:
Fabio Berger
2018-03-29 14:06:29 +02:00
parent 9f1c212596
commit 743c3dbe01
22 changed files with 2045 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
[
{
"version": "0.1.13",
"changes": [
{
"note": "Add postpublish utils"
}
],
"timestamp": 1521327600,
"isPublished": true
}
]

View File

@@ -7,6 +7,7 @@
"scripts": {
"build:watch": "tsc -w",
"deps_versions": "node ./lib/deps_versions.js",
"convert_changelogs": "yarn build; node ./lib/convert_changelogs.js",
"lint": "tslint --project . 'src/**/*.ts'",
"clean": "shx rm -rf lib",
"build": "tsc"
@@ -24,6 +25,7 @@
"devDependencies": {
"@0xproject/tslint-config": "0.4.8",
"@types/glob": "^5.0.33",
"@types/moment": "2.13.0",
"@types/node": "^8.0.53",
"@types/rimraf": "^2.0.2",
"lerna-get-packages": "^1.0.0",
@@ -37,6 +39,7 @@
"es6-promisify": "^5.0.0",
"glob": "^7.1.2",
"lodash": "^4.17.4",
"moment": "2.13.0",
"promisify-child-process": "^1.0.5",
"publish-release": "0xproject/publish-release",
"rimraf": "^2.6.2",

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env node
import * as fs from 'fs';
import lernaGetPackages = require('lerna-get-packages');
import * as _ from 'lodash';
import * as moment from 'moment';
import * as path from 'path';
import { Changelog, Changes, UpdatedPackage } from './types';
import { utils } from './utils';
const MONOREPO_ROOT_PATH = path.join(__dirname, '../../..');
(async () => {
const allLernaPackages = lernaGetPackages(MONOREPO_ROOT_PATH);
const publicLernaPackages = _.filter(allLernaPackages, pkg => !pkg.package.private);
_.each(publicLernaPackages, lernaPackage => {
const changelogMdIfExists = getChangelogMdIfExists(lernaPackage.package.name, lernaPackage.location);
if (_.isUndefined(changelogMdIfExists)) {
throw new Error(`${lernaPackage.package.name} should have CHANGELOG.md b/c it's public. Add one.`);
}
const lines = (changelogMdIfExists as any).split('\n');
const changelogs: Changelog[] = [];
let changelog: Changelog = {
version: '',
changes: [],
};
for (const line of lines) {
if (_.startsWith(line, '## ')) {
const version = line.substr(4).split(' - ')[0];
const dateStr = line.split('_')[1];
let date;
if (!_.includes(dateStr, 'TBD')) {
date = moment(dateStr, 'MMMM D, YYYY');
}
changelog = {
version,
changes: [],
};
if (!_.isUndefined(date)) {
changelog.timestamp = date.unix();
}
if (!_.includes(dateStr, 'TBD')) {
changelog.isPublished = true;
}
(changelogs as any).push(changelog);
} else if (_.includes(line, '* ')) {
const note = line.split('* ')[1].split(' (#')[0];
const prChunk = line.split(' (#')[1];
let pr;
if (!_.isUndefined(prChunk)) {
pr = prChunk.split(')')[0];
}
const changes = {
note,
pr,
};
changelog.changes.push(changes);
}
}
const changelogJson = JSON.stringify(changelogs, null, '\t');
fs.writeFileSync(`${lernaPackage.location}/CHANGELOG.json`, changelogJson);
});
})().catch(err => {
utils.log(err.stdout);
process.exit(1);
});
function getChangelogMdIfExists(packageName: string, location: string): string | undefined {
const changelogPath = path.join(location, 'CHANGELOG.md');
let changelogMd: string;
try {
changelogMd = fs.readFileSync(changelogPath, 'utf-8');
return changelogMd;
} catch (err) {
// If none exists, create new, empty one.
return undefined;
}
}

View File

@@ -0,0 +1,17 @@
export interface UpdatedPackage {
name: string;
version: string;
private: boolean;
}
export interface Changes {
note: string;
pr?: number;
}
export interface Changelog {
timestamp?: number;
version: string;
changes: Changes[];
isPublished?: boolean;
}