Skip to content

Commit dc4b41f

Browse files
Refactor preview and image utilities to use Config for improved parameter handling, extract getReadme into helper, and update tests/snapshots
1 parent 222dac5 commit dc4b41f

8 files changed

Lines changed: 74 additions & 66 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "preview-updater",
3-
"description": "Lightweight preview update in Your repository",
3+
"description": "Lightweight preview update in your repository",
44
"author": "Andrey Helldar <helldar@dragon-code.pro>",
55
"main": "./dist/index.js",
66
"scripts": {

src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ImageParameters
1616
export interface Image
1717
{
1818
url: string;
19-
parameters?: ImageParameters;
19+
parameters: ImageParameters;
2020
}
2121

2222
export interface Config

src/utils/image.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Image, ImageParameters } from '../types/config'
1+
import { Config, ImageParameters } from '../types/config'
22
import { hasComposer, hasNpm, hasYarn } from './packageManagers'
33

44
const encodeUri = (value: string): string => {
@@ -9,63 +9,64 @@ const encodeUri = (value: string): string => {
99
return encodeURIComponent(value)
1010
}
1111

12-
const detectPackageManager = (visibility: string): string => {
13-
if (hasComposer()) {
12+
const detectPackageManager = (config: Config, visibility: string): string => {
13+
if (hasComposer(config)) {
1414
return `composer${ visibility } require`
1515
}
1616

17-
if (hasNpm()) {
17+
if (hasNpm(config)) {
1818
return `npm${ visibility } install`
1919
}
2020

21-
if (hasYarn()) {
21+
if (hasYarn(config)) {
2222
return `yarn${ visibility } add`
2323
}
2424

2525
return ''
2626
}
2727

28-
const packageManager = (image: ImageParameters): string => {
29-
if (image.packageManager === 'none') {
30-
return ''
31-
}
28+
const packageManager = (config: Config): string => {
3229

33-
const visibility = image.packageGlobal ? ' global' : ''
30+
const visibility = config.image.parameters.packageGlobal ? ' global' : ''
3431

35-
switch (image.packageManager) {
32+
switch (config.image.parameters.packageManager) {
3633
case 'composer':
3734
return `composer${ visibility } require`
3835
case 'npm':
3936
return `npm${ visibility } install`
4037
case 'yarn':
4138
return `yarn${ visibility } add`
4239
case 'auto':
43-
return detectPackageManager(visibility)
40+
return detectPackageManager(config, visibility)
41+
default:
42+
return ''
4443
}
4544
}
4645

4746
const packageName = (image: ImageParameters): string => image.packageManager !== 'none' ? image.packageName : ''
4847

49-
const render = (image: Image, theme: 'light' | 'dark', suffix: string = ''): string => {
48+
const render = (config: Config, theme: 'light' | 'dark', suffix: string = ''): string => {
49+
const image = config.image.parameters
50+
5051
const params = new URLSearchParams({
5152
theme: theme,
52-
pattern: image.parameters.pattern,
53-
style: image.parameters.style,
54-
fontSize: image.parameters.fontSize,
55-
images: image.parameters.icon,
56-
packageManager: encodeUri(packageManager(image.parameters)),
57-
packageName: encodeUri(packageName(image.parameters)),
58-
description: encodeUri(image.parameters.description)
53+
pattern: image.pattern,
54+
style: image.style,
55+
fontSize: image.fontSize,
56+
images: image.icon,
57+
packageManager: packageManager(config),
58+
packageName: packageName(image),
59+
description: image.description
5960
})
6061

61-
return image.url + '/' + encodeUri(image.parameters.title) + '.png?' + params.toString() + suffix
62+
return config.image.url.replace('{title}', encodeUri(image.title)) + '?' + params.toString() + suffix
6263
}
6364

6465
const format = (title: string, url: string): string => `![${ title }](${ url })`
6566

66-
export const getImages = (image: Image): string[] => {
67-
const light = format(image.parameters.title, render(image, 'light', '#gh-light-mode-only'))
68-
const dark = format(image.parameters.title, render(image, 'dark', '#gh-dark-mode-only'))
67+
export const getImages = (config: Config): string[] => {
68+
const light = format(config.image.parameters.title, render(config, 'light', '#gh-light-mode-only'))
69+
const dark = format(config.image.parameters.title, render(config, 'dark', '#gh-dark-mode-only'))
6970

7071
return [light, dark]
7172
}

src/utils/preview.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { getImages } from './image'
2-
import { Image } from '../types/config'
2+
import { Config } from '../types/config'
33

44
const hasHeader = (content: string) => content.match(/^#\s+/)
55

6-
const cleanUp = (content: string) => content
6+
const cleanUp = (content: string): string => content
77
.replace(/^(#\s+.+\n+)(!\[.+]\(.*\)\n?){1,2}\n?/, '$1\n')
88
.replace(/^(#\s+.+\n+)(<img\s.*\/>\n?){1,2}\n?/, '$1\n')
99

1010
const titleCase = (title: string) => title
11-
.replace(/[A-Z]/g, ' $1')
11+
.replace(/([A-Z])/g, '$1')
1212
.toLowerCase()
1313
.replace(/(^|\s|-|_)\S/g, (match: string) => match.toUpperCase())
1414
.replace(/[-_]/g, ' ')
1515

16-
export const setPreview = (content: string, repositoryName: string, image: Image) => {
16+
export const setPreview = (content: string, config: Config) => {
1717
if (! hasHeader(content)) {
18-
const title = titleCase(repositoryName)
18+
const title = titleCase(config.image.parameters.title)
1919

2020
content = `# ${ title }\n\n${ content }`
2121
}
2222

23-
const images = getImages(image).join('\n')
23+
const images = getImages(config).join('\n')
2424

25-
return cleanUp(content).replace(/^(#\s+.+\n\n)/, `$1${ images }\n\n`)
25+
return cleanUp(content).replace(/^(#\s+.+\n\n)/, '$1' + images + '\n\n')
2626
}

tests/helpers/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@ import { Config, defaultConfig } from '../../src/types/config'
22
import { deepmerge } from 'deepmerge-ts'
33

44
export const testConfig: Config = <Config>deepmerge(defaultConfig, {
5-
directory: process.cwd()
5+
directory: process.cwd(),
6+
7+
image: {
8+
parameters: {
9+
packageName: 'TheDragonCode/preview-updater',
10+
title: 'Preview Updater',
11+
description: 'Lightweight preview update in your repository'
12+
}
13+
}
614
})

tests/helpers/filesystem.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { readFile } from '../../src/utils/filesystem'
2+
import { setPreview } from '../../src/utils/preview'
3+
import { testConfig } from './config'
4+
5+
export const getReadme = (filename: string): string => {
6+
const content = readFile(testConfig, 'tests/fixtures/readme/' + filename)
7+
8+
return setPreview(content, testConfig)
9+
}

tests/unit/__snapshots__/preview.test.ts.snap

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`empty 1`] = `
4-
"# $1werty
4+
"# Preview Updater
55
6-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
7-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
6+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
7+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
88
99
"
1010
`;
1111

1212
exports[`just-text 1`] = `
13-
"# $1werty
13+
"# Preview Updater
1414
15-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
16-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
15+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
16+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
1717
1818
Something
1919
"
@@ -22,8 +22,8 @@ Something
2222
exports[`with-one-image 1`] = `
2323
"# Preview Updater
2424
25-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
26-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
25+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
26+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
2727
2828
2929
## Usage
@@ -33,10 +33,10 @@ Something
3333
`;
3434

3535
exports[`with-one-image-without-header 1`] = `
36-
"# $1werty
36+
"# Preview Updater
3737
38-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
39-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
38+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
39+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
4040
4141
4242
## Usage
@@ -48,8 +48,8 @@ Something
4848
exports[`with-two-images 1`] = `
4949
"# Preview Updater
5050
51-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
52-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
51+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
52+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
5353
5454
5555
## Usage
@@ -59,10 +59,10 @@ Something
5959
`;
6060

6161
exports[`with-two-images-without-header 1`] = `
62-
"# $1werty
62+
"# Preview Updater
6363
64-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
65-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
64+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
65+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
6666
6767
6868
## Usage
@@ -72,10 +72,10 @@ Something
7272
`;
7373

7474
exports[`without-all 1`] = `
75-
"# $1werty
75+
"# Preview Updater
7676
77-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
78-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
77+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
78+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
7979
8080
## Usage
8181
@@ -86,8 +86,8 @@ Something
8686
exports[`without-images 1`] = `
8787
"# Preview Updater
8888
89-
![](https://banners.beyondco.de/{title}.png/.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-light-mode-only)
90-
![](https://banners.beyondco.de/{title}.png/.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm%2520install&packageName=&description=#gh-dark-mode-only)
89+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=light&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-light-mode-only)
90+
![Preview Updater](https://banners.beyondco.de/Preview%20Updater.png?theme=dark&pattern=topography&style=style_2&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg&packageManager=npm+install&packageName=TheDragonCode%2Fpreview-updater&description=Lightweight+preview+update+in+your+repository#gh-dark-mode-only)
9191
9292
## Usage
9393

tests/unit/preview.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import { setPreview } from '../../src/utils/preview'
2-
import { readFile } from '../../src/utils/filesystem'
3-
import { defaultConfig } from '../../src/types/config'
4-
5-
const getReadme = (filename: string): string => {
6-
const content = readFile(defaultConfig, 'tests/fixtures/readme/' + filename)
7-
8-
console.log(content)
9-
10-
return setPreview(content, 'Qwerty', defaultConfig.image)
11-
}
1+
import { getReadme } from '../helpers/filesystem'
122

133
test('empty', () => expect(getReadme('empty.md')).toMatchSnapshot())
144
test('just-text', () => expect(getReadme('just-text.md')).toMatchSnapshot())

0 commit comments

Comments
 (0)