Skip to content

Commit

Permalink
feat(generator): create README.md
Browse files Browse the repository at this point in the history
This commit also cleans up tests to use Jest's snapshots for cleaner tests.
  • Loading branch information
macklinu committed May 17, 2017
1 parent 9b053e6 commit 3a52edf
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"dependencies": {
"chalk": "^1.1.3",
"generator-node": "^2.1.0",
"github-url-from-username-repo": "^1.0.2",
"gulp-rename": "^1.2.2",
"inquirer-npm-name": "^2.0.0",
"lodash": "^4.17.4",
Expand Down
130 changes: 130 additions & 0 deletions src/app/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,110 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generator:app CODE_OF_CONDUCT.md contains author email 1`] = `
"# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
"
`;

exports[`generator:app LICENSE.md contains the current year and author name 1`] = `
"Copyright 2017 Macklin Underdown
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \\"Software\\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED \\"AS IS\\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"
`;

exports[`generator:app README.md contains plugin name 1`] = `
"# danger-plugin-fun-time
[![Build Status](https://travis-ci.org/macklinu/danger-plugin-fun-time.svg?branch=master)](https://travis-ci.org/macklinu/danger-plugin-fun-time)
[![npm version](https://badge.fury.io/js/danger-plugin-fun-time.svg)](https://badge.fury.io/js/danger-plugin-fun-time)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
Danger plugin that tells you to have a fun time
## Usage
Install:
\`\`\`sh
yarn add danger-plugin-fun-time --dev
\`\`\`
At a glance:
\`\`\`js
// dangerfile.js
import funTime from 'danger-plugin-fun-time'
funTime()
\`\`\`
## Changelog
See the GitHub [release history](https://github.com/macklinu/danger-plugin-fun-time/releases).
## Contributing
See [CONTRIBUTING.md](contributing.md).
"
`;

exports[`generator:app package.json fills package.json with correct information 1`] = `
Object {
"author": Object {
"email": "[email protected]",
"name": "Macklin Underdown",
},
"bugs": Object {
"url": "https://github.com/macklinu/danger-plugin-fun-time/issues",
},
"config": Object {
"commitizen": Object {
"path": "cz-conventional-changelog",
Expand All @@ -30,6 +129,7 @@ Object {
"engines": Object {
"node": ">=4.0.0",
},
"homepage": "https://github.com/macklinu/danger-plugin-fun-time#readme",
"keywords": Array [
"danger",
"danger-plugin",
Expand All @@ -46,6 +146,10 @@ Object {
"optionalDependencies": Object {
"esdoc": "^0.5.2",
},
"repository": Object {
"type": "git",
"url": "https://github.com/macklinu/danger-plugin-fun-time.git",
},
"scripts": Object {
"build": "babel src --out-dir dist --ignore *.test.js",
"commit": "git-cz",
Expand All @@ -61,3 +165,29 @@ Object {
"version": "0.0.0-development",
}
`;

exports[`generator:app src/ generates source file based on plugin name 1`] = `
"export default function funTime() {
}
"
`;

exports[`generator:app src/ generates test file based on plugin name 1`] = `
"describe('funTime()', () => {
it('does something')
})
"
`;

exports[`generator:app types/ generates TypeScript test file 1`] = `
"import jestTestRunner from './'
jestTestRunner()
"
`;

exports[`generator:app types/ generates TypeScript type defintion 1`] = `
"export default function jestTestRunner(): void
"
`;
38 changes: 28 additions & 10 deletions src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import askName from 'inquirer-npm-name'
import _ from 'lodash'
import mkdirp from 'mkdirp'
import rename from 'gulp-rename'
import githubUrl from 'github-url-from-username-repo'

import { defaultEmail, defaultName } from './values'
import { defaultEmail, defaultGitHubUsername, defaultName } from './values'
import * as validators from './validators'

const PLUGIN_PREFIX = 'danger-plugin-'
Expand Down Expand Up @@ -54,6 +55,12 @@ export default class extends Generator {
message: 'What is your email (for npm authorship)?',
default: async () => await defaultEmail(),
},
{
type: 'input',
name: 'githubUsername',
message: 'What is your GitHub username?',
default: async () => await defaultGitHubUsername(),
},
{
type: 'confirm',
name: 'useYarn',
Expand All @@ -62,7 +69,10 @@ export default class extends Generator {
},
])

this.props = { pluginName, ...otherPromptOptions }
this.props = {
pluginName,
...otherPromptOptions,
}
}

default() {
Expand All @@ -79,6 +89,10 @@ export default class extends Generator {
}

writing() {
const pluginFunctionName = makePluginFunctionName(this.props.pluginName)
const repoSlug = `${this.props.githubUsername}/${this.props.pluginName}`
const githubBaseUrl = githubUrl(repoSlug)

this.registerTransformStream(
rename(path => {
if (path.basename === 'index' && !path.extname) {
Expand All @@ -94,15 +108,21 @@ export default class extends Generator {
})
)

this.fs.copyTpl(
this.templatePath('README.md'),
this.destinationPath('README.md'),
{ ...this.props, pluginFunctionName, githubBaseUrl, repoSlug }
)

this.fs.copy(
this.templatePath('CONTRIBUTING.md'),
this.destinationPath('CONTRIBUTING.md')
)

this.fs.copyTpl(
this.templatePath('package.json'),
this.destinationPath('package.json'),
_.pick(this.props, [
'pluginName',
'description',
'authorName',
'authorEmail',
])
{ ...this.props, githubBaseUrl }
)

this.fs.copyTpl(
Expand Down Expand Up @@ -139,8 +159,6 @@ export default class extends Generator {

this.fs.copy(this.templatePath('babelrc'), this.destinationPath('.babelrc'))

const pluginFunctionName = makePluginFunctionName(this.props.pluginName)

this.fs.copyTpl(this.templatePath('src/**'), this.destinationPath('src'), {
pluginFunctionName,
})
Expand Down
47 changes: 27 additions & 20 deletions src/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ jest.mock('npm-name', () => () => Promise.resolve(true))
jest.mock('./values', () => ({
defaultName: () => Promise.resolve('Git Name'),
defaultEmail: () => Promise.resolve('[email protected]'),
defaultGitHubUsername: () => Promise.resolve('macklinu'),
}))

function readFile(filename, json) {
function readFile(filename, json = false) {
const file = fs.readFileSync(filename, 'utf8')
return json ? JSON.parse(file) : file
return filename.endsWith('.json') || json ? JSON.parse(file) : file
}

describe('generator:app', () => {
Expand All @@ -22,6 +23,8 @@ describe('generator:app', () => {
authorEmail: '[email protected]',
})
assert.file([
'README.md',
'CONTRIBUTING.md',
'package.json',
'CODE_OF_CONDUCT.md',
'LICENSE.md',
Expand All @@ -38,24 +41,33 @@ describe('generator:app', () => {
'types/types.test.js',
])
})
describe('src/', () => {
it('generates source file based on plugin name', async () => {
describe('README.md', () => {
beforeEach(async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-fun-time',
description: 'Danger plugin that tells you to have a fun time',
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
})
assert.fileContent('src/index.js', 'funTime()')
})
it('generates test file based on plugin name', async () => {
it('contains plugin name', () => {
expect(readFile('README.md')).toMatchSnapshot()
})
})
describe('src/', () => {
beforeEach(async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-fun-time',
description: 'Danger plugin that tells you to have a fun time',
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
})
assert.fileContent('src/index.test.js', 'funTime()')
})
it('generates source file based on plugin name', async () => {
expect(readFile('src/index.js')).toMatchSnapshot()
})
it('generates test file based on plugin name', async () => {
expect(readFile('src/index.test.js')).toMatchSnapshot()
})
})
describe('types/', () => {
Expand All @@ -68,16 +80,10 @@ describe('generator:app', () => {
})
})
it('generates TypeScript type defintion', () => {
assert.fileContent(
'types/index.d.ts',
'export default function jestTestRunner(): void'
)
expect(readFile('types/index.d.ts')).toMatchSnapshot()
})
it('generates TypeScript test file', () => {
assert.fileContent(
'types/test.ts',
`import jestTestRunner from './'\n\njestTestRunner()`
)
expect(readFile('types/test.ts')).toMatchSnapshot()
})
})
describe('CODE_OF_CONDUCT.md', () => {
Expand All @@ -88,7 +94,7 @@ describe('generator:app', () => {
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
})
assert.fileContent('CODE_OF_CONDUCT.md', '[email protected]')
expect(readFile('CODE_OF_CONDUCT.md')).toMatchSnapshot()
})
})
describe('LICENSE.md', () => {
Expand All @@ -100,7 +106,7 @@ describe('generator:app', () => {
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
})
assert.fileContent('LICENSE.md', /2017 Macklin Underdown/g)
expect(readFile('LICENSE.md')).toMatchSnapshot()
})
})
describe('package.json', () => {
Expand All @@ -110,24 +116,25 @@ describe('generator:app', () => {
description: 'Danger plugin that tells you to have a fun time',
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
githubUsername: 'macklinu',
})
const pkg = readFile('package.json', true)
const pkg = readFile('package.json')
expect(pkg).toMatchSnapshot()
})
it('uses git user.name as default name prompt value', async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-fun-time',
description: 'Danger plugin that tells you to have a fun time',
})
const pkg = readFile('package.json', true)
const pkg = readFile('package.json')
expect(pkg.author.name).toEqual('Git Name')
})
it('uses git user.email as default email prompt value', async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-fun-time',
description: 'Danger plugin that tells you to have a fun time',
})
const pkg = readFile('package.json', true)
const pkg = readFile('package.json')
expect(pkg.author.email).toEqual('[email protected]')
})
})
Expand Down
11 changes: 11 additions & 0 deletions src/app/templates/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Development

Install [Yarn](https://yarnpkg.com/en/), and install the dependencies - `yarn install`.

Run the [Jest](https://facebook.github.io/jest/) test suite with `yarn test`.

This project uses [semantic-release](https://github.com/semantic-release/semantic-release) for automated NPM package publishing.

The main caveat: instead of running `git commit`, run `yarn commit` and follow the prompts to input a conventional changelog message via [commitizen](https://github.com/commitizen/cz-cli).

:heart:
Loading

0 comments on commit 3a52edf

Please sign in to comment.