diff --git a/.eslintignore b/.eslintignore index 6e0ebdd..a363967 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ -src/app/templates +**/templates/** ./app/ coverage/ package-lock.json diff --git a/.npmignore b/.npmignore index c16bd13..de24886 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,6 @@ # Source src +scripts # Logs logs diff --git a/package.json b/package.json index d754bf2..7d0850e 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,10 @@ "node": ">=6" }, "scripts": { - "build": "yarn build:babel && yarn build:templates", + "build": "yarn build:clean && yarn build:babel && yarn build:templates", + "build:clean": "rimraf ./app", "build:babel": "BABEL_ENV=production babel src --out-dir .", - "build:templates": "ncp ./src/app/templates ./app/templates", + "build:templates": "./scripts/build-templates.sh", "test": "jest --coverage", "test:watch": "jest --watch", "lint": "eslint ./src", @@ -64,7 +65,6 @@ "eslint-plugin-promise": "^4.0.1", "eslint-plugin-standard": "^4.0.0", "jest": "^23.6.0", - "ncp": "^2.0.0", "prettier": "^1.15.3", "rimraf": "^2.6.2", "semantic-release": "^15.13.1", diff --git a/scripts/build-templates.sh b/scripts/build-templates.sh new file mode 100755 index 0000000..3381910 --- /dev/null +++ b/scripts/build-templates.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cp -r ./src/app/generators/project/templates ./app/generators/project/templates +cp -r ./src/app/generators/github/templates ./app/generators/github/templates +cp -r ./src/app/generators/travis/templates ./app/generators/travis/templates diff --git a/src/app/generators/base-generator.js b/src/app/generators/base-generator.js new file mode 100644 index 0000000..889caba --- /dev/null +++ b/src/app/generators/base-generator.js @@ -0,0 +1,13 @@ +const Generator = require('yeoman-generator'); + +const options = require('../options'); + +module.exports = class extends Generator { + constructor(...args) { + super(...args); + + for (const [name, value] of Object.entries(options)) { + this.option(name, value); + } + } +}; diff --git a/src/app/generators/github/index.js b/src/app/generators/github/index.js new file mode 100644 index 0000000..35438df --- /dev/null +++ b/src/app/generators/github/index.js @@ -0,0 +1,37 @@ +const BaseGenerator = require('../base-generator'); + +const github = require('../../lib/github'); + +module.exports = class extends BaseGenerator { + writing() { + if (this.options.githubTemplates) { + const templatesToCopy = [ + { templatePath: '_github', destinationPath: '.github' }, + { templatePath: 'other', destinationPath: 'other' }, + { templatePath: 'contributing.md', destinationPath: 'contributing.md' }, + ]; + + templatesToCopy.forEach(({ templatePath, destinationPath }) => + this.fs.copyTpl( + this.templatePath(templatePath), + this.destinationPath(destinationPath), + this.options + ) + ); + } + } + + async end() { + if (this.options.createGithubRepository) { + const { + data: { html_url: url }, + } = await github.createRepository({ + name: this.options.projectName, + description: this.options.description, + }); + + this.log('\n\n'); + this.log(`Repository created: ${url}`); + } + } +}; diff --git a/src/app/templates/_github/issue_template.md b/src/app/generators/github/templates/_github/issue_template.md similarity index 100% rename from src/app/templates/_github/issue_template.md rename to src/app/generators/github/templates/_github/issue_template.md diff --git a/src/app/templates/_github/pull_request_template.md b/src/app/generators/github/templates/_github/pull_request_template.md similarity index 100% rename from src/app/templates/_github/pull_request_template.md rename to src/app/generators/github/templates/_github/pull_request_template.md diff --git a/src/app/templates/contributing.md b/src/app/generators/github/templates/contributing.md similarity index 100% rename from src/app/templates/contributing.md rename to src/app/generators/github/templates/contributing.md diff --git a/src/app/templates/other/code_of_conduct.md b/src/app/generators/github/templates/other/code_of_conduct.md similarity index 100% rename from src/app/templates/other/code_of_conduct.md rename to src/app/generators/github/templates/other/code_of_conduct.md diff --git a/src/app/templates/other/examples.md b/src/app/generators/github/templates/other/examples.md similarity index 100% rename from src/app/templates/other/examples.md rename to src/app/generators/github/templates/other/examples.md diff --git a/src/app/templates/other/roadmap.md b/src/app/generators/github/templates/other/roadmap.md similarity index 100% rename from src/app/templates/other/roadmap.md rename to src/app/generators/github/templates/other/roadmap.md diff --git a/src/app/generators/project/index.js b/src/app/generators/project/index.js new file mode 100644 index 0000000..28c97d7 --- /dev/null +++ b/src/app/generators/project/index.js @@ -0,0 +1,101 @@ +const path = require('path'); +const chalk = require('chalk'); +const commandExists = require('command-exists'); +const findUp = require('find-up'); +const makeDir = require('make-dir'); + +const BaseGenerator = require('../base-generator'); + +module.exports = class extends BaseGenerator { + /* + Run Loop + */ + + configuring() { + if (path.basename(this.destinationRoot()) !== this.options.projectName) { + return makeDir(this.options.projectName).then(path => { + this.destinationRoot(path); + this.log(`\nGenerating a new project in ${chalk.green(path)}\n`); + }); + } + } + + writing() { + const templatesToCopy = [ + { templatePath: '_babelrc', destinationPath: '.babelrc' }, + { templatePath: '_editorconfig', destinationPath: '.editorconfig' }, + { templatePath: '_eslintignore', destinationPath: '.eslintignore' }, + { templatePath: '_eslintrc', destinationPath: '.eslintrc' }, + { templatePath: '_gitattributes', destinationPath: '.gitattributes' }, + { templatePath: '_gitignore', destinationPath: '.gitignore' }, + { templatePath: '_npmignore', destinationPath: '.npmignore' }, + { templatePath: '_package.json', destinationPath: 'package.json' }, + { templatePath: 'license', destinationPath: 'license' }, + { templatePath: 'readme.md', destinationPath: 'readme.md' }, + { templatePath: 'src', destinationPath: 'src' }, + ]; + + templatesToCopy.forEach(({ templatePath, destinationPath }) => + this.fs.copyTpl( + this.templatePath(templatePath), + this.destinationPath(destinationPath), + this.options + ) + ); + } + + async install() { + this._installNpmDeps(); + } + + async end() { + await this._removeYoRc(); + + this._createGit(); + } + + /* + Private Methods + */ + + _installNpmDeps() { + const hasYarn = commandExists.sync('yarn'); + + this.installDependencies({ + bower: false, + npm: !hasYarn, + yarn: hasYarn, + }); + } + + _createGit() { + const { repository } = this.options; + + this.spawnCommandSync('git', ['init', '--quiet']); + this.spawnCommandSync('git', [ + 'remote', + 'add', + 'origin', + `git@github.com:${repository}.git`, + ]); + this.spawnCommandSync('git', ['add', '.']); + this.spawnCommandSync('git', [ + 'commit', + '-m', + 'Generated by generator-node-mdl 🔥', + '--quiet', + ]); + } + + async _removeYoRc() { + const yoRC = await findUp('.yo-rc.json'); + + if (yoRC) { + this.fs.delete(yoRC); + + await new Promise((resolve, reject) => { + this.fs.commit(() => resolve()); + }); + } + } +}; diff --git a/src/app/templates/_babelrc b/src/app/generators/project/templates/_babelrc similarity index 100% rename from src/app/templates/_babelrc rename to src/app/generators/project/templates/_babelrc diff --git a/src/app/templates/_editorconfig b/src/app/generators/project/templates/_editorconfig similarity index 100% rename from src/app/templates/_editorconfig rename to src/app/generators/project/templates/_editorconfig diff --git a/src/app/templates/_eslintignore b/src/app/generators/project/templates/_eslintignore similarity index 100% rename from src/app/templates/_eslintignore rename to src/app/generators/project/templates/_eslintignore diff --git a/src/app/templates/_eslintrc b/src/app/generators/project/templates/_eslintrc similarity index 100% rename from src/app/templates/_eslintrc rename to src/app/generators/project/templates/_eslintrc diff --git a/src/app/templates/_gitattributes b/src/app/generators/project/templates/_gitattributes similarity index 100% rename from src/app/templates/_gitattributes rename to src/app/generators/project/templates/_gitattributes diff --git a/src/app/templates/_gitignore b/src/app/generators/project/templates/_gitignore similarity index 100% rename from src/app/templates/_gitignore rename to src/app/generators/project/templates/_gitignore diff --git a/src/app/templates/_npmignore b/src/app/generators/project/templates/_npmignore similarity index 100% rename from src/app/templates/_npmignore rename to src/app/generators/project/templates/_npmignore diff --git a/src/app/templates/_package.json b/src/app/generators/project/templates/_package.json similarity index 87% rename from src/app/templates/_package.json rename to src/app/generators/project/templates/_package.json index 5a40296..fccd120 100644 --- a/src/app/templates/_package.json +++ b/src/app/generators/project/templates/_package.json @@ -14,8 +14,7 @@ }, "scripts": { "prebuild": "rimraf dist", - "build": "babel src --out-dir dist --copy-files --ignore **/*.test.js", - "postbuild": "rimraf dist/**/*.test.js", + "build": "babel src --out-dir dist --ignore **/*.test.js", "test:watch": "jest --watch", "test": "jest --coverage",<% if (coveralls) { %> "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",<% } %> @@ -33,8 +32,8 @@ "babel-core": "^7.0.0-bridge.0", "babel-eslint": "^10.0.1", "babel-jest": "^23.6.0", - "babel-plugin-add-module-exports": "^1.0.0", - "coveralls": "^3.0.2", + "babel-plugin-add-module-exports": "^1.0.0",<% if (coveralls) { %> + "coveralls": "^3.0.2",<% } %> "eslint": "^5.10.0", "eslint-config-prettier": "^3.3.0", "eslint-config-standard": "^12.0.0", diff --git a/src/app/templates/license b/src/app/generators/project/templates/license similarity index 100% rename from src/app/templates/license rename to src/app/generators/project/templates/license diff --git a/src/app/templates/readme.md b/src/app/generators/project/templates/readme.md similarity index 100% rename from src/app/templates/readme.md rename to src/app/generators/project/templates/readme.md diff --git a/src/app/templates/src/index.js b/src/app/generators/project/templates/src/index.js similarity index 100% rename from src/app/templates/src/index.js rename to src/app/generators/project/templates/src/index.js diff --git a/src/app/templates/src/index.test.js b/src/app/generators/project/templates/src/index.test.js similarity index 100% rename from src/app/templates/src/index.test.js rename to src/app/generators/project/templates/src/index.test.js diff --git a/src/app/generators/travis/index.js b/src/app/generators/travis/index.js new file mode 100644 index 0000000..795119f --- /dev/null +++ b/src/app/generators/travis/index.js @@ -0,0 +1,38 @@ +const BaseGenerator = require('../base-generator'); + +module.exports = class extends BaseGenerator { + writing() { + this.fs.copyTpl( + this.templatePath('_travis.yml'), + this.destinationPath('.travis.yml'), + this.options + ); + } + + async install() { + if (process.env.NODE_ENV === 'test') return; + + const { repository, npmDeploy, npmToken } = this.props; + + this.spawnCommandSync('gem', [ + 'install', + 'travis', + '--no-ri', + '--no-rdoc', + '--quiet', + ]); + this.spawnCommandSync('travis', ['login', '--auto']); + this.spawnCommandSync('travis', ['enable', '-r', repository]); + + if (npmDeploy) { + this.spawnCommandSync('travis', [ + 'env', + 'set', + 'NPM_TOKEN', + npmToken, + '-r', + repository, + ]); + } + } +}; diff --git a/src/app/templates/_travis.yml b/src/app/generators/travis/templates/_travis.yml similarity index 100% rename from src/app/templates/_travis.yml rename to src/app/generators/travis/templates/_travis.yml diff --git a/src/app/index.js b/src/app/index.js index 6c71112..af7c677 100644 --- a/src/app/index.js +++ b/src/app/index.js @@ -1,361 +1,23 @@ -'use strict'; - -const path = require('path'); const Generator = require('yeoman-generator'); -const camelCase = require('lodash.camelcase'); -const kebabCase = require('lodash.kebabcase'); -const chalk = require('chalk'); -const commandExists = require('command-exists'); -const findUp = require('find-up'); -const makeDir = require('make-dir'); - -const github = require('./lib/github'); -const npm = require('./lib/npm'); +const Prompter = require('./prompter'); module.exports = class extends Generator { - /* - Run Loop - */ + constructor(...args) { + super(...args); - async prompting() { - this.props = {}; - - await this._promptGeneral(); - await this._promptGithub(); - await this._promptTravis(); - await this._promptNpm(); + this.prompter = new Prompter(this); } - configuring() { - if (path.basename(this.destinationRoot()) !== this.props.projectName) { - return makeDir(this.props.projectName).then(path => { - this.destinationRoot(path); - this.log(`\nGenerating a new project in ${chalk.green(path)}\n`); - }); - } - } - - writing() { - const templatesToCopy = [ - { templatePath: '_babelrc', destinationPath: '.babelrc' }, - { templatePath: '_editorconfig', destinationPath: '.editorconfig' }, - { templatePath: '_eslintignore', destinationPath: '.eslintignore' }, - { templatePath: '_eslintrc', destinationPath: '.eslintrc' }, - { templatePath: '_gitattributes', destinationPath: '.gitattributes' }, - { templatePath: '_gitignore', destinationPath: '.gitignore' }, - { templatePath: '_package.json', destinationPath: 'package.json' }, - { templatePath: 'license', destinationPath: 'license' }, - { templatePath: 'readme.md', destinationPath: 'readme.md' }, - { templatePath: 'src', destinationPath: 'src' }, - ]; - - if (this.props.githubTemplates) { - templatesToCopy.push( - { templatePath: '_github', destinationPath: '.github' }, - { templatePath: 'other', destinationPath: 'other' }, - { templatePath: 'contributing.md', destinationPath: 'contributing.md' } - ); - } - - if (this.props.travisCI) { - templatesToCopy.push({ - templatePath: '_travis.yml', - destinationPath: '.travis.yml', - }); - } - - if (this.props.npmDeploy) { - templatesToCopy.push({ - templatePath: '_npmignore', - destinationPath: '.npmignore', - }); - } - - templatesToCopy.forEach(({ templatePath, destinationPath }) => - this.fs.copyTpl( - this.templatePath(templatePath), - this.destinationPath(destinationPath), - this.props - ) - ); + async prompting() { + this.props = await this.prompter.prompt(); } - async install() { - this._installNpmDeps(); + configuring() { + this.composeWith(require.resolve('./generators/project'), this.props); + this.composeWith(require.resolve('./generators/github'), this.props); if (this.props.travisCI) { - await this._installTravis(); - } - } - - async end() { - await this._removeYoRc(); - - this._createGit(); - - if (this.props.createGithubRepository) { - const repository = await this._createGithubRepository(); - - this.log('\n\n'); - this.log(`Repository created: ${repository.html_url}`); - } - } - - /* - Private Methods - */ - - async _promptGeneral() { - const answers = await this.prompt([ - { - name: 'projectName', - message: 'Project name', - default: kebabCase(this.appname), - filter: kebabCase, - }, - { - name: 'description', - message: 'Description', - default: 'Generated by `generator-node-mdl`', - store: true, - }, - { - name: 'name', - message: "Author's name", - default: this.user.git.name(), - }, - { - name: 'email', - message: "Author's email", - default: this.user.git.email(), - }, - { - name: 'website', - message: "Author's website", - store: true, - }, - ]); - - Object.assign(this.props, answers, { - camelProject: camelCase(answers.projectName), - }); - } - - async _promptGithub() { - const answers = await this.prompt([ - { - name: 'githubUsername', - message: 'GitHub username', - store: true, - }, - { - name: 'githubTemplates', - message: 'Would you like to make it Github friendly for contributions?', - type: 'confirm', - default: true, - }, - { - name: 'createGithubRepository', - message: 'Create a github repository?', - type: 'confirm', - default: true, - }, - ]); - - Object.assign(this.props, answers, { - repository: `${answers.githubUsername}/${this.props.projectName}`, - }); - - if (answers.createGithubRepository) { - await this._loginToGithub(); - } - } - - async _promptTravis() { - const answers = await this.prompt([ - { - name: 'travisCI', - message: 'Give your project super prowers using Travis CI?', - type: 'confirm', - default: true, - when: () => { - this.log( - '\nLearn how to use Travis CI: https://docs.travis-ci.com/user/tutorial/#to-get-started-with-travis-ci' - ); - return true; - }, - }, - { - name: 'coveralls', - message: 'Connect TravisCI to Coveralls?', - type: 'confirm', - default: true, - when: ({ travisCI }) => { - if (travisCI) { - this.log('\nLearn how to use Coveralls: https://coveralls.io'); - return true; - } - - return false; - }, - }, - ]); - - Object.assign(this.props, answers, { coveralls: answers.coveralls }); - } - - async _promptNpm() { - const answers = await this.prompt([ - { - name: 'npmDeploy', - message: 'Automatically deploy to npm using TravisCI?', - type: 'confirm', - default: true, - when: () => { - if (this.props.travisCI) { - this.log('\nNeed to have an npm account: https://www.npmjs.com/'); - return true; - } - - return false; - }, - }, - ]); - - Object.assign(this.props, { - npmDeploy: answers.npmDeploy, - }); - - if (this.props.npmDeploy) { - await this._loginToNpm(); - } - } - - _promptNpmLogin() { - return this.prompt([ - { - name: 'npmUsername', - message: 'Your npm username:', - store: true, - }, - { - name: 'npmPassword', - message: 'Your npm password:', - type: 'password', - }, - ]); - } - - _promptGithubPassword() { - const { githubUsername } = this.props; - - return this.prompt([ - { - name: 'githubPassword', - message: `Enter you github password for ${githubUsername}:`, - type: 'password', - }, - ]); - } - - async _loginToNpm() { - const { npmUsername, npmPassword } = await this._promptNpmLogin(); - - // generate npm-token - const { token } = await npm.login({ - username: npmUsername, - password: npmPassword, - }); - - Object.assign(this.props, { - npmToken: token, - }); - } - - async _loginToGithub() { - const { githubUsername } = this.props; - const { githubPassword } = await this._promptGithubPassword(); - - github.login({ - username: githubUsername, - password: githubPassword, - }); - } - - _installTravis() { - if (process.env.NODE_ENV === 'test') { - return; + this.composeWith(require.resolve('./generators/travis'), this.props); } - const { repository, npmDeploy, npmToken } = this.props; - - this.spawnCommandSync('gem', [ - 'install', - 'travis', - '--no-ri', - '--no-rdoc', - '--quiet', - ]); - this.spawnCommandSync('travis', ['login', '--auto']); - this.spawnCommandSync('travis', ['enable', '-r', repository]); - - if (npmDeploy) { - this.spawnCommandSync('travis', [ - 'env', - 'set', - 'NPM_TOKEN', - npmToken, - '-r', - repository, - ]); - } - } - - _installNpmDeps() { - const hasYarn = commandExists.sync('yarn'); - - this.installDependencies({ - bower: false, - npm: !hasYarn, - yarn: hasYarn, - }); - } - - _createGit() { - const { repository } = this.props; - - this.spawnCommandSync('git', ['init', '--quiet']); - this.spawnCommandSync('git', [ - 'remote', - 'add', - 'origin', - `git@github.com:${repository}.git`, - ]); - this.spawnCommandSync('git', ['add', '.']); - this.spawnCommandSync('git', [ - 'commit', - '-m', - 'Generated by generator-node-mdl 🔥', - '--quiet', - ]); - } - - async _removeYoRc() { - return findUp('.yo-rc.json').then(res => { - if (res) { - this.fs.delete(res); - return new Promise((resolve, reject) => { - this.fs.commit(() => resolve()); - }); - } - }); - } - - async _createGithubRepository() { - const { data } = await github.createRepository({ - name: this.props.projectName, - description: this.props.description, - }); - - return data; } }; diff --git a/src/app/index.test.js b/src/app/index.test.js index a508c6f..5eaf80c 100644 --- a/src/app/index.test.js +++ b/src/app/index.test.js @@ -29,6 +29,7 @@ test('default files', () => { '.eslintrc', '.gitattributes', '.gitignore', + '.npmignore', 'license', 'package.json', 'readme.md', @@ -43,8 +44,6 @@ test('default files', () => { 'other/roadmap.md', // travisCI '.travis.yml', - // npmDeploy - '.npmignore', ]); assert.noFile([ @@ -276,7 +275,6 @@ describe('prompts', () => { npmPassword: 'some-password', }) .then(() => { - assert.file(['.npmignore']); assert.fileContent('.travis.yml', 'deploy:'); assert.fileContent('.travis.yml', 'provider: npm'); assert.fileContent('.travis.yml', 'api_key: $NPM_TOKEN'); @@ -290,7 +288,6 @@ describe('prompts', () => { npmDeploy: false, }) .then(() => { - assert.noFile(['.npmignore']); assert.noFileContent('.travis.yml', 'deploy'); }); }); diff --git a/src/app/options.js b/src/app/options.js new file mode 100644 index 0000000..7dc1f2e --- /dev/null +++ b/src/app/options.js @@ -0,0 +1,49 @@ +module.exports = { + projectName: { + type: String, + required: true, + desc: 'Project name', + default: 'Generated by `generator-node-mdl`', + }, + camelProject: { + type: String, + required: true, + desc: 'Project name camelCase', + }, + description: { + type: String, + required: true, + desc: 'Project description', + default: 'Generated by `generator-node-mdl`', + }, + name: { type: String, required: true, desc: "Author's name" }, + email: { type: String, required: true, desc: "Author's email" }, + website: { type: String, required: true, desc: "Author's website" }, + githubUsername: { type: String, required: true, desc: 'GitHub username' }, + repository: { + type: String, + required: true, + desc: 'Repository name username/project-name', + }, + githubTemplates: { + type: Boolean, + desc: 'Would you like to make it Github friendly for contributions?', + }, + createGithubRepository: { + type: Boolean, + desc: 'Let me create a new github repository for you?', + }, + coveralls: { type: Boolean, desc: 'Connect TravisCI to Coveralls?' }, + travisCI: { + type: Boolean, + desc: 'Give your project super prowers using Travis CI?', + }, + npmDeploy: { + type: Boolean, + desc: 'Automatically deploy to npm using TravisCI?', + }, + npmToken: { + type: String, + desc: 'Your npm-token', + }, +}; diff --git a/src/app/prompter.js b/src/app/prompter.js new file mode 100644 index 0000000..333bef3 --- /dev/null +++ b/src/app/prompter.js @@ -0,0 +1,205 @@ +const camelCase = require('lodash.camelcase'); +const kebabCase = require('lodash.kebabcase'); + +const github = require('./lib/github'); +const npm = require('./lib/npm'); + +const options = require('./options'); + +module.exports = class { + constructor(generator) { + this.generator = generator; + } + + async prompt() { + this.props = {}; + + await this._promptGeneral(); + await this._promptGithub(); + await this._promptTravis(); + await this._promptNpm(); + + return this.props; + } + + async _promptGeneral() { + const answers = await this.generator.prompt([ + { + name: 'projectName', + message: options.projectName.desc, + default: kebabCase(this.generator.appname), + filter: kebabCase, + }, + { + name: 'description', + message: options.description.desc, + default: options.description.default, + store: true, + }, + { + name: 'name', + message: options.name.desc, + default: this.generator.user.git.name(), + }, + { + name: 'email', + message: options.email.desc, + default: this.generator.user.git.email(), + }, + { + name: 'website', + message: options.website.desc, + store: true, + }, + ]); + + Object.assign(this.props, answers, { + camelProject: camelCase(answers.projectName), + }); + } + + async _promptGithub() { + const answers = await this.generator.prompt([ + { + name: 'githubUsername', + message: options.githubUsername.desc, + store: true, + }, + { + name: 'githubTemplates', + message: options.githubTemplates.desc, + type: 'confirm', + default: true, + }, + { + name: 'createGithubRepository', + message: options.createGithubRepository.desc, + type: 'confirm', + default: true, + }, + ]); + + Object.assign(this.props, answers, { + repository: `${answers.githubUsername}/${this.props.projectName}`, + }); + + if (answers.createGithubRepository) { + await this._loginToGithub(); + } + } + + async _promptTravis() { + const answers = await this.generator.prompt([ + { + name: 'travisCI', + message: options.travisCI.desc, + type: 'confirm', + default: true, + when: () => { + this.generator.log( + '\nLearn how to use Travis CI: https://docs.travis-ci.com/user/tutorial/#to-get-started-with-travis-ci' + ); + return true; + }, + }, + { + name: 'coveralls', + message: options.coveralls.desc, + type: 'confirm', + default: true, + when: ({ travisCI }) => { + if (travisCI) { + this.generator.log( + '\nLearn how to use Coveralls: https://coveralls.io' + ); + return true; + } + + return false; + }, + }, + ]); + + Object.assign(this.props, answers, { coveralls: answers.coveralls }); + } + + async _promptNpm() { + const answers = await this.generator.prompt([ + { + name: 'npmDeploy', + message: options.npmDeploy.desc, + type: 'confirm', + default: true, + when: () => { + if (this.props.travisCI) { + this.generator.log( + '\nNeed to have an npm account: https://www.npmjs.com/' + ); + return true; + } + + return false; + }, + }, + ]); + + Object.assign(this.props, { + npmDeploy: answers.npmDeploy, + }); + + if (this.props.npmDeploy) { + await this._loginToNpm(); + } + } + + _promptNpmLogin() { + return this.generator.prompt([ + { + name: 'npmUsername', + message: 'Your npm username:', + store: true, + }, + { + name: 'npmPassword', + message: 'Your npm password:', + type: 'password', + }, + ]); + } + + _promptGithubPassword() { + const { githubUsername } = this.props; + + return this.generator.prompt([ + { + name: 'githubPassword', + message: `Enter you github password for ${githubUsername}:`, + type: 'password', + }, + ]); + } + + async _loginToNpm() { + const { npmUsername, npmPassword } = await this._promptNpmLogin(); + + // generate npm-token + const { token } = await npm.login({ + username: npmUsername, + password: npmPassword, + }); + + Object.assign(this.props, { + npmToken: token, + }); + } + + async _loginToGithub() { + const { githubUsername } = this.props; + const { githubPassword } = await this._promptGithubPassword(); + + github.login({ + username: githubUsername, + password: githubPassword, + }); + } +}; diff --git a/yarn.lock b/yarn.lock index aac750d..3a897dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5662,11 +5662,6 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= -ncp@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/ncp/-/ncp-2.0.0.tgz#195a21d6c46e361d2fb1281ba38b91e9df7bdbb3" - integrity sha1-GVoh1sRuNh0vsSgbo4uR6d9727M= - needle@^2.2.1: version "2.2.4" resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e"