From e9d7c58c98574d92cba6ae813ab28a33f3196bcf Mon Sep 17 00:00:00 2001 From: ItsJonQ Date: Tue, 5 Jul 2016 18:20:00 -0400 Subject: [PATCH 1/3] Adds test command - automatically crawls project for .scss with a directory of test/ - leverages sass-test to parse the .scss -> mocha compatible test - autogens .js files compatible for Mocha (and deletes them after the test) - uses Mocha to run the .scss tests --- bin/cli.js | 4 ++- commands/test/index.js | 70 +++++++++++++++++++++++++++++++++++++++++ package.json | 6 ++++ templates/test/index.js | 4 +++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 commands/test/index.js create mode 100644 templates/test/index.js diff --git a/bin/cli.js b/bin/cli.js index 211e290..2cdabcf 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -15,6 +15,7 @@ var cli = meow(` -c, --config Custom sass-lint config (.yml) -i, --ignore Ignore files for linting/testing -l, --lint Initialize .scss lint (powered by sass-lint) + -t, --test Initialize tests (powered by true + mocha) ${pkg.name} v${pkg.version} License: ${pkg.license} @@ -24,7 +25,8 @@ var cli = meow(` c: 'config', i: 'ignore', n: 'new', - l: 'lint' + l: 'lint', + t: 'test' } }); diff --git a/commands/test/index.js b/commands/test/index.js new file mode 100644 index 0000000..69bf06c --- /dev/null +++ b/commands/test/index.js @@ -0,0 +1,70 @@ +// Commands :: Test +'use strict'; + +if (!global.cli.flags.test) { + return false; +} + +var _ = require('lodash'); +var del = require('del'); +var findRoot = require('find-root'); +var fs = require('fs'); +var glob = require('glob'); +var mkdirp = require('mkdirp'); +var path = require('path'); +var uuid = require('node-uuid'); + +var Mocha = require('mocha'); +var mocha = new Mocha(); +var root = findRoot(process.cwd()); +var templateFile = global.templateDir + 'test/index.js'; +var template = fs.readFileSync(templateFile, 'utf8'); +var testCache = root + '/.seed-test-cache'; +var testDir = 'test'; + +var options = { + ignore: [ + 'bower_components/**/*', + 'node_modules/**/*' + ] +}; + +var getTestFileName = function(file) { + return path.basename(file).replace(/_/g, '').replace('.scss', '') + '-' + uuid.v1() + '.js'; +}; + +// Delete the test cache directory (just in case it was left over) +del(testCache); + +glob('**/*.scss', options, function(err, matches) { + var files = matches; + var currentDir = process.cwd().split('/').pop(); + + if (currentDir !== testDir) { + files = files.filter(function(file) { + var dir = testDir + '/'; + return file.indexOf(dir) >= 0; + }); + } + // Create the test cache directory + mkdirp.sync(testCache); + + _.forEach(files, function(file) { + var dest = testCache + '/' + getTestFileName(file); + var options = { + file: file + }; + // Add mocha compatible .js file to test cache + fs.writeFileSync(dest, _.template(template)(options)); + // Add path of test cache file to Mocha + mocha.addFile(dest); + }); + + mocha.run(function(failures){ + // Delete test cache redirectory + del(testCache); + process.on('exit', function() { + process.exit(failures); + }); + }); +}); diff --git a/package.json b/package.json index 8bf8bee..c94cb1d 100644 --- a/package.json +++ b/package.json @@ -25,14 +25,20 @@ "authors-to-markdown": "^0.1.0", "chalk": "^1.1.3", "copy-files": "^0.1.0", + "del": "^2.2.1", "file-exists": "^1.0.0", + "find-root": "^1.0.0", + "glob": "^7.0.5", "inquirer": "^1.1.0", "is-blank": "^1.1.0", "lodash": "^4.13.1", "meow": "^3.7.0", "mkdirp": "^0.5.1", + "mocha": "^2.5.3", + "node-uuid": "^1.4.7", "require-dir": "^0.3.0", "sass-lint": "^1.8.2", + "sass-true": "^2.1.0", "shelljs": "^0.7.0" } } diff --git a/templates/test/index.js b/templates/test/index.js new file mode 100644 index 0000000..02910e3 --- /dev/null +++ b/templates/test/index.js @@ -0,0 +1,4 @@ +var path = require('path'); +var sassTrue = require('sass-true'); + +sassTrue.runSass({file: '<%= file %>'}, describe, it); From 4aadbae138e60b9aeec780866b36293fb90e5ac5 Mon Sep 17 00:00:00 2001 From: ItsJonQ Date: Wed, 6 Jul 2016 10:39:02 -0400 Subject: [PATCH 2/3] Adds generator for test - refactors generator code - adds generator for test - adds generator command for test "seed g test awesome" - adds test.scss template --- commands/generate/config.js | 4 ++ commands/generate/generate.js | 81 ++++++++++++++++++++++++++--------- commands/generate/help.js | 1 + commands/generate/index.js | 7 +-- commands/generate/prompt.js | 25 ++++++----- commands/test/index.js | 14 +++++- templates/test/test.scss | 29 +++++++++++++ 7 files changed, 123 insertions(+), 38 deletions(-) create mode 100644 templates/test/test.scss diff --git a/commands/generate/config.js b/commands/generate/config.js index 420ec85..c3ca68c 100644 --- a/commands/generate/config.js +++ b/commands/generate/config.js @@ -60,6 +60,10 @@ config.prototype.setPrefixType = function() { prefix = 's'; type = 'scope'; } + if (_type === 'test' || _type === 't') { + prefix = 't'; + type = 'test'; + } if (_type === 'utility' || _type === 'u') { prefix = 'u'; type = 'utility'; diff --git a/commands/generate/generate.js b/commands/generate/generate.js index 69cbfe4..e06eb42 100644 --- a/commands/generate/generate.js +++ b/commands/generate/generate.js @@ -2,50 +2,91 @@ 'use strict'; var _ = require('lodash'); -var chalk = require('chalk'); +var config = require('./config'); +var cli = global.cli; var fs = require('fs'); -var isEmpty = require('is-empty'); var mkdirp = require('mkdirp'); var path = require('path'); - -var cli = global.cli; -var config = require('./config'); -var currentDir = path.basename(process.cwd()).toLowerCase(); -var templateDir = global.templateDir + 'module/'; - var prompt = require('./prompt'); -var generate = function(options) { +var parseOptions = function(options) { if (!options) { - process.exit(1) return false; } - options = new config(options).options; - var dest = options.name; - console.log(currentDir); + if (options.type === 'test') { + return parseTest(options); + } + else { + return parseModule(options); + } +}; + +var parseModule = function(options) { + if (!options) { + return false; + } + var currentDir = path.basename(process.cwd()).toLowerCase(); + + options.dest = options.name; + options.templateDir = global.templateDir + 'module/'; if (options.type !== 'scope' && currentDir.indexOf(options.type) < 0) { var type = options.type; if (type === 'utility') { type = 'utilities'; } + if (type === 'test') { + type = 'test'; + } else { type = `${ type }s`; } - dest = `${ type }/${ dest }`; + options.dest = `${ type }/${ options.dest }`; } - mkdirp.sync(dest); + options.templateFiles = fs.readdirSync(options.templateDir); + + return options; +}; + +var parseTest = function(options) { + if (!options) { + return false; + } + + options.name = options.name.replace('seed-', ''); + options.dest = 'test'; + options.templateDir = global.templateDir + 'test/'; + options.templateFiles = [ 'test.scss' ]; + + return options; +}; + + +var generate = function(options) { + if (!options) { + process.exit(1) + return false; + } + + options = parseOptions(options); + + mkdirp.sync(options.dest); console.log(`Generating your new ${ options.type }…\n`); - var templateFiles = fs.readdirSync(templateDir); - _.forEach(templateFiles, function(file) { - var template = fs.readFileSync(templateDir + file, 'utf8'); - fs.writeFileSync(dest + '/' + file, _.template(template)(options)); - console.log(` created ${ dest + '/' + file }`); + _.forEach(options.templateFiles, function(file) { + var template = fs.readFileSync(options.templateDir + file, 'utf8'); + var outputFile = file; + + if (options.type === 'test') { + outputFile = '_' + options.name + '.scss'; + } + + fs.writeFileSync(options.dest + '/' + outputFile, _.template(template)(options)); + console.log(` created ${ options.dest + '/' + outputFile }`); }); console.log(`\nCongrats! Your new .scss ${ options.type } has been created.`); diff --git a/commands/generate/help.js b/commands/generate/help.js index 0ce3706..d3589ac 100644 --- a/commands/generate/help.js +++ b/commands/generate/help.js @@ -10,6 +10,7 @@ var help = ` c, component o, object s, scope + t, test u, utility Name: diff --git a/commands/generate/index.js b/commands/generate/index.js index 84ae181..65cda2f 100644 --- a/commands/generate/index.js +++ b/commands/generate/index.js @@ -15,12 +15,7 @@ if (command === 'generate' || command === 'g') { if (type && name) { var options = { name: name, - type: type, - components: false, - objects: false, - modifers: false, - states: false, - variables: false + type: type }; return generate(options); diff --git a/commands/generate/prompt.js b/commands/generate/prompt.js index 6dbcd8c..d441173 100644 --- a/commands/generate/prompt.js +++ b/commands/generate/prompt.js @@ -8,15 +8,6 @@ var cli = global.cli; var templateDir = global.templateDir; var questions = [ - { - name: 'name', - type: 'input', - message: 'name:', - default: 'button', - filter: function(val) { - return val.replace('seed-', '').toLowerCase(); - } - }, { name: 'type', type: 'list', @@ -38,13 +29,27 @@ var questions = [ name: 'Scope', value: 'scope' }, + { + key: 't', + name: 'Test', + value: 'test' + }, { key: 'u', name: 'Utility', value: 'utility' } ] - } + }, + { + name: 'name', + type: 'input', + message: 'name:', + default: 'button', + filter: function(val) { + return val.replace('seed-', '').toLowerCase(); + } + }, ]; var prompt = function() { diff --git a/commands/test/index.js b/commands/test/index.js index 69bf06c..3152a84 100644 --- a/commands/test/index.js +++ b/commands/test/index.js @@ -29,12 +29,16 @@ var options = { ] }; +var clean = function() { + return del(testCache, { force: true }); +}; + var getTestFileName = function(file) { return path.basename(file).replace(/_/g, '').replace('.scss', '') + '-' + uuid.v1() + '.js'; }; // Delete the test cache directory (just in case it was left over) -del(testCache); +clean(); glob('**/*.scss', options, function(err, matches) { var files = matches; @@ -49,6 +53,12 @@ glob('**/*.scss', options, function(err, matches) { // Create the test cache directory mkdirp.sync(testCache); + if (!files.length) { + console.log(`\nCouldn't find any tests in your project!`); + console.log(`You can create a new test by executinexecuting "seed g"\n`); + process.exit(0) + } + _.forEach(files, function(file) { var dest = testCache + '/' + getTestFileName(file); var options = { @@ -62,7 +72,7 @@ glob('**/*.scss', options, function(err, matches) { mocha.run(function(failures){ // Delete test cache redirectory - del(testCache); + clean(); process.on('exit', function() { process.exit(failures); }); diff --git a/templates/test/test.scss b/templates/test/test.scss new file mode 100644 index 0000000..75c3f06 --- /dev/null +++ b/templates/test/test.scss @@ -0,0 +1,29 @@ +// <%= docName %> :: <%= docType %> + +@import "true"; + +@include test-module("<%= name %>") { + + @include test("Example .scss test (Generated by seed-cli)") { + @include assert('pink') { + + $pink-hot: pink; + @include output { + background: $pink-hot; + } + + @include expect { + background: pink; + } + + } + } + + @include test("Another .scss test (Generated by seed-cli)") { + + $test: 0; + $expect: 0; + + @include assert-equal($test, $expect, '0'); + } +} From 9589ec02d1d246e9d6f6088b0a8a62257256e164 Mon Sep 17 00:00:00 2001 From: ItsJonQ Date: Wed, 6 Jul 2016 13:21:22 -0400 Subject: [PATCH 3/3] Updates cli readme + readme.md --- README.md | 5 +++-- bin/cli.js | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 619a08f..bb408c6 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,14 @@ Usage: seed Commands: - n, new Creates a new Seed package - g, generate Creates a new .scss module (component/object/utility) + n, new Creates a new Seed pack + g, generate Creates a new .scss module (component/object/scope/test/utility) Options: -c, --config Custom sass-lint config (.yml) -i, --ignore Ignore files for linting/testing -l, --lint Initialize .scss lint (powered by sass-lint) + -t, --test Initialize tests (powered by mocha + sass-true) ``` ## Thanks diff --git a/bin/cli.js b/bin/cli.js index 2cdabcf..1ae5ddf 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -8,17 +8,16 @@ var cli = meow(` seed Commands: - n, new Creates a new Seed package - g, generate Creates a new .scss module (component/object/utility) + n, new Creates a new Seed pack + g, generate Creates a new .scss module (component/object/scope/test/utility) Options: -c, --config Custom sass-lint config (.yml) -i, --ignore Ignore files for linting/testing -l, --lint Initialize .scss lint (powered by sass-lint) - -t, --test Initialize tests (powered by true + mocha) + -t, --test Initialize tests (powered by mocha + sass-true) ${pkg.name} v${pkg.version} - License: ${pkg.license} Website: ${pkg.homepage} `, { alias: {