Skip to content

Commit

Permalink
feat(conventional-changelog-writer): timeZone option (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
takuminish committed Nov 13, 2023
1 parent 7e51c6d commit 27f3642
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
7 changes: 6 additions & 1 deletion packages/conventional-changelog-writer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Issue or pull request keyword in the url if `context.linkReferences === true`.
##### date
Type: `string` Default: formatted (`'yyyy-mm-dd'`) today's date in UTC timezone
Type: `string` Default: formatted (`'yyyy-mm-dd'`) today's date in timezone set by [`timeZone`](#timeZone) option.
If `version` is found in the last commit, `committerDate` will overwrite this.
Expand Down Expand Up @@ -298,6 +298,11 @@ Type: `object`
Partials that used in the main template, if any. The key should be the partial name and the value should be handlebars template strings. If you are using handlebars template files, read files by yourself.
##### timeZone
Type: `string` Default: `'UTC'`
The timezone to use. The date in the changelog is generated based on timezone.
## Customization Guide
Expand Down
14 changes: 8 additions & 6 deletions packages/conventional-changelog-writer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {

const dirname = fileURLToPath(new URL('.', import.meta.url))

// sv-SE is used for yyyy-mm-dd format
const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})
function formatDate (date, timeZone = 'UTC') {
// sv-SE is used for yyyy-mm-dd format
return Intl.DateTimeFormat('sv-SE', {
timeZone
}).format(date)
}

function immediate () {
return new Promise(resolve => setImmediate(resolve))
Expand All @@ -24,7 +26,7 @@ async function conventionalChangelogWriterInit (context, options) {
context = {
commit: 'commits',
issue: 'issues',
date: dateFormatter.format(new Date()),
date: formatDate(new Date(), options?.timeZone),
...context
}

Expand Down Expand Up @@ -78,7 +80,7 @@ async function conventionalChangelogWriterInit (context, options) {
return
}

return dateFormatter.format(new Date(date))
return formatDate(new Date(date), options?.timeZone)
},
...options.transform
}
Expand Down
72 changes: 49 additions & 23 deletions packages/conventional-changelog-writer/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { describe, it, expect } from 'vitest'
import { delay, throughObj } from '../../../tools/test-tools.js'
import conventionalChangelogWriter, { parseArray } from '../index.js'

// sv-SEis used for yyyy-mm-dd format
const dateFormatter = Intl.DateTimeFormat('sv-SE', {
timeZone: 'UTC'
})
const today = dateFormatter.format(new Date())
function formatDate (date, timeZone = 'UTC') {
// sv-SE is used for yyyy-mm-dd format
return Intl.DateTimeFormat('sv-SE', {
timeZone
}).format(date)
}

function getTodayDate (timeZone) {
return formatDate(new Date(), timeZone)
}

const todayUtc = getTodayDate()

const commits = [
{
hash: '9b1aff905b638aa274a5fc8f88662df446d374bd',
Expand Down Expand Up @@ -82,7 +90,7 @@ describe('conventional-changelog-writer', () => {

for await (let chunk of upstream.pipe(conventionalChangelogWriter())) {
chunk = chunk.toString()
expect(chunk).toBe('## (' + today + ')\n\n\n\n\n')
expect(chunk).toBe('## (' + todayUtc + ')\n\n\n\n\n')
i++
}

Expand Down Expand Up @@ -180,7 +188,7 @@ describe('conventional-changelog-writer', () => {
expect(context).toEqual({
commit: 'commits',
issue: 'issues',
date: today
date: todayUtc
})
called = true
return commit
Expand All @@ -193,7 +201,7 @@ describe('conventional-changelog-writer', () => {
expect(context).toEqual({
commit: 'commits',
issue: 'issues',
date: today
date: todayUtc
})

return commit
Expand Down Expand Up @@ -283,15 +291,15 @@ describe('conventional-changelog-writer', () => {
}
})

expect(changelog).toBe('## (' + today + ')\n\n\n\n\n')
expect(changelog).toBe('## (' + todayUtc + ')\n\n\n\n\n')

for await (let chunk of getStream().pipe(conventionalChangelogWriter({}, {
transform () {
return false
}
}))) {
chunk = chunk.toString()
expect(chunk).toBe('## (' + today + ')\n\n\n\n\n')
expect(chunk).toBe('## (' + todayUtc + ')\n\n\n\n\n')

i++
}
Expand Down Expand Up @@ -400,7 +408,7 @@ describe('conventional-changelog-writer', () => {
let i = 0
const changelog = await parseArray(commits)

expect(changelog).toContain('## (' + today)
expect(changelog).toContain('## (' + todayUtc)
expect(changelog).toContain('feat(scope): ')
expect(changelog).toContain('## <small>1.0.1 (2015-04-07)</small>')
expect(changelog).toContain('fix(ng-list): ')
Expand All @@ -411,7 +419,7 @@ describe('conventional-changelog-writer', () => {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).toContain('## (' + today)
expect(chunk).toContain('## (' + todayUtc)
expect(chunk).toContain('feat(scope): ')

expect(chunk).not.toContain('fix(ng-list): ')
Expand Down Expand Up @@ -483,7 +491,7 @@ describe('conventional-changelog-writer', () => {
generateOn: 'version'
})

expect(changelog).toContain('## (' + today)
expect(changelog).toContain('## (' + todayUtc)
expect(changelog).toContain('feat(scope): broadcast $destroy event on scope destruction')
expect(changelog).not.toContain('<a name=""></a>')
expect(changelog).toContain('fix(ng-list): Allow custom separator')
Expand All @@ -497,7 +505,7 @@ describe('conventional-changelog-writer', () => {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).toContain('## (' + today)
expect(chunk).toContain('## (' + todayUtc)

expect(chunk).not.toContain('## 1.0.1 (2015-04-07)')
} else if (i === 1) {
Expand Down Expand Up @@ -532,7 +540,7 @@ describe('conventional-changelog-writer', () => {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).toContain('## (' + today)
expect(chunk).toContain('## (' + todayUtc)
expect(chunk).not.toContain('## 1.0.1 (2015-04-07)')
}

Expand All @@ -550,7 +558,7 @@ describe('conventional-changelog-writer', () => {
}))) {
chunk = chunk.toString()

expect(chunk).toContain('## (' + today)
expect(chunk).toContain('## (' + todayUtc)

i++
}
Expand Down Expand Up @@ -598,7 +606,7 @@ describe('conventional-changelog-writer', () => {
chunk = chunk.toString()

if (i === 0) {
expect(chunk).toBe('## (' + today + ')\n\n\n\n\n')
expect(chunk).toBe('## (' + todayUtc + ')\n\n\n\n\n')
} else {
expect(chunk).toBe('## <small>1.0.1 (2015-04-07 15:00:44 +1000)</small>\n\n\n\n\n')
}
Expand All @@ -616,7 +624,7 @@ describe('conventional-changelog-writer', () => {
includeDetails: true
}))) {
if (i === 0) {
expect(chunk.log).toContain('## (' + today + ')\n\n')
expect(chunk.log).toContain('## (' + todayUtc + ')\n\n')
expect(chunk.log).toContain('feat(scope): broadcast $destroy event on scope destruction')
expect(chunk.keyCommit).toBe()
} else {
Expand Down Expand Up @@ -784,7 +792,7 @@ describe('conventional-changelog-writer', () => {
## (xxxx-xx-xx)`).replace('xxxx-xx-xx', today))
## (xxxx-xx-xx)`).replace('xxxx-xx-xx', todayUtc))

for await (let chunk of upstream.pipe(conventionalChangelogWriter({}, {
reverse: true
Expand All @@ -807,7 +815,7 @@ describe('conventional-changelog-writer', () => {
expect(chunk).toContain('perf(template): ')
expect(chunk).toContain('refactor(name): ')
} else if (i === 3) {
expect(chunk).toContain('## (' + today)
expect(chunk).toContain('## (' + todayUtc)
}

i++
Expand All @@ -830,7 +838,7 @@ describe('conventional-changelog-writer', () => {
if (i === 0) {
expect(chunk).toBe('## <small>1.0.1 (2015-04-07 15:00:44 +1000)</small>\n\n\n\n\n')
} else {
expect(chunk).toBe('## (' + today + ')\n\n\n\n\n')
expect(chunk).toBe('## (' + todayUtc + ')\n\n\n\n\n')
}

i++
Expand All @@ -839,6 +847,24 @@ describe('conventional-changelog-writer', () => {
expect(i).toBe(2)
})

it('should generated date from timeZone of the option', async () => {
let i = 0

for await (const chunk of getStream().pipe(conventionalChangelogWriter({}, {
timeZone: 'America/New_York',
transform () {
return false
}
}))) {
if (i === 0) {
expect(chunk).toBe('## (' + getTodayDate('America/New_York') + ')\n\n\n\n\n')
}
i++
}

expect(i).toBe(2)
})

it('should include details', async () => {
let i = 0

Expand All @@ -853,7 +879,7 @@ describe('conventional-changelog-writer', () => {
expect(chunk.keyCommit.version).toBe('1.0.1')
expect(chunk.keyCommit.committerDate).toBe('2015-04-07')
} else {
expect(chunk.log).toContain('## (' + today + ')\n\n')
expect(chunk.log).toContain('## (' + todayUtc + ')\n\n')
expect(chunk.log).toContain('perf(template): tweak')
expect(chunk.log).toContain('refactor(name): rename this module to conventional-changelog-writer')
expect(chunk.keyCommit).toBe()
Expand Down Expand Up @@ -960,7 +986,7 @@ describe('conventional-changelog-writer', () => {
upstream.end()

for await (const chunk of upstream.pipe(conventionalChangelogWriter())) {
expect(chunk.toString()).toBe('## (' + today + ')\n\n* bla\n\n\n\n')
expect(chunk.toString()).toBe('## (' + todayUtc + ')\n\n* bla\n\n\n\n')
i++
}

Expand Down

0 comments on commit 27f3642

Please sign in to comment.