Skip to content

Commit

Permalink
fix(changelog): date is not required (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe authored Jun 19, 2023
1 parent 6a8a7fd commit b7b1c4d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 25 deletions.
8 changes: 7 additions & 1 deletion src/transform/plugins/changelog/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ const collect = (input: string, {path: filepath, log, changelogs, extractChangel
}

if (rawChanges.length && changelogs && extractChangelogs) {
changelogs.push(...parseChangelogs(rawChanges.join('\n\n'), filepath));
const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath);
if (parsedChangelogs.length !== rawChanges.length) {
log.error(
`Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`,
);
}
changelogs.push(...parsedChangelogs);
}

return result;
Expand Down
17 changes: 8 additions & 9 deletions src/transform/plugins/changelog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import {bold} from 'chalk';
import yaml from 'js-yaml';
import StateCore from 'markdown-it/lib/rules_core/state_core';

interface Metadata {
date: string;
}

interface Options {
extractChangelogs?: boolean;
}
Expand Down Expand Up @@ -58,11 +54,14 @@ function parseBody(tokens: Token[], state: StateCore) {
if (metadataToken?.type !== 'fence') {
throw new Error('Metadata tag not found');
}
const rawMetadata = yaml.load(metadataToken.content) as Metadata;
const metadata = {
...rawMetadata,
date: new Date(rawMetadata.date).toISOString(),
};

let metadata: object = {};
const rawMetadata = yaml.load(metadataToken.content, {
schema: yaml.JSON_SCHEMA,
});
if (rawMetadata && typeof rawMetadata === 'object') {
metadata = rawMetadata;
}

if (!isTitle(tokens)) {
throw new Error('Title tag not found');
Expand Down
1 change: 0 additions & 1 deletion src/transform/plugins/changelog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export interface ChangelogItem {
ratio?: string;
};
description: string;
date: string;
[x: string]: unknown;
}
84 changes: 70 additions & 14 deletions test/changelog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,28 @@ import {Logger} from '../src/transform/log';
import {ChangelogItem} from '../src/transform/plugins/changelog/types';

describe('Changelog', () => {
function getItem(date: string | undefined, index: number | undefined) {
return {
date,
index,
storyId: 123321,
title: 'Change log title',
image: {
alt: 'My image',
ratio: 0.5625,
src: '../src/asd.png',
},
description: '<p>Change log payload</p>\n',
};
}

test('Should cut changelog', async () => {
expect.assertions(2);

const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8');
const data = await fs.promises.readFile(
path.join(__dirname, 'data/changelog/changelog.md'),
'utf8',
);

const {
result: {html, changelogs: logs},
Expand All @@ -24,10 +42,33 @@ describe('Changelog', () => {
expect(logs).toBe(undefined);
});

test('Should cut changelog and write it in env', async () => {
test('Should cut changelog with date and write it in env', async () => {
expect.assertions(2);

const data = await fs.promises.readFile(
path.join(__dirname, 'data/changelog/changelog_date.md'),
'utf8',
);

const {
result: {html, changelogs: logs},
} = transform(data, {
plugins: [changelogPlugin, imsize],
extractChangelogs: true,
});

expect(html).toBe(`<h1>Some changelog</h1>\n<p>After changelog</p>\n`);

expect(logs).toEqual(new Array(3).fill(getItem('2023-05-10', undefined)));
});

test('Should cut changelog with index and write it in env', async () => {
expect.assertions(2);

const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8');
const data = await fs.promises.readFile(
path.join(__dirname, 'data/changelog/changelog_index.md'),
'utf8',
);

const {
result: {html, changelogs: logs},
Expand All @@ -39,24 +80,39 @@ describe('Changelog', () => {
expect(html).toBe(`<h1>Some changelog</h1>\n<p>After changelog</p>\n`);

expect(logs).toEqual(
new Array(3).fill({
date: '2023-05-10T00:00:00.000Z',
storyId: 123321,
title: 'Change log title',
image: {
alt: 'My image',
ratio: 0.5625,
src: '../src/asd.png',
},
description: '<p>Change log payload</p>\n',
new Array(3).fill(getItem(undefined, 0)).map((item, index) => {
return {...item, index: 3 - index};
}),
);
});

test('Should cut changelog and write it in env', async () => {
expect.assertions(2);

const data = await fs.promises.readFile(
path.join(__dirname, 'data/changelog/changelog.md'),
'utf8',
);

const {
result: {html, changelogs: logs},
} = transform(data, {
plugins: [changelogPlugin, imsize],
extractChangelogs: true,
});

expect(html).toBe(`<h1>Some changelog</h1>\n<p>After changelog</p>\n`);

expect(logs).toEqual(new Array(3).fill(getItem(undefined, undefined)));
});

test('Should cut changelog and write it in variable', async () => {
expect.assertions(2);

const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8');
const data = await fs.promises.readFile(
path.join(__dirname, 'data/changelog/changelog.md'),
'utf8',
);

const changelogs: ChangelogItem[] = [];
const html = changelogCollect(data, {
Expand Down
37 changes: 37 additions & 0 deletions test/data/changelog/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Some changelog

{% changelog %}
```
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}

{% changelog %}
```
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}


{% changelog %}
```
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}

After changelog
File renamed without changes.
40 changes: 40 additions & 0 deletions test/data/changelog/changelog_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Some changelog

{% changelog %}
```
index: 3
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}

{% changelog %}
```
index: 2
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}


{% changelog %}
```
index: 1
storyId: 123321
```
# Change log title
![My image](../src/asd.png =16x9)

Change log payload

{% endchangelog %}

After changelog

0 comments on commit b7b1c4d

Please sign in to comment.