From c3937bf1b572867cb5724718da25472d570a7ad2 Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Tue, 7 Apr 2015 23:34:21 -0700 Subject: [PATCH] Towards some new beginning - Deleted old app structure - Start the app generator - Created the travis.CI node sub generator --- .travis.yml | 4 +- app/index.js | 146 ------------------ app/templates/Gruntfile.js | 48 ------ app/templates/README.md | 42 ----- app/templates/_package.json | 41 ----- app/templates/cli.js | 16 -- app/templates/editorconfig | 12 -- app/templates/gitattributes | 1 - app/templates/gitignore | 2 - app/templates/index.js | 4 - app/templates/jshintrc | 15 -- app/templates/test/test.js | 10 -- generators/app/index.js | 23 +++ generators/travis/index.js | 11 ++ .../travis}/templates/travis.yml | 4 +- package.json | 14 +- test/app.js | 32 ++++ test/test-creation.js | 96 ------------ test/test-load.js | 9 -- test/travis.js | 18 +++ 20 files changed, 92 insertions(+), 456 deletions(-) delete mode 100644 app/index.js delete mode 100644 app/templates/Gruntfile.js delete mode 100644 app/templates/README.md delete mode 100644 app/templates/_package.json delete mode 100644 app/templates/cli.js delete mode 100644 app/templates/editorconfig delete mode 100644 app/templates/gitattributes delete mode 100644 app/templates/gitignore delete mode 100644 app/templates/index.js delete mode 100644 app/templates/jshintrc delete mode 100644 app/templates/test/test.js create mode 100644 generators/app/index.js create mode 100644 generators/travis/index.js rename {app => generators/travis}/templates/travis.yml (100%) create mode 100644 test/app.js delete mode 100644 test/test-creation.js delete mode 100644 test/test-load.js create mode 100644 test/travis.js diff --git a/.travis.yml b/.travis.yml index dedfc07..b0c6a8d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ sudo: false language: node_js node_js: - - 'iojs' - - '0.12' - '0.10' + - '0.12' + - 'iojs' diff --git a/app/index.js b/app/index.js deleted file mode 100644 index ea7fa77..0000000 --- a/app/index.js +++ /dev/null @@ -1,146 +0,0 @@ -'use strict'; -var path = require('path'); -var npmName = require('npm-name'); -var yeoman = require('yeoman-generator'); - -module.exports = yeoman.generators.Base.extend({ - init: function () { - this.pkg = require('../package.json'); - this.log( - this.yeoman + - '\nThe name of your project shouldn\'t contain "node" or "js" and' + - '\nshould be a unique ID not already in use at npmjs.org.'); - }, - askForModuleName: function () { - var done = this.async(); - - var prompts = [{ - name: 'name', - message: 'Module Name', - default: path.basename(process.cwd()), - }, { - type: 'confirm', - name: 'pkgName', - message: 'The name above already exists on npm, choose another?', - default: true, - when: function(answers) { - var done = this.async(); - - npmName(answers.name, function (err, available) { - if (!available) { - done(true); - return; - } - - done(false); - }); - } - }]; - - this.prompt(prompts, function (props) { - if (props.pkgName) { - return this.askForModuleName(); - } - - this.slugname = this._.slugify(props.name); - this.safeSlugname = this.slugname.replace(/-+([a-zA-Z0-9])/g, function (g) { - return g[1].toUpperCase(); - }); - - done(); - }.bind(this)); - }, - - askFor: function () { - var cb = this.async(); - - var prompts = [{ - name: 'description', - message: 'Description', - default: 'The best module ever.' - }, { - name: 'homepage', - message: 'Homepage' - }, { - name: 'license', - message: 'License', - default: 'MIT' - }, { - name: 'githubUsername', - message: 'GitHub username or organization', - store: true - }, { - name: 'authorName', - message: 'Author\'s Name', - store: true - }, { - name: 'authorEmail', - message: 'Author\'s Email', - store: true - }, { - name: 'authorUrl', - message: 'Author\'s Homepage', - store: true - }, { - name: 'keywords', - message: 'Key your keywords (comma to split)' - }, { - type: 'confirm', - name: 'cli', - message: 'Do you need a CLI?' - }, { - type: 'confirm', - name: 'browser', - message: 'Do you need Browserify?' - }]; - - this.currentYear = (new Date()).getFullYear(); - - this.prompt(prompts, function (props) { - if (props.githubUsername) { - this.repoUrl = props.githubUsername + '/' + this.slugname; - } else { - this.repoUrl = 'user/repo'; - } - - this.keywords = props.keywords.split(',').map(function (el) { - return el.trim(); - }); - - this.props = props; - - cb(); - }.bind(this)); - }, - - - app: function () { - this.config.save(); - this.copy('editorconfig', '.editorconfig'); - this.copy('jshintrc', '.jshintrc'); - this.copy('gitignore', '.gitignore'); - this.copy('gitattributes', '.gitattributes'); - this.copy('travis.yml', '.travis.yml'); - - this.template('README.md', 'README.md'); - this.template('Gruntfile.js', 'Gruntfile.js'); - this.template('_package.json', 'package.json'); - - if (this.props.cli) { - this.template('cli.js', 'cli.js'); - } - }, - - projectfiles: function () { - this.template('index.js', 'index.js'); - this.mkdir('test'); - this.template('test/test.js', 'test/test.js'); - }, - - install: function () { - this.installDependencies({ - bower: false, - skipInstall: this.options['skip-install'] - }); - } -}); diff --git a/app/templates/Gruntfile.js b/app/templates/Gruntfile.js deleted file mode 100644 index cf14b9e..0000000 --- a/app/templates/Gruntfile.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; -module.exports = function (grunt) { - // Show elapsed time at the end - require('time-grunt')(grunt); - // Load all grunt tasks - require('load-grunt-tasks')(grunt); - - grunt.initConfig({ - jshint: { - options: { - jshintrc: '.jshintrc', - reporter: require('jshint-stylish') - }, - gruntfile: { - src: ['Gruntfile.js'] - }, - js: { - src: ['*.js'] - }, - test: { - src: ['test/**/*.js'] - } - }, - mochacli: { - options: { - reporter: 'nyan', - bail: true - }, - all: ['test/*.js'] - }, - watch: { - gruntfile: { - files: '<%%= jshint.gruntfile.src %>', - tasks: ['jshint:gruntfile'] - }, - js: { - files: '<%%= jshint.js.src %>', - tasks: ['jshint:js', 'mochacli'] - }, - test: { - files: '<%%= jshint.test.src %>', - tasks: ['jshint:test', 'mochacli'] - } - } - }); - - grunt.registerTask('default', ['jshint', 'mochacli']); -}; diff --git a/app/templates/README.md b/app/templates/README.md deleted file mode 100644 index 7bb1bb8..0000000 --- a/app/templates/README.md +++ /dev/null @@ -1,42 +0,0 @@ -# <%= props.slugname %> [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] - -> <%= props.description %> - - -## Install - -```sh -$ npm install --save <%= slugname %> -``` - - -## Usage - -```js -var <%= safeSlugname %> = require('<%= slugname %>'); - -<%= safeSlugname %>('Rainbow'); -```<% if (props.cli) { %> - -```sh -$ npm install --global <%= slugname %> -$ <%= slugname %> --help -```<% } %><% if (props.browser) { %> - -```sh -# creates a browser.js -$ npm run browser -```<% } %> - - -## License - -<%= props.license %> © [<%= props.authorName %>](<%= props.authorUrl %>) - - -[npm-image]: https://badge.fury.io/js/<%= slugname %>.svg -[npm-url]: https://npmjs.org/package/<%= slugname %> -[travis-image]: https://travis-ci.org/<%= props.githubUsername %>/<%= slugname %>.svg?branch=master -[travis-url]: https://travis-ci.org/<%= props.githubUsername %>/<%= slugname %> -[daviddm-image]: https://david-dm.org/<%= props.githubUsername %>/<%= slugname %>.svg?theme=shields.io -[daviddm-url]: https://david-dm.org/<%= props.githubUsername %>/<%= slugname %> diff --git a/app/templates/_package.json b/app/templates/_package.json deleted file mode 100644 index c94eee3..0000000 --- a/app/templates/_package.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "name": "<%= slugname %>", - "version": "0.0.0", - "description": "<%= props.description %>",<% if (props.homepage) { %> - "homepage": "<%= props.homepage %>",<% } %> - "author": { - "name": "<%= props.authorName %>", - "email": "<%= props.authorEmail %>"<% if (props.authorUrl) { %>, - "url": "<%= props.authorUrl %>"<% } %> - }, - "repository": "<%= repoUrl %>", - "license": "<%= props.license %>", - "files": [ - "index.js"<% if (props.cli) { %>, - "cli.js"<% } %> - ], - "keywords": [ - "<%= slugname %>"<% for (var i = 0; i < keywords.length; i++) { %>, - "<%= keywords[i] %>"<% } %> - ], - "dependencies": {<% if (props.cli) { %> - "meow": "^3.1.0" - <% } %>}, - "devDependencies": {<% if (props.browser) { %> - "browserify": "^9.0.3",<% } %> - "grunt-cli": "^0.1.13", - "grunt-contrib-jshint": "^0.11.0", - "grunt-contrib-watch": "^0.6.1", - "grunt-mocha-cli": "^1.12.0", - "jshint-stylish": "^1.0.1", - "load-grunt-tasks": "^3.1.0", - "time-grunt": "^1.1.0" - }, - "scripts": { - "test": "grunt"<% if (props.browser) { %>, - "browser": "browserify index.js > browser.js"<% } %> - }<% if (props.cli) { %>, - "bin": { - "<%= slugname %>": "cli.js" - }<% } %> -} diff --git a/app/templates/cli.js b/app/templates/cli.js deleted file mode 100644 index eccb9aa..0000000 --- a/app/templates/cli.js +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env node -'use strict'; -var meow = require('meow'); -var <%= safeSlugname %> = require('./'); - -var cli = meow({ - help: [ - 'Usage', - ' <%= slugname %> ', - '', - 'Example', - ' <%= slugname %> Unicorn' - ].join('\n') -}); - -<%= safeSlugname %>(cli.input[0]); diff --git a/app/templates/editorconfig b/app/templates/editorconfig deleted file mode 100644 index 4a7ea30..0000000 --- a/app/templates/editorconfig +++ /dev/null @@ -1,12 +0,0 @@ -root = true - -[*] -indent_style = space -indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.md] -trim_trailing_whitespace = false diff --git a/app/templates/gitattributes b/app/templates/gitattributes deleted file mode 100644 index 176a458..0000000 --- a/app/templates/gitattributes +++ /dev/null @@ -1 +0,0 @@ -* text=auto diff --git a/app/templates/gitignore b/app/templates/gitignore deleted file mode 100644 index 91fa8cf..0000000 --- a/app/templates/gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/node_modules/ -npm-debug.log diff --git a/app/templates/index.js b/app/templates/index.js deleted file mode 100644 index a02a199..0000000 --- a/app/templates/index.js +++ /dev/null @@ -1,4 +0,0 @@ -'use strict'; -module.exports = function (str) { - console.log(str || 'Rainbow'); -}; diff --git a/app/templates/jshintrc b/app/templates/jshintrc deleted file mode 100644 index 2cb788b..0000000 --- a/app/templates/jshintrc +++ /dev/null @@ -1,15 +0,0 @@ -{ - "boss": true, - "curly": true, - "eqeqeq": true, - "eqnull": true, - "immed": true, - "latedef": true, - "mocha" : true, - "newcap": true, - "noarg": true, - "node": true, - "sub": true, - "undef": true, - "unused": true -} diff --git a/app/templates/test/test.js b/app/templates/test/test.js deleted file mode 100644 index 9645830..0000000 --- a/app/templates/test/test.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; -var assert = require('assert'); -var <%= safeSlugname %> = require('../'); - -describe('<%= slugname %> node module', function () { - it('must have at least one test', function () { - <%= safeSlugname %>(); - assert(false, 'I was too lazy to write any tests. Shame on me.'); - }); -}); diff --git a/generators/app/index.js b/generators/app/index.js new file mode 100644 index 0000000..635c76f --- /dev/null +++ b/generators/app/index.js @@ -0,0 +1,23 @@ +'use strict'; +var generators = require('yeoman-generator'); + +module.exports = generators.Base.extend({ + constructor: function () { + generators.Base.apply(this, arguments); + + this.option('travis', { + type: Boolean, + required: false, + defaults: true, + description: 'Include travis config' + }); + }, + + initializing: function() { + if (this.options.travis) { + this.composeWith('node:travis', {}, { + local: require.resolve('../travis') + }); + } + } +}); diff --git a/generators/travis/index.js b/generators/travis/index.js new file mode 100644 index 0000000..dd02486 --- /dev/null +++ b/generators/travis/index.js @@ -0,0 +1,11 @@ +'use strict'; +var generators = require('yeoman-generator'); + +module.exports = generators.Base.extend({ + initializing: function() { + this.fs.copy( + this.templatePath('travis.yml'), + this.destinationPath('.travis.yml') + ); + } +}); diff --git a/app/templates/travis.yml b/generators/travis/templates/travis.yml similarity index 100% rename from app/templates/travis.yml rename to generators/travis/templates/travis.yml index dedfc07..b0c6a8d 100644 --- a/app/templates/travis.yml +++ b/generators/travis/templates/travis.yml @@ -1,6 +1,6 @@ sudo: false language: node_js node_js: - - 'iojs' - - '0.12' - '0.10' + - '0.12' + - 'iojs' diff --git a/package.json b/package.json index 051b066..dfc5cbe 100644 --- a/package.json +++ b/package.json @@ -10,23 +10,17 @@ "cli" ], "author": "Yeoman team", - "main": "app", "repository": "yeoman/generator-node", "scripts": { - "test": "mocha --timeout 50000" + "test": "mocha test -R spec" }, "dependencies": { "npm-name": "^1.0.2", - "yeoman-generator": "^0.18.3" + "yeoman-generator": "^0.19.0" }, "devDependencies": { - "grunt": "^0.4.5", - "mocha": "*", - "shelljs": "^0.4.0" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">=1.2.10" + "mocha": "^2.2.3", + "yeoman-assert": "^2.0.0" }, "files": [ "app" diff --git a/test/app.js b/test/app.js new file mode 100644 index 0000000..3790828 --- /dev/null +++ b/test/app.js @@ -0,0 +1,32 @@ +'use strict'; + +var path = require('path'); +var assert = require('yeoman-assert'); +var helpers = require('yeoman-generator').test; + +describe('node:app', function () { + before(function (done) { + helpers.run(path.join(__dirname, '../generators/app')) + .on('end', done); + }); + + it('creates files', function () { + assert.file([ + '.travis.yml' + ]); + }); +}); + +describe('node:app --no-travis', function () { + before(function (done) { + helpers.run(path.join(__dirname, '../generators/app')) + .withOptions({ travis: false }) + .on('end', done); + }); + + it('skip .travis.yml', function () { + assert.noFile([ + '.travis.yml' + ]); + }); +}); diff --git a/test/test-creation.js b/test/test-creation.js deleted file mode 100644 index d250bba..0000000 --- a/test/test-creation.js +++ /dev/null @@ -1,96 +0,0 @@ -'use strict'; -var path = require('path'); -var assert = require('yeoman-generator').assert; -var helpers = require('yeoman-generator').test; -var shelljs = require('shelljs'); - -describe('node generator', function () { - beforeEach(function (done) { - helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { - if (err) { - done(err); - return; - } - - this.app = helpers.createGenerator('node:app', [ - '../../app' - ]); - this.app.options['skip-install'] = true; - done(); - }.bind(this)); - }); - - it('creates expected files', function (done) { - var expected = [ - 'index.js', - 'cli.js', - 'test/test.js', - '.gitignore', - '.jshintrc', - '.travis.yml', - '.editorconfig', - 'Gruntfile.js', - 'package.json', - 'README.md' - ]; - - helpers.mockPrompt(this.app, { - 'name': 'mymodule', - 'description': 'awesome module', - 'pkgName': false, - 'license': 'MIT', - 'homepage': 'http://yeoman.io', - 'githubUsername': 'octocat', - 'authorName': 'Octo Cat', - 'authorEmail': 'octo@example.com', - 'authorUrl': 'http://yeoman.io', - 'keywords': 'keyword1,keyword2,keyword3', - 'cli': true, - 'browser': true - }); - - shelljs.exec('npm install meow', {silent: true}); - - this.app.run(function () { - assert.file(expected); - assert.fileContent('package.json', /"name": "mymodule"/); - assert.deepEqual(require('./temp/cli.js'), {}); - done(); - }); - - }); - - it('creates expected files without cli', function (done) { - var expected = [ - 'index.js', - 'test/test.js', - '.gitignore', - '.jshintrc', - '.travis.yml', - 'Gruntfile.js', - 'package.json', - 'README.md' - ]; - - helpers.mockPrompt(this.app, { - 'name': 'mymodule', - 'description': 'awesome module', - 'pkgName': false, - 'license': 'MIT', - 'homepage': 'http://yeoman.io', - 'githubUsername': 'octocat', - 'authorName': 'Octo Cat', - 'authorEmail': 'octo@example.com', - 'authorUrl': 'http://yeoman.io', - 'keywords': 'keyword1,keyword2,keyword3', - 'cli': false, - 'browser': true - }); - - this.app.run(function () { - assert.file(expected); - assert.fileContent('package.json', /"name": "mymodule"/); - done(); - }); - }); -}); diff --git a/test/test-load.js b/test/test-load.js deleted file mode 100644 index 76d85b4..0000000 --- a/test/test-load.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; -var assert = require('assert'); - -describe('node generator', function () { - it('can be imported without blowing up', function () { - var app = require('../app'); - assert(app !== undefined); - }); -}); diff --git a/test/travis.js b/test/travis.js new file mode 100644 index 0000000..05ad47d --- /dev/null +++ b/test/travis.js @@ -0,0 +1,18 @@ +'use strict'; + +var path = require('path'); +var assert = require('yeoman-assert'); +var helpers = require('yeoman-generator').test; + +describe('node:travis', function () { + before(function (done) { + helpers.run(path.join(__dirname, '../generators/travis')) + .on('end', done); + }); + + it('creates .travis.yml', function () { + assert.file([ + '.travis.yml' + ]); + }); +});