Skip to content

Commit

Permalink
[internal/scripts] release-image script now accepts multiple packages…
Browse files Browse the repository at this point in the history
… and versions (#3139)

Co-authored-by: Augustine Kim <augustinekim@google.com>
  • Loading branch information
AndrewJakubowicz and augustjk committed Jul 18, 2022
1 parent afff4c1 commit 359860d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .changeset/small-doors-develop.md
@@ -0,0 +1,2 @@
---
---
103 changes: 78 additions & 25 deletions packages/internal-scripts/src/release-image.ts
Expand Up @@ -12,47 +12,92 @@ import {readFile} from 'fs/promises';
import path from 'path';

const optionDefinitions = [
{name: 'file', type: String, defaultOption: true},
{name: 'version', type: String},
{
name: 'files',
alias: 'f',
type: String,
defaultOption: true,
multiple: true,
},
{name: 'versions', alias: 'v', type: String, multiple: true},
];

/**
* A cache of the parsed changelogs such that a package may be referenced
* multiple times to render multiple versions.
*/
const CHANGELOG_CACHE = new Map<string, Changelog>();

export const run = async () => {
const options = commandLineArgs(optionDefinitions);

if (!options.file) {
if (
!options.files ||
!Array.isArray(options.files) ||
(Array.isArray(options.versions) &&
options.versions.length !== options.files.length)
) {
console.error(
`
USAGE
release-image CHANGELOG_PATH [--version VERSION]
release-image CHANGELOG_PATH
release-image (-f CHANGELOG_PATH [-v VERSION])...
EXAMPLES
To generate the release image for the reactive-element package:
release-image packages/reactive-element/CHANGELOG.md
For multiple packages in a single image:
release-image reactive-element/CHANGELOG.md lit-html/CHANGELOG.md
To generate an image composed of specific version numbers, including
multiple versions of the same package:
release-image -f reactive-element/CHANGELOG.md -v 3.2.0 \\
-f lit-html/CHANGELOG.md -v 2.0.1 \\
-f lit-html/CHANGELOG.md -v 2.0.0
`.trim()
);
process.exit(1);
}
const filename: string = options.file;
const version: string = options.version;

const packageJson = JSON.parse(
await readFile(path.join(path.dirname(filename), 'package.json'), {
encoding: 'utf-8',
})
);
const packageName = packageJson.name;

console.log(`Reading ${packageName} release ${version} from ${filename}`);
const changelog = (await parseChangelog({
filePath: filename,
removeMarkdown: false,
})) as Changelog;
const release = await getRelease(changelog, version);
if (release === undefined) {
throw new Error('no release found');
const releasesToRender: Release[] = [];
for (let i = 0; i < options.files.length; i++) {
const filename: string = options.files[i];
const version: string = (options.versions ?? [])[i];
let changelog = CHANGELOG_CACHE.get(filename);
if (!changelog) {
const packageJson = JSON.parse(
await readFile(path.join(path.dirname(filename), 'package.json'), {
encoding: 'utf-8',
})
);
const packageName = packageJson.name as string;

console.log(
`Reading ${packageName} release ${version ?? 'latest'} from ${filename}`
);
changelog = (await parseChangelog({
filePath: filename,
removeMarkdown: false,
})) as Changelog;
changelog.packageName = packageName;
CHANGELOG_CACHE.set(filename, changelog);
}
const release = await getRelease(changelog, version);
if (release === undefined) {
throw new Error('no release found');
}
// Fix the release fields since our CHANGELOG files result in `title`
// containing the version number.
release.version = release.title;
release.title = changelog.packageName;
releasesToRender.push(release);
}
const body = marked(release.body);

// colors taken from https://github.com/dracula/dracula-theme
const html = `
<!doctype html>
Expand Down Expand Up @@ -89,8 +134,13 @@ EXAMPLES
</style>
</head>
<body>
<h2><span class="name">${packageName}</span> ${release.title}</h2>
${body}
${releasesToRender
.map(
({title, body, version}) =>
`<h2><span class="name">${title}</span> ${version}</h2>
${marked(body)}`
)
.join('')}
</body>
</html>
`;
Expand All @@ -102,7 +152,7 @@ EXAMPLES
const bounds = (await page.evaluate(`
document.documentElement.getBoundingClientRect().toJSON()
`)) as DOMRect;
const imageFileName = `${path.basename(packageName)}-${release.title}.png`;
const imageFileName = `release.png`;
await page.screenshot({
path: imageFileName,
encoding: 'binary',
Expand Down Expand Up @@ -145,6 +195,9 @@ interface Release {
}

interface Changelog {
/** Name of the package, taken from package.json */
packageName: string;
/** parseChangelog populates this with "Change Log" */
title: string;
description: string;
versions: Array<Release>;
Expand Down

0 comments on commit 359860d

Please sign in to comment.