generator-optick-node-module
Version:
A Yeoman generator that creates the perfect node module
230 lines (203 loc) • 6.48 kB
JavaScript
'use strict'
Object.defineProperty(exports, '__esModule', {
value: true
})
var _yeomanGenerator = require('yeoman-generator')
var _yeomanGenerator2 = _interopRequireDefault(_yeomanGenerator)
var _chalk = require('chalk')
var _chalk2 = _interopRequireDefault(_chalk)
var _yosay = require('yosay')
var _yosay2 = _interopRequireDefault(_yosay)
var _mkdirp = require('mkdirp')
var _mkdirp2 = _interopRequireDefault(_mkdirp)
var _es6Promisify = require('es6-promisify')
var _es6Promisify2 = _interopRequireDefault(_es6Promisify)
var _lodash = require('lodash')
function _interopRequireDefault (obj) {
return obj && obj.__esModule ? obj : { default: obj }
}
const mkdir = (0, _es6Promisify2.default)(_mkdirp2.default)
exports.default = class extends _yeomanGenerator2.default {
constructor (args, opts) {
super(args, opts)
this.argument('name', { type: String, required: false })
this.option('showMessages', { default: true })
this.option('boilerplate', { default: true })
this.option('gitignore', { default: true })
this.option('readme', { default: true })
this.option('user', { type: String })
this.option('repo', { type: String })
this.option('description', { type: String })
this.option('homepage', { type: String })
this.option('authorName', { type: String })
this.option('authorEmail', { type: String })
this.option('authorUrl', { type: String })
}
async prompting () {
const prompts = []
const email = this.user.git.email()
if (this.options.showMessages) {
const name = _chalk2.default.red('Optick Node Module')
this.log((0, _yosay2.default)(`Welcome to the ${name} generator!`))
}
if (!this.options.user) {
prompts.push({
type: 'input',
name: 'user',
message: 'What is the Github username/organization for this project?',
// FIXME: default: email ? this.user.github.username() : process.env.USER,
default: this.options.user || process.env.USER,
store: true
})
}
if (!this.options.repo) {
prompts.push({
type: 'input',
name: 'repo',
message: 'What is the repository/project name?',
default: this.options.name || this.determineAppname(),
store: true
})
}
if (!this.options.description) {
prompts.push({
type: 'input',
name: 'description',
message: 'What is a short description for this project?',
store: true
})
}
if (!this.options.homepage) {
prompts.push({
type: 'input',
name: 'homepage',
message: 'What is the homepage for this project?',
default: props => `https://github.com/${props.user}/${props.repo}`,
store: true
})
}
if (!this.options.authorName) {
prompts.push({
type: 'input',
name: 'authorName',
message: 'Who is the author of this project?',
default: this.user.git.name(),
store: true
})
}
if (!this.options.authorEmail) {
prompts.push({
type: 'input',
name: 'authorEmail',
message: `What is the author's email?`,
default: email,
store: true
})
}
if (!this.options.authorUrl) {
prompts.push({
type: 'input',
name: 'authorUrl',
message: `What is the author's URL?`,
store: true
})
}
//
if (this.options.name) {
await mkdir(this.options.name)
process.chdir(this.options.name)
this.destinationRoot(this.destinationPath(this.options.name))
}
//
const props = await this.prompt(prompts)
Object.assign(this.options, props)
//
if (this.options.boilerplate) {
this.composeWith(require.resolve('../boilerplate'))
}
//
if (this.options.gitignore) {
this.composeWith(require.resolve('../gitignore'))
}
//
if (this.options.readme) {
this.composeWith(require.resolve('../readme'), this.options)
}
}
writing () {
const src = this.templatePath('**/{.[^.],}*')
this.fs.copyTpl(src, this.destinationRoot(), this.options)
// Package.json logic tweaked from https://github.com/yeoman/generator-node/
// Re-read the content at this point because a composed generator might
// modify it.
const pkg = {
name: this.options.repo,
version: '0.0.0',
description: this.options.description,
homepage: this.options.homepage,
author: {
name: this.options.authorName,
email: this.options.authorEmail,
url: this.options.authorUrl
},
files: ['dist'],
main: 'dist/index.js',
keywords: [],
license: 'ISC',
repository: {
type: 'git',
url: `git+ssh://git@github.com:${this.options.user}/${this.options
.repo}.git`
},
bugs: {
url: `https://github.com/${this.options.user}/${this.options
.repo}/issues`
},
scripts: {
nsp: 'nsp check',
format: 'prettier-standard "**/*.js"',
compile: 'babel src/ -d dist/ && npm run format',
pretest: 'npm run nsp && npm run compile',
test: 'jest',
precommit: 'npm test && git add .',
pushtags: 'git push --follow-tags origin master',
release: 'npm test && standard-version && npm run pushtags',
prepublishOnly: 'npm run release'
},
devDependencies: {
'babel-cli': '^6.26.0',
'babel-core': '6.25.0',
'babel-jest': '^20.0.3',
'babel-plugin-syntax-object-rest-spread': '^6.13.0',
'babel-preset-env': '^1.6.0',
'cz-conventional-changelog': '^2.0.0',
husky: '^0.14.3',
jest: '^20.0.4',
nsp: '^2.7.0',
'prettier-standard': '^6.0.0',
'regenerator-runtime': '^0.10.5',
standard: '^10.0.3',
'standard-version': '^4.2.0'
},
babel: {
presets: [['env', { targets: { node: true } }]],
plugins: ['babel-plugin-syntax-object-rest-spread']
},
config: {
commitizen: {
path: './node_modules/cz-conventional-changelog'
}
}
}
const pkgPath = this.destinationPath('package.json')
this.fs.writeJSON(
pkgPath,
(0, _lodash.merge)(pkg, this.fs.readJSON(pkgPath, {}))
)
}
install () {
this.spawnCommandSync('git', ['init'])
this.npmInstall()
}
}
module.exports = exports['default']