diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/package.json b/package.json new file mode 100644 index 00000000..fff190a5 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "badge-generator", + "description": "Online tool to help you quickly generate tailor-made badges/shields for your repo docs and learn to work with badges", + "main": "main.js", + "scripts": { + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MichaelCurrin/badge-generator.git" + }, + "author": "Michael Currin", + "license": "MIT", + "bugs": { + "url": "https://github.com/MichaelCurrin/badge-generator/issues" + }, + "homepage": "https://github.com/MichaelCurrin/badge-generator#readme", + "devDependencies": { + "jest": "^26.0.1" + } +} \ No newline at end of file diff --git a/src/__tests__/external-repo-badge.test.js b/src/__tests__/external-repo-badge.test.js new file mode 100644 index 00000000..e69de29b diff --git a/src/__tests__/license.test.js b/src/__tests__/license.test.js new file mode 100644 index 00000000..f68fd9dc --- /dev/null +++ b/src/__tests__/license.test.js @@ -0,0 +1,34 @@ +const { License } = require('../components/license.js') + +test('MIT license badge title displays correctly', () => { + var license = new License('MIT') + + expect(license.title()).toBe('License: MIT') +}) + +test('MIT license badge image is correct', () => { + var license = new License('MIT') + + expect(license.img()).toBe('https://img.shields.io/badge/License-MIT-blue') +}) + +test('MIT license badge relative target is correct', () => { + var license = new License('MIT') + + expect(license.REL_TARGET).toBe('#license') +}) + +test('MIT license full badge with relative target displays correctly', () => { + var license = new License('MIT') + + var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](#license)'; + expect(license.markdown()).toBe(badge) +}) + +test('MIT license full badge with FQDN target displays correctly', () => { + var target = 'https://github.com/my-user/my-repo-name/blob/master/LICENSE'; + var license = new License('MIT', target) + + var badge = '[![License: MIT](https://img.shields.io/badge/License-MIT-blue)](https://github.com/my-user/my-repo-name/blob/master/LICENSE)'; + expect(license.markdown()).toBe(badge) +}) diff --git a/src/__tests__/made-with-badge.test.js b/src/__tests__/made-with-badge.test.js new file mode 100644 index 00000000..c35cf741 --- /dev/null +++ b/src/__tests__/made-with-badge.test.js @@ -0,0 +1,6 @@ +test('Made with Bash badge is correct', () => { + var badge = new MadeWith('Bash') + + // Use mix of shield and markdown links internally. And preset target. + var result = '[![Made with Bash](https://img.shields.io/badge/Made_with-Bash-blue.svg)](https://www.gnu.org/software/bash/)' +}) diff --git a/src/__tests__/markdown-link.test.js b/src/__tests__/markdown-link.test.js new file mode 100644 index 00000000..d61a5e50 --- /dev/null +++ b/src/__tests__/markdown-link.test.js @@ -0,0 +1,36 @@ +const { MarkdownLink } = require('../components/markdownLink.js') + +test('An ID markdown link formats correctly', () => { + var tag = MarkdownLink('My link', '#target') + expect(tag.md()).toBe('[My link](#target)') +}) + +test('An absolute markdown link formats correctly', () => { + var tag = MarkdownLink('Absolute link', '/foo/bar.md') + expect(tag.md()).toBe('[Absolute link](/foo/bar.md)') +}) + +test('A markdown URL link formats correctly', () => { + var tag = MarkdownLink('Example link', 'https://example.com') + expect(tag.md()).toBe('[Example link](https://example.com)') +}) + +test('A markdown image tag formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png') + expect(tag.image()).toBe('![Example image](https://example.com/image.png)') +}) + +test('A markdown image tag with target URL formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com') + expect(tag.image()).toBe('[![Example image](https://example.com/image.png)](https://foo.bar.com)') +}) + +test('An ID markdown link with alt text formats correctly', () => { + var tag = MarkdownLink('My link', '#target') + expect(tag.md("My alt test for hover")).toBe('[My link](#target "My alt test for hover")') +}) + +test('A markdown image tag with alt text and target URL formats correctly', () => { + var tag = MarkdownLink('Example image', 'https://example.com/image.png', 'https://foo.bar.com') + expect(tag.image("My alt test for hover")).toBe('[![Example image](https://example.com/image.png "My alt test for hover")](https://foo.bar.com)') +}) diff --git a/src/__tests__/repo.test.js b/src/__tests__/repo.test.js new file mode 100644 index 00000000..65bce9f6 --- /dev/null +++ b/src/__tests__/repo.test.js @@ -0,0 +1,78 @@ +const { parseRepoUrl, Repo } = require('../components/repo.js') + +test("A repo URL can be split correctly", () => { + var url = 'https://github.com/my-username/my-repo-name' + + expect(parseRepoUrl(url)).toBe({ + username: 'my-username', + repoName: 'my-repo-name' + }) +}) + +test("Create a repo instance", () => { + var repo = new Repo('my-username', 'my-repo-name') + + expect(repo.username).toBe('my-username') + expect(repo.repoName).toBe('my-repo-name') +}) + +test("Create a repo instance by key-value pairs", () => { + var repo = new Repo({ username: 'my-username', repoName: 'my-repo-name' }) + + expect(repo.username).toBe('my-username') + expect(repo.repoName).toBe('my-repo-name') +}) + +test('Get Github URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.githubUrl()).toBe('https://github.com/my-user/my-repo-name') +}) + +test('Get repo-based Github Pages URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/my-repo-name') +}) + +test('Get user-based Github Pages URL for a repo', () => { + var repo = new Repo('my-user') + + expect(repo.githubPagesUrl()).toBe('https://my-user.github.io/') +}) + +test('Get correct full license URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.licenseUrl()).toBe('https://github.com/my-user/my-repo-name/blob/master/LICENSE') +}) + +test('Get generate template URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.template()).toBe('https://github.com/my-user/my-repo-name/template') +}) + +test('Get tags URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.tags()).toBe('https://github.com/my-user/my-repo-name/tags') +}) + +test('Get releases URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.releases()).toBe('https://github.com/my-user/my-repo-name/releases') +}) + +test('Get forks URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.forks()).toBe('https://github.com/my-user/my-repo-name/network/members') +}) + +test('Get stars URL for a repo', () => { + var repo = new Repo('my-user', 'my-repo-name') + + expect(repo.stars()).toBe('https://github.com/my-user/my-repo-name/stargazers') +}) diff --git a/src/components/license.js b/src/components/license.js new file mode 100644 index 00000000..443a9b4a --- /dev/null +++ b/src/components/license.js @@ -0,0 +1,32 @@ +class License { + DOMAIN = 'https://img.shields.io' + REL_TARGET = '#license' + DEFAULT_COLOR = 'blue' + + constructor(type, target = null, color = null) { + this.type = type + + this.target = target || this.REL_TARGET + this.color = color || this.DEFAULT_COLOR + } + + title() { + return `License: ${this.type}` + } + + img() { + return `${this.DOMAIN}/badge/License-${this.type}-${this.color}` + } + + fileUrl() { + return `` + } + + markdown() { + return `[![${this.title()}](${this.img()})](${this.target})` + } +} + +module.exports = { + License, +} \ No newline at end of file diff --git a/src/components/repo.js b/src/components/repo.js new file mode 100644 index 00000000..d9c02232 --- /dev/null +++ b/src/components/repo.js @@ -0,0 +1,19 @@ +class Repo { + constructor(username, repoName) { + this.username = username + this.repoName = repoName + } + + url() { + return `https://github.com/${this.username}/${this.repoName}` + } + + licenseUrl() { + return `${this.url()}/blob/master/LICENSE` + } +} + + +module.exports = { + Repo +} \ No newline at end of file