generator-module-maker
Version:
Node module scaffolding template with lots of optional badges
183 lines (168 loc) • 6.93 kB
JavaScript
const
Yeoman = require( "yeoman-generator" ),
slug = require( "slug" ),
camel = require( "camelcase" ),
badges = [
{ checked: true, name: "coveralls" },
{ checked: true, name: "travis" },
{ checked: true, name: "depstat" },
{ checked: true, name: "npm" },
{ checked: true, name: "license" },
{ checked: true, name: "snyk" }
],
badgeUrls = {
'coveralls': {
name: 'Coveralls Status',
image: 'https://coveralls.io/repos/github/julianjensen/PROJECT/badge.svg?branch=master',
url: 'https://coveralls.io/github/julianjensen/PROJECT?branch=master'
},
'travis': {
name: 'Build Status',
url: 'https://travis-ci.org/julianjensen/PROJECT',
image: 'http://img.shields.io/travis/julianjensen/PROJECT.svg'
},
'depstat': {
name: 'Dependency Status',
url: 'https://gemnasium.com/github.com/julianjensen/PROJECT',
image: 'https://gemnasium.com/badges/github.com/julianjensen/PROJECT.svg'
},
'npm': {
name: 'npm version',
url: 'https://badge.fury.io/js/PROJECT',
image: 'https://badge.fury.io/js/PROJECT.svg'
},
'license': {
name: 'License',
url: 'https://github.com/julianjensen/PROJECT/blob/master/LICENSE',
image: 'https://img.shields.io/badge/license-MIT-brightgreen.svg'
},
'snyk': {
name: 'Known Vulnerabilities',
url: 'https://snyk.io/test/github/julianjensen/PROJECT',
image: 'https://snyk.io/test/github/julianjensen/PROJECT/badge.svg'
}
},
prompts = [
{
name: "moduleName",
message: "What is the module name?",
filter: s => slug( s ),
default: require( "path" ).basename( process.cwd() ).replace( /\s+/g, "-" )
},
{
name: "moduleVersion",
message: "What is the module version number (use SEMVER)?",
default: "1.0.0"
},
{
name: "moduleDesc",
message: "What is the module description?",
default: props => props.name
},
{
name: "githubUsername",
message: "What is your GitHub user name?",
store: true,
validate: value => value.length > 0 ? true : "github needed"
},
{
name: "moduleKeywords",
message: "Keywords?",
default: "node"
},
{
type: 'checkbox',
name: 'badges',
message: "Select what badges to include",
choices: badges
},
{
name: "codacyserial",
message: "Enter your codacy project token",
store: true,
when: answers => answers.badges.indexOf( 'codacy' ) !== -1
},
{
name: "codebeatserial",
message: "Enter your codebeat project secret (Project UUID under settings)",
store: true,
when: answers => answers.badges.indexOf( 'codebeat' ) !== -1
}
];
module.exports = class extends Yeoman
{
prompting()
{
return this.prompt( prompts ).then(
props => {
this.moduleName = props.moduleName;
this.moduleVersion = props.moduleVersion;
this.moduleDesc = props.moduleDesc;
this.camelModuleName = camel( props.moduleName );
this.moduleKeywords = props.moduleKeywords.trim().split( /\s*,\s*|\s+/ ).map( s => ( s || "" ).trim() );
this.githubUsername = props.githubUsername;
this.name = this.user.git.name();
this.email = this.user.git.email();
this.repository = 'https://github.com/' + this.githubUsername + '/' + this.moduleName;
this.badges = props.badges;
this.badgesTop = '';
this.badgesFoot = '';
const
top = [],
foot = [];
( props.badges || [] ).forEach( name => {
const { name: title, image, url } = badgeUrls[ name ];
top.push( `[![${title}][${name}-image]][${name}-url]` );
foot.push(
`[${name}-url]: ${url.replace( /PROJECT/g, this.moduleName ).replace( /\$([^$]+\$)/g, ( $0, $1 ) => this[ $1 ] )}`,
`[${name}-image]: ${image.replace( /PROJECT/g, this.moduleName )}\n`
);
} );
this.badgesTop = top.join( '\n' );
this.badgesFoot = foot.join( '\n' );
this.files = {};
this._template( "README.md" );
this._template( "package.json" );
this._template( "LICENSE" );
this._template( "CHANGELOG.md" );
this._template( "jest.config.js" );
this._template( "index.js", "index.js" );
this._template( "index-test.js", "test/index-test.js" );
this._template( "editorconfig", ".editorconfig" );
this._template( "gitignore", ".gitignore" );
this._template( "eslintrc.json", ".eslintrc.json" );
} );
}
_template( src, dst )
{
this.files[ src ] = dst || src;
}
writing()
{
Object.keys( this.files ).forEach( filename => this.fs.copyTpl( this.templatePath( filename ), this.destinationPath( this.files[ filename ] ), this ) );
}
install()
{
this.installDependencies( { bower: false } );
this.composeWith( 'generator-travis', {
options: {
config: {
sudo: 'false',
language: 'node_js',
node_js: [ 'stable', 'lts/*' ],
after_script: [ 'npm run coveralls' ]
}
}
}, { local: require.resolve( 'generator-travis/generators/app' ) } );
this.composeWith( 'generator-git-setup', {
options: {
commit: 'Initial commit',
repository: this.repository
}
}, { local: require.resolve( 'generator-git-setup/generators/app' ) } );
}
end()
{
this.spawnCommand( 'npm', [ 'run', 'version' ] );
}
};