Skip to content

Commit

Permalink
feat(generator): create types/
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed May 17, 2017
1 parent bc33e4e commit 9b053e6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 16 deletions.
3 changes: 2 additions & 1 deletion 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",
"gulp-rename": "^1.2.2",
"inquirer-npm-name": "^2.0.0",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
Expand All @@ -63,7 +64,7 @@
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
"/app/src/templates/src/"
"<rootDir>/app/src/templates"
]
},
"config": {
Expand Down
37 changes: 25 additions & 12 deletions src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Generator from 'yeoman-generator'
import askName from 'inquirer-npm-name'
import _ from 'lodash'
import mkdirp from 'mkdirp'
import rename from 'gulp-rename'

import { defaultEmail, defaultName } from './values'
import * as validators from './validators'
Expand All @@ -16,10 +17,7 @@ function makeGeneratorName(name) {
}

function makePluginFunctionName(name) {
const functionName = _.camelCase(
name.substring(PLUGIN_PREFIX.length, name.length)
)
return `${functionName}()`
return _.camelCase(name.substring(PLUGIN_PREFIX.length, name.length))
}
export default class extends Generator {
initializing() {
Expand Down Expand Up @@ -81,6 +79,21 @@ export default class extends Generator {
}

writing() {
this.registerTransformStream(
rename(path => {
if (path.basename === 'index' && !path.extname) {
path.extname = '.js'
}

if (path.extname === '.test') {
path.basename += '.test'
path.extname = '.js'
}

return path
})
)

this.fs.copyTpl(
this.templatePath('package.json'),
this.destinationPath('package.json'),
Expand Down Expand Up @@ -126,16 +139,16 @@ export default class extends Generator {

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

this.fs.copyTpl(
this.templatePath('src/index'),
this.destinationPath('src/index.js'),
{ pluginFunctionName: makePluginFunctionName(this.props.pluginName) }
)
const pluginFunctionName = makePluginFunctionName(this.props.pluginName)

this.fs.copyTpl(this.templatePath('src/**'), this.destinationPath('src'), {
pluginFunctionName,
})

this.fs.copyTpl(
this.templatePath('src/index.test'),
this.destinationPath('src/index.test.js'),
{ pluginFunctionName: makePluginFunctionName(this.props.pluginName) }
this.templatePath('types/**'),
this.destinationPath('types'),
{ pluginFunctionName }
)
}

Expand Down
28 changes: 27 additions & 1 deletion src/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('generator:app', () => {
'.esdoc.json',
'src/index.js',
'src/index.test.js',
'types/index.d.ts',
'types/test.ts',
'types/tsconfig.json',
'types/types.test.js',
])
})
describe('src/', () => {
Expand All @@ -44,7 +48,7 @@ describe('generator:app', () => {
})
assert.fileContent('src/index.js', 'funTime()')
})
it('generates source file based on plugin name', async () => {
it('generates test file based on plugin name', async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-fun-time',
description: 'Danger plugin that tells you to have a fun time',
Expand All @@ -54,6 +58,28 @@ describe('generator:app', () => {
assert.fileContent('src/index.test.js', 'funTime()')
})
})
describe('types/', () => {
beforeEach(async () => {
await helpers.run(__dirname).withPrompts({
pluginName: 'danger-plugin-jest-test-runner',
description: 'Danger plugin that runs Jest tests',
authorName: 'Macklin Underdown',
authorEmail: '[email protected]',
})
})
it('generates TypeScript type defintion', () => {
assert.fileContent(
'types/index.d.ts',
'export default function jestTestRunner(): void'
)
})
it('generates TypeScript test file', () => {
assert.fileContent(
'types/test.ts',
`import jestTestRunner from './'\n\njestTestRunner()`
)
})
})
describe('CODE_OF_CONDUCT.md', () => {
it('contains author email', async () => {
await helpers.run(__dirname).withPrompts({
Expand Down
2 changes: 1 addition & 1 deletion src/app/templates/src/index
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function <%= pluginFunctionName %> {
export default function <%= pluginFunctionName %>() {

}
2 changes: 1 addition & 1 deletion src/app/templates/src/index.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
describe('<%= pluginFunctionName %>', () => {
describe('<%= pluginFunctionName %>()', () => {
it('does something')
})
1 change: 1 addition & 0 deletions src/app/templates/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function <%= pluginFunctionName %>(): void
3 changes: 3 additions & 0 deletions src/app/templates/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import <%= pluginFunctionName %> from './'

<%= pluginFunctionName %>()
10 changes: 10 additions & 0 deletions src/app/templates/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"sourceMap": false
}
}
8 changes: 8 additions & 0 deletions src/app/templates/types/types.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { join } from 'path'
import { check } from 'typings-tester'

test('TypeScript types', () => {
expect(() => {
check([join(__dirname, 'test.ts')], join(__dirname, 'tsconfig.json'))
}).not.toThrow()
})
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,10 @@ gruntfile-editor@^1.0.0:
ast-query "^2.0.0"
lodash "^4.6.1"

gulp-rename@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/gulp-rename/-/gulp-rename-1.2.2.tgz#3ad4428763f05e2764dec1c67d868db275687817"

handlebars@^4.0.3:
version "4.0.8"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.8.tgz#22b875cd3f0e6cbea30314f144e82bc7a72ff420"
Expand Down

0 comments on commit 9b053e6

Please sign in to comment.