From 894c5bfae5338325f40c76d2c44c03566d481b19 Mon Sep 17 00:00:00 2001 From: topleft Date: Sat, 14 Nov 2015 11:22:55 -0700 Subject: [PATCH 01/10] sync with master --- notes.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 notes.md diff --git a/notes.md b/notes.md new file mode 100644 index 000000000..b4b7dfbce --- /dev/null +++ b/notes.md @@ -0,0 +1,30 @@ +1. how to pass the commmand line argument 1. then ohw to parse it +1. then how to iterate over it inside the template? + + +sequelize model:create --name User --attributes "email:[type:string, unique:true, allowNull: false, {validate: { isEmail: true } }]" + +email: { + type: DataTypes.STRING, + allowNull: false, + validate: { + isEmail: true + } + } + + +sequelize model:create --name User --attributes email:[type:string, unique:true, allowNull: false, validate: [ isEmail: true ]] + + + + +email:[type:string:unique:true:allowNull:false [validate: [ isEmail: true ] ]] name:[type:string] + +email:[type:string, unique:true, allowNull: false, validate: [ isEmail: true ]] + + +11/13/15 1130am -- Trying to Run Tests, need to connect to DB, looking for config.json file... + +!!!! in /test/support/index.js set env variable to sqlite + +!!!! in gulp file created two new tasks - test-model and tes-migrate \ No newline at end of file From 3a527b6f95fc127f33d61d09b551446301e78ed4 Mon Sep 17 00:00:00 2001 From: topleft Date: Sat, 14 Nov 2015 11:35:56 -0700 Subject: [PATCH 02/10] added new functions for parsing attributes --- lib/helpers/model-helper.js | 67 +++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index bccd15f52..700aacec5 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -19,16 +19,71 @@ module.exports = { - 'first_name:string, last_name:string, bio:text' */ - var set = flag.replace(/,/g, ' ').split(/\s+/); - var result = {}; + /* + proposed flag format: + - name:[type:string,required:true] email:[type:string,required:true] + */ + + var self = this; + var copy = flag.slice(); + var arr = copy.split(' '); + + var stringObj = arr.map(function(str){ + var newStr = str.replace(/\[/g, '{'); + newStr = newStr.replace(/\]/g, '}'); + return newStr; + }); + + var result = stringObj.reduce(function(prev, curr) { + var obj = self.objectifyString(curr); + prev[obj[0]] = obj[1]; + return prev; + }, {}); + + return result; + }, - set.forEach(function (pair) { - var split = pair.split(':'); + objectifyString: function(str) { + + // grab field name + var splitPoint = str.indexOf(':'); + var field = str.slice(0, splitPoint); + + // grab attributes as string + var start = str.indexOf('{'); + var end = str.indexOf('}'); + var preObj = str.slice(start+1, end); - result[split[0]] = split[1]; + // create arr of arrays, [key, value] + var attrArr = preObj.split(','); + var pairs = attrArr.map(function(str){ + return str.split(':'); }); - return result; + // create attribute object + var attrObj = pairs.reduce(function(prev, curr){ + prev[curr[0]] = curr[1]; + return prev; + }, {}); + + // create final pair, [field, { attributes, ...}] + this.convertValueToType(attrObj); + return [field, attrObj]; + + }, + + convertValueToType: function(obj) { + for(var key in obj) { + + var copy = obj[key]; + if (copy === 'true' || copy === 'false') { + obj[key] = Boolean(copy); + } + else if (parseInt(copy)) { + obj[key] = parseInt(copy); + } + } + return obj; }, generateFileContent: function (args) { From 9fdac67eb002bf934568086990120df4d65f425a Mon Sep 17 00:00:00 2001 From: topleft Date: Sat, 14 Nov 2015 11:47:22 -0700 Subject: [PATCH 03/10] updated git ignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index b4604dc68..57585e9ad 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ pids *.pid *.seed +# Notes +notes.md + # Directory for instrumented libs generated by jscoverage/JSCover lib-cov From 12fbb1002a2ba83b8bd49042b0d2c4bae7c66e63 Mon Sep 17 00:00:00 2001 From: topleft Date: Sat, 14 Nov 2015 11:48:24 -0700 Subject: [PATCH 04/10] cleared cache --- notes.md | 30 ------------------------------ test/support/tmp/.gitkeep | 0 2 files changed, 30 deletions(-) delete mode 100644 notes.md delete mode 100644 test/support/tmp/.gitkeep diff --git a/notes.md b/notes.md deleted file mode 100644 index b4b7dfbce..000000000 --- a/notes.md +++ /dev/null @@ -1,30 +0,0 @@ -1. how to pass the commmand line argument 1. then ohw to parse it -1. then how to iterate over it inside the template? - - -sequelize model:create --name User --attributes "email:[type:string, unique:true, allowNull: false, {validate: { isEmail: true } }]" - -email: { - type: DataTypes.STRING, - allowNull: false, - validate: { - isEmail: true - } - } - - -sequelize model:create --name User --attributes email:[type:string, unique:true, allowNull: false, validate: [ isEmail: true ]] - - - - -email:[type:string:unique:true:allowNull:false [validate: [ isEmail: true ] ]] name:[type:string] - -email:[type:string, unique:true, allowNull: false, validate: [ isEmail: true ]] - - -11/13/15 1130am -- Trying to Run Tests, need to connect to DB, looking for config.json file... - -!!!! in /test/support/index.js set env variable to sqlite - -!!!! in gulp file created two new tasks - test-model and tes-migrate \ No newline at end of file diff --git a/test/support/tmp/.gitkeep b/test/support/tmp/.gitkeep deleted file mode 100644 index e69de29bb..000000000 From d96b6ba60e6b47e147923d1e76485496710dae5c Mon Sep 17 00:00:00 2001 From: topleft Date: Sun, 15 Nov 2015 15:27:37 -0700 Subject: [PATCH 05/10] added backwards compatability for flag formats --- lib/helpers/model-helper.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index 700aacec5..2c2eec434 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -22,17 +22,31 @@ module.exports = { /* proposed flag format: - name:[type:string,required:true] email:[type:string,required:true] + - name:string email:[type:string,required:true], first_name:string, last_name:string */ + + /* + deprecated (if accepted) flag format: + - first_name:string,last_name:string,bio:text + */ + + + var self = this; var copy = flag.slice(); var arr = copy.split(' '); var stringObj = arr.map(function(str){ - var newStr = str.replace(/\[/g, '{'); - newStr = newStr.replace(/\]/g, '}'); - return newStr; - }); + var newStr = str.replace(/\[/g, '{'); + result = newStr.replace(/\]/g, '}'); + if (result[result.length-1] === ',') { + var tempArr = result.split(''); + tempArr.splice(result.length-1, 1); + result = tempArr.join(''); + } + return result; + }); var result = stringObj.reduce(function(prev, curr) { var obj = self.objectifyString(curr); @@ -50,9 +64,15 @@ module.exports = { var field = str.slice(0, splitPoint); // grab attributes as string - var start = str.indexOf('{'); - var end = str.indexOf('}'); - var preObj = str.slice(start+1, end); + if (str.indexOf('{') !== -1) { + var start = str.indexOf('{'); + var end = str.indexOf('}'); + var preObj = str.slice(start+1, end); + } + else { + var typeValue = str.split(':')[1]; + return [field, {type: typeValue}]; + } // create arr of arrays, [key, value] var attrArr = preObj.split(','); From c8e44f6faacad511f6da670e9853c9ec57aead9b Mon Sep 17 00:00:00 2001 From: topleft Date: Mon, 16 Nov 2015 05:42:42 -0700 Subject: [PATCH 06/10] fixed linting errors --- lib/helpers/model-helper.js | 50 ++++++++++++++----------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index 2c2eec434..b426cb19d 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -26,12 +26,9 @@ module.exports = { */ /* - deprecated (if accepted) flag format: + deprecated flag format: - first_name:string,last_name:string,bio:text */ - - - var self = this; var copy = flag.slice(); @@ -49,9 +46,9 @@ module.exports = { }); var result = stringObj.reduce(function(prev, curr) { - var obj = self.objectifyString(curr); - prev[obj[0]] = obj[1]; - return prev; + var obj = self.objectifyString(curr); + prev[obj[0]] = obj[1]; + return prev; }, {}); return result; @@ -59,51 +56,42 @@ module.exports = { objectifyString: function(str) { - // grab field name var splitPoint = str.indexOf(':'); var field = str.slice(0, splitPoint); - // grab attributes as string - if (str.indexOf('{') !== -1) { - var start = str.indexOf('{'); - var end = str.indexOf('}'); - var preObj = str.slice(start+1, end); - } - else { + if (str.indexOf('{') === -1) { var typeValue = str.split(':')[1]; return [field, {type: typeValue}]; } - // create arr of arrays, [key, value] + var start = str.indexOf('{'); + var end = str.indexOf('}'); + var preObj = str.slice(start+1, end); var attrArr = preObj.split(','); var pairs = attrArr.map(function(str){ - return str.split(':'); + return str.split(':'); }); - // create attribute object var attrObj = pairs.reduce(function(prev, curr){ - prev[curr[0]] = curr[1]; - return prev; + prev[curr[0]] = curr[1]; + return prev; }, {}); - // create final pair, [field, { attributes, ...}] this.convertValueToType(attrObj); return [field, attrObj]; - + }, convertValueToType: function(obj) { - for(var key in obj) { - - var copy = obj[key]; - if (copy === 'true' || copy === 'false') { - obj[key] = Boolean(copy); + return Object.keys(obj).forEach(function(attr) { + var value = obj[attr]; + if (value === 'true' || value === 'false') { + return obj[attr] = Boolean(value); } - else if (parseInt(copy)) { - obj[key] = parseInt(copy); + else if (parseInt(value)) { + return obj[attr] = parseInt(value); } - } - return obj; + }); }, generateFileContent: function (args) { From cb83e35afa1c0eb89327b584745bad19eead9a8f Mon Sep 17 00:00:00 2001 From: topleft Date: Mon, 16 Nov 2015 05:53:03 -0700 Subject: [PATCH 07/10] fixed linting error --- lib/helpers/model-helper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index b426cb19d..e1e586482 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -86,10 +86,10 @@ module.exports = { return Object.keys(obj).forEach(function(attr) { var value = obj[attr]; if (value === 'true' || value === 'false') { - return obj[attr] = Boolean(value); + obj[attr] = Boolean(value); } else if (parseInt(value)) { - return obj[attr] = parseInt(value); + obj[attr] = parseInt(value); } }); }, From 1560ac4fab5a98eeb2d6937195d8de9d554e830c Mon Sep 17 00:00:00 2001 From: topleft Date: Mon, 16 Nov 2015 06:43:37 -0700 Subject: [PATCH 08/10] fixed linting errors --- lib/helpers/model-helper.js | 48 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index e1e586482..ab7497896 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -4,6 +4,7 @@ var clc = require('cli-color'); var helpers = require(__dirname); module.exports = { + notifyAboutExistingFile: function (file) { helpers.view.error( 'The file ' + clc.blueBright(file) + ' already exists. ' + @@ -14,51 +15,46 @@ module.exports = { transformAttributes: function (flag) { /* possible flag formats: - - first_name:string,last_name:string,bio:text - 'first_name:string last_name:string bio:text' - 'first_name:string, last_name:string, bio:text' - */ - /* proposed flag format: - name:[type:string,required:true] email:[type:string,required:true] - name:string email:[type:string,required:true], first_name:string, last_name:string - */ - /* deprecated flag format: - first_name:string,last_name:string,bio:text */ - + var self = this; var copy = flag.slice(); var arr = copy.split(' '); - - var stringObj = arr.map(function(str){ + + var stringObj = arr.map(function (str) { var newStr = str.replace(/\[/g, '{'); - result = newStr.replace(/\]/g, '}'); - if (result[result.length-1] === ',') { - var tempArr = result.split(''); - tempArr.splice(result.length-1, 1); - result = tempArr.join(''); + var result = newStr.replace(/\]/g, '}'); + if (result[ result.length - 1 ] === ',') { + var tempArr = result.split(''); + tempArr.splice(result.length - 1, 1); + result = tempArr.join(''); } return result; }); - var result = stringObj.reduce(function(prev, curr) { + var result = stringObj.reduce(function (prev, curr) { var obj = self.objectifyString(curr); prev[obj[0]] = obj[1]; return prev; }, {}); - + return result; }, - objectifyString: function(str) { - + objectifyString: function (str) { + var splitPoint = str.indexOf(':'); var field = str.slice(0, splitPoint); - + if (str.indexOf('{') === -1) { var typeValue = str.split(':')[1]; return [field, {type: typeValue}]; @@ -66,28 +62,28 @@ module.exports = { var start = str.indexOf('{'); var end = str.indexOf('}'); - var preObj = str.slice(start+1, end); + var preObj = str.slice(start + 1, end); var attrArr = preObj.split(','); - var pairs = attrArr.map(function(str){ + var pairs = attrArr.map(function (str) { return str.split(':'); }); - var attrObj = pairs.reduce(function(prev, curr){ + var attrObj = pairs.reduce(function (prev, curr) { prev[curr[0]] = curr[1]; return prev; }, {}); this.convertValueToType(attrObj); return [field, attrObj]; - + }, - convertValueToType: function(obj) { - return Object.keys(obj).forEach(function(attr) { - var value = obj[attr]; + convertValueToType: function (obj) { + return Object.keys(obj).forEach(function (attr) { + var value = obj[attr]; if (value === 'true' || value === 'false') { obj[attr] = Boolean(value); - } + } else if (parseInt(value)) { obj[attr] = parseInt(value); } From 3195e0fcf7d653bd46212c0bd713c8c8b837fb20 Mon Sep 17 00:00:00 2001 From: topleft Date: Mon, 16 Nov 2015 06:45:15 -0700 Subject: [PATCH 09/10] fixed linting errors --- lib/helpers/model-helper.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index ab7497896..699aacb34 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -33,11 +33,13 @@ module.exports = { var stringObj = arr.map(function (str) { var newStr = str.replace(/\[/g, '{'); var result = newStr.replace(/\]/g, '}'); + if (result[ result.length - 1 ] === ',') { var tempArr = result.split(''); tempArr.splice(result.length - 1, 1); result = tempArr.join(''); } + return result; }); @@ -64,6 +66,7 @@ module.exports = { var end = str.indexOf('}'); var preObj = str.slice(start + 1, end); var attrArr = preObj.split(','); + var pairs = attrArr.map(function (str) { return str.split(':'); }); @@ -81,12 +84,13 @@ module.exports = { convertValueToType: function (obj) { return Object.keys(obj).forEach(function (attr) { var value = obj[attr]; + if (value === 'true' || value === 'false') { obj[attr] = Boolean(value); - } - else if (parseInt(value)) { + } else if (parseInt(value)) { obj[attr] = parseInt(value); } + }); }, From 4ae9ea277cb8a57d5973fcfcc9f28b969879f46d Mon Sep 17 00:00:00 2001 From: topleft Date: Mon, 16 Nov 2015 06:53:08 -0700 Subject: [PATCH 10/10] fixed linting errors --- lib/helpers/model-helper.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index 699aacb34..fd343dbe1 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -36,6 +36,7 @@ module.exports = { if (result[ result.length - 1 ] === ',') { var tempArr = result.split(''); + tempArr.splice(result.length - 1, 1); result = tempArr.join(''); } @@ -45,6 +46,7 @@ module.exports = { var result = stringObj.reduce(function (prev, curr) { var obj = self.objectifyString(curr); + prev[obj[0]] = obj[1]; return prev; }, {}); @@ -53,12 +55,12 @@ module.exports = { }, objectifyString: function (str) { - var splitPoint = str.indexOf(':'); var field = str.slice(0, splitPoint); if (str.indexOf('{') === -1) { var typeValue = str.split(':')[1]; + return [field, {type: typeValue}]; } @@ -78,7 +80,6 @@ module.exports = { this.convertValueToType(attrObj); return [field, attrObj]; - }, convertValueToType: function (obj) { @@ -90,7 +91,6 @@ module.exports = { } else if (parseInt(value)) { obj[attr] = parseInt(value); } - }); },