From a24ea9671ef059195e2b512e5a94acb2f27249c4 Mon Sep 17 00:00:00 2001 From: meriadec Date: Tue, 10 Mar 2015 15:47:13 +0100 Subject: [PATCH] feat(directive): prompt for style file creation --- directive/index.js | 31 +++++++++++++++++++++++++++++++ directive/templates/style.scss | 0 test/directive.js | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 directive/templates/style.scss diff --git a/directive/index.js b/directive/index.js index 7158189..e99a31a 100644 --- a/directive/index.js +++ b/directive/index.js @@ -1,6 +1,12 @@ 'use strict'; var yeoman = require('yeoman-generator'); +var chalk = require('chalk'); +var genUtils = require('../util'); + +function bangLog (msg, color) { + console.log('[' + chalk.blue('bangular') + ']: ' + chalk[color](msg)); +} var BangularGenerator = yeoman.generators.NamedBase.extend({ @@ -19,8 +25,14 @@ var BangularGenerator = yeoman.generators.NamedBase.extend({ name: 'template', message: 'Do this directive needs an html template?', default: false + }, { + type: 'confirm', + name: 'import', + message: 'Do you want to create and import the ' + chalk.blue(this.dashName + '.scss') + ' style in your app.scss?', + default: false }], function (props) { self.needTemplate = props.template; + self.import = props.import; done(); }); @@ -37,6 +49,25 @@ var BangularGenerator = yeoman.generators.NamedBase.extend({ this.template('directive.html', basePath + '.html'); } + if (this.import) { + + this.template('style.scss', basePath + '.scss'); + + genUtils.appendNeedleOrOnTop({ + needle: '// imports', + file: 'client/styles/app.scss', + append: '@import "../directives/' + this.dashName + '/' + this.dashName + '";' + }, function importCallback (err) { + /* istanbul ignore if */ + if (err) { + bangLog('There was an error importing the style.', 'red'); + } else { + bangLog('Your style was successfully injected.', 'green'); + } + }); + + } + } }); diff --git a/directive/templates/style.scss b/directive/templates/style.scss new file mode 100644 index 0000000..e69de29 diff --git a/test/directive.js b/test/directive.js index 84d2725..b06c631 100644 --- a/test/directive.js +++ b/test/directive.js @@ -61,6 +61,25 @@ describe('Launching directive tests', function () { }); }); + it('should create a file with a style', function (done) { + bangDirective = helpers.createGenerator('bangular:directive', [bangDir + '/directive'], 'flammenkuche'); + helpers.mockPrompt(bangDirective, { import: true }); + bangDirective.run(function () { + assert.file('client/directives/flammenkuche/flammenkuche.directive.js'); + assert.file('client/directives/flammenkuche/flammenkuche.spec.js'); + assert.file('client/directives/flammenkuche/flammenkuche.scss'); + + assert.fileContent('client/directives/flammenkuche/flammenkuche.directive.js', '.directive(\'flammenkuche\', function () {'); + assert.fileContent('client/directives/flammenkuche/flammenkuche.spec.js', 'angular.element(\'\');'); + + setTimeout(function () { + assert.fileContent('client/styles/app.scss', '@import "../directives/flammenkuche/flammenkuche";'); + done(); + }, 250); + + }); + }); + }); });