Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new test case for writer, added path var for project creation

  • Loading branch information...
commit a077f347f26da3cbb6d8bf756dd87a885f4cb4e3 1 parent 1c9b105
@nick-jonas authored
View
6 .gitignore
@@ -22,4 +22,8 @@ node_modules
# Development #
#####################
dev_scripts
-dev_bed
+dev_bed
+
+# Testing #
+#####################
+test/testproj
View
36 bin/assemble.js
@@ -28,13 +28,32 @@ prompt.start();
// command: init
if(program.init){
- promptClassBuild('project', function(className, desc){
- var data = {
- 'name' : className,
- 'description' : desc
- };
+ prompt.get([{
+ 'name': 'className',
+ 'description': 'Enter the project name',
+ 'type': 'string',
+ 'required': true
+ }, {
+ 'name': 'classDesc',
+ 'description': 'Enter a description',
+ 'type': 'string'
+ }, {
+ 'name': 'path',
+ 'description': 'Enter the path relative to this directory.',
+ 'default': '.',
+ 'type': 'string'
+ }],
+ function(err, result){
+ var data = {
+ 'name' : sanitizeClassName(result.className),
+ 'description' : result.classDesc,
+ 'path' : path.resolve(result.path)
+ };
// write files
- commands.write.execute(path.join(__dirname, '../lib/templates/create/'), data);
+ commands.write.execute(path.join(__dirname, '../lib/templates/create/'), data, function(err, result){
+ if(err) throw err;
+ console.log('✓ Successfully created project');
+ });
});
}
@@ -67,7 +86,10 @@ if(program.view){
'description' : desc
};
// write files
- commands.write.execute(path.join(__dirname, '../lib/templates/view/'), data);
+ commands.write.execute(path.join(__dirname, '../lib/templates/view/'), data, function(err, result){
+ if(err) throw err;
+ console.log('✓ Successfully created view files');
+ });
});
}
View
6 lib/commands/reader.js
@@ -1,3 +1,9 @@
+/*
+
+THIS FILE IS NO LONGER USED, REPLACED WITH readdirp MODULE
+
+*/
+
var _ = require('lodash'),
minimatch = require('minimatch'),
path = require('path'),
View
33 lib/commands/writer.js
@@ -20,7 +20,8 @@ var _currentIndex = 0,
_newDirectories = [],
_traceLog = [], // collect console.log statements for when execution is complete
_vars = {},
- _replaceAll = false;
+ _replaceAll = false,
+ _callback = null;
/**
* Writes file
@@ -29,8 +30,16 @@ var _currentIndex = 0,
* @param {Object} variables passed along to template files, 'name' is required
* @api public
*/
-var _execute = function(templateDir, vars){
+var _execute = function(templateDir, vars, callback){
_vars = vars;
+ _callback = callback;
+
+ if(vars.path){
+ current_directory = path.resolve(current_directory, vars.path);
+ fs.mkdirSync(current_directory);
+ shell.cd(current_directory);
+ }
+
// init config
Config = new Config(templateDir, vars);
@@ -48,6 +57,10 @@ var _execute = function(templateDir, vars){
});
};
+var _fail = function(err){
+ _callback(err);
+};
+
/**
* Asyncronously writes new directories
*
@@ -56,7 +69,7 @@ var _execute = function(templateDir, vars){
var _writeDirectories = function(completeFn){
if(_currentIndex < _newDirectories.length){
mkdirp(_newDirectories[_currentIndex].path, function(err){
- if(err) console.log(err);
+ if(err) _fail(err);
_writeDirectories(completeFn);
});
_currentIndex++;
@@ -159,10 +172,11 @@ var _onWriteFileComplete = function(){
* @api private
*/
var _onWriteAllFilesComplete = function(){
- for(var i = 0; i < _traceLog.length; i++){
- console.log(_traceLog[i]);
- }
- _traceLog = [];
+ // for(var i = 0; i < _traceLog.length; i++){
+ // console.log(_traceLog[i]);
+ // }
+ // _traceLog = [];
+ _callback(null, current_directory);
};
/**
@@ -177,15 +191,14 @@ var _compileTemplate = function(fullFilePath, onCompleteFn){
var templateOutput = '';
// read template file
fs.readFile(fullFilePath, function(err, file){
- if(err) throw err;
+ if(err) _fail(err);
// compile template with data
try{
var templateFn = _.template(file.toString());
templateOutput = templateFn(_vars);
onCompleteFn(templateOutput);
}catch(e){
- console.log('error on file: ' + fullFilePath);
- throw e;
+ _fail(e);
}
});
};
View
0  test/bed/root_dir1/root_dir1_file1.ext1
No changes.
View
0  test/bed/root_dir1/root_dir1_file2.ext2
No changes.
View
0  test/bed/root_dir1/root_dir1_file3.ext3
No changes.
View
0  test/bed/root_dir1/root_dir1_subdir1/root1_dir1_subdir1_file1.ext1
No changes.
View
0  test/bed/root_dir1/root_dir1_subdir2/test.js
No changes.
View
0  test/bed/root_dir2/root_dir2_file1.ext1
No changes.
View
0  test/bed/root_dir2/root_dir2_file2.ext2
No changes.
View
0  test/bed/root_dir2/root_dir2_subdir1/test.js
No changes.
View
0  test/bed/root_dir2/root_dir2_subdir2/test.js
No changes.
View
0  test/bed/root_file1.ext1
No changes.
View
0  test/bed/root_file2.ext2
No changes.
View
0  test/bed/root_file3.ext3
No changes.
View
122 test/reader.js
@@ -1,122 +0,0 @@
-var _ = require('lodash'),
- vows = require('vows'),
- path = require('path'),
- assert = require('assert'),
- fs = require('fs'),
- os = require('os'),
- reader = require('../lib/commands/reader');
-
-/*
-Structure of test bed:
- .
- ├── root_dir1
- │ ├── root_dir1_file1.ext1
- │ ├── root_dir1_file2.ext2
- │ ├── root_dir1_file3.ext3
- │ ├── root_dir1_subdir1
- │ │ └── root1_dir1_subdir1_file1.ext1
- │ └── root_dir1_subdir2
- │ └── test.js
- ├── root_dir2
- │ ├── root_dir2_file1.ext1
- │ ├── root_dir2_file2.ext2
- │ ├── root_dir2_subdir1
- │ │ └── test.js
- │ └── root_dir2_subdir2
- │ └── test.js
- ├── root_file1.ext1
- ├── root_file2.ext2
- └── root_file3.ext3
-
- 6 directories, 12 files
-*/
-
-
-
-// test walk
-vows.describe('Read Directory').addBatch({
- 'with no filter': {
- topic: function(){
- reader.readDir(path.join(__dirname, 'bed'), this.callback);
- },
- 'can be accessed': function(err, files){
- assert.isNull(err);
- assert.isArray(files);
- },
- 'returns 12 files': function(err, files){
- assert.equal(files.length, 12);
- },
- 'first item has properties': function(err, files){
- assert.isObject(files[0]);
- assert.isString(files[0].filedir);
- assert.isString(files[0].filename);
- assert.isObject(files[0].filestat);
- }
- },
- 'with excluding file and directory filters': {
- topic: function(){
- reader.readDir(path.join(__dirname, 'bed'), this.callback, {
- 'fileFilter': ['!test.js'],
- 'dirFilter': ['!root_dir1']
- });
- },
- 'can be accessed': function(err, files){
- assert.isNull(err);
- assert.isArray(files);
- },
- 'returns 5 files': function(err, files){
- assert.equal(files.length, 5);
- },
- 'does not contain test.js files': function(err, files){
- var filenames = _.pluck(files, 'filename');
- assert.equal(_.indexOf(filenames, 'test.js'), -1);
- }
- },
- 'with mixed inclusive and exclusive':{
- topic: function(){
- reader.readDir(path.join(__dirname, 'bed'), this.callback, {
- 'fileFilter': ['!test.js'],
- 'dirFilter': ['root_dir1']
- });
- },
- 'can be accessed': function(err, files){
- assert.isNull(err);
- assert.isArray(files);
- },
- 'returns 6 files': function(err, files){
- console.log(_.pluck(files, 'filename'));
- assert.equal(files.length, 6);
- },
- 'does not contain test.js files': function(err, files){
- var filenames = _.pluck(files, 'filename');
- assert.equal(_.indexOf(filenames, 'test.js'), -1);
- },
- 'only recursed into root and `root_dir1` directory': function(err, files){
- var filedirs = _.pluck(files, 'filedir');
- var unexpectedDirs = _.without(filedirs, '/', 'root_dir1/');
- assert.equal(unexpectedDirs.length, 0);
- }
- },
- 'with exclusive directory':{
- topic: function(){
- reader.readDir(path.join(__dirname, 'bed'), this.callback, {
- 'dirFilter': ['!root_dir1']
- });
- },
- 'can be accessed': function(err, files){
- assert.isNull(err);
- assert.isArray(files);
- },
- 'returns 7 files': function(err, files){
- assert.equal(files.length, 7);
- },
- 'does not contain directory with root_dir1': function(err, files){
- var filedirs = _.pluck(files, 'filedir'),
- i = 0;
- for(i; i < filedirs.length; i++){
- var items = filedirs[i].split(path.sep);
- assert.equal(_.indexOf(items, 'root_dir1'), -1);
- }
- }
- }
-}).export(module);
View
27 test/writer.js
@@ -0,0 +1,27 @@
+var _ = require('lodash'),
+ vows = require('vows'),
+ path = require('path'),
+ assert = require('assert'),
+ shell = require('../lib/shell'),
+ fs = require('fs'),
+ os = require('os'),
+ writer = require('../lib/commands/writer');
+
+
+var TEST_DIR = path.resolve(__dirname, 'testproj');
+
+// test walk
+vows.describe('Write `create/init` command').addBatch({
+ 'with no filter': {
+ topic: function(){
+ writer.execute(path.join(__dirname, '../lib/templates/create/'),
+ {name: ' my project', description: ' my test project xxx', path: TEST_DIR}, this.callback);
+ },
+ 'is created': function(err, result){
+ assert.isNull(err);
+ assert.isNotNull(result);
+ assert.equal(result, TEST_DIR);
+ assert.isTrue(fs.existsSync(TEST_DIR));
+ }
+ }
+}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.