From 743c3dbe01b3ffaa1743c2907cbcab1d7ed78d72 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 29 Mar 2018 14:06:29 +0200 Subject: Generate CHANGELOG.json files and add convert_changelog script --- .../monorepo-scripts/src/convert_changelogs.ts | 80 ++++++++++++++++++++++ packages/monorepo-scripts/src/types.ts | 17 +++++ 2 files changed, 97 insertions(+) create mode 100644 packages/monorepo-scripts/src/convert_changelogs.ts create mode 100644 packages/monorepo-scripts/src/types.ts (limited to 'packages/monorepo-scripts/src') diff --git a/packages/monorepo-scripts/src/convert_changelogs.ts b/packages/monorepo-scripts/src/convert_changelogs.ts new file mode 100644 index 000000000..419f36ff4 --- /dev/null +++ b/packages/monorepo-scripts/src/convert_changelogs.ts @@ -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; + } +} diff --git a/packages/monorepo-scripts/src/types.ts b/packages/monorepo-scripts/src/types.ts new file mode 100644 index 000000000..045316ab5 --- /dev/null +++ b/packages/monorepo-scripts/src/types.ts @@ -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; +} -- cgit v1.2.3