Skip to content

Commit 3c975df

Browse files
committed
reorganized patternlab.js
should be the makings of a more sustainable, unit testable library. having some challenges referencing node from qunit js. added support for version command, which fixes #36
1 parent d212af3 commit 3c975df

File tree

4 files changed

+102
-56
lines changed

4 files changed

+102
-56
lines changed

builder/patternlab.js

Lines changed: 86 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.1.1 - 2014-05-05
2+
* patternlab-node - v0.1.1 - 2014-05-06
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -8,58 +8,61 @@
88
*
99
*/
1010

11-
var path = require('path');
12-
13-
var oPattern = function(name, subdir, filename, data){
14-
this.name = name; //this is the unique name with the subDir
15-
this.subdir = subdir;
16-
this.filename = filename;
17-
this.data = data;
18-
this.template = '';
19-
this.patternPartial = '';
20-
this.patternName = ''; //this is the display name for the ui
21-
this.patternLink = '';
22-
this.patternGroup = name.substring(name.indexOf('-') + 1, name.indexOf('-', 4) + 1 - name.indexOf('-') + 1);
23-
this.patternSubGroup = subdir.substring(subdir.indexOf('/') + 4);
24-
this.flatPatternPath = subdir.replace(/\//g, '-');
25-
};
26-
27-
var oBucket = function(name){
28-
this.bucketNameLC = name;
29-
this.bucketNameUC = name.charAt(0).toUpperCase() + name.slice(1);
30-
this.navItems = [];
31-
this.navItemsIndex = [];
32-
this.patternItems = [];
33-
this.patternItemsIndex = [];
34-
};
35-
36-
var oNavItem = function(name){
37-
this.sectionNameLC = name;
38-
this.sectionNameUC = name.charAt(0).toUpperCase() + name.slice(1);
39-
this.navSubItems = [];
40-
this.navSubItemsIndex = [];
41-
};
42-
43-
var oNavSubItem = function(name){
44-
this.patternPath = '';
45-
this.patternPartial = '';
46-
this.patternName = name.charAt(0).toUpperCase() + name.slice(1);;
47-
};
11+
var plnode = function(grunt){
12+
var path = require('path'),
13+
mustache = require('mustache'),
14+
patternlab = {};
15+
16+
patternlab.package = grunt.file.readJSON('package.json');
17+
18+
var oPattern = function(name, subdir, filename, data){
19+
this.name = name; //this is the unique name with the subDir
20+
this.subdir = subdir;
21+
this.filename = filename;
22+
this.data = data;
23+
this.template = '';
24+
this.patternPartial = '';
25+
this.patternName = ''; //this is the display name for the ui
26+
this.patternLink = '';
27+
this.patternGroup = name.substring(name.indexOf('-') + 1, name.indexOf('-', 4) + 1 - name.indexOf('-') + 1);
28+
this.patternSubGroup = subdir.substring(subdir.indexOf('/') + 4);
29+
this.flatPatternPath = subdir.replace(/\//g, '-');
30+
};
31+
32+
var oBucket = function(name){
33+
this.bucketNameLC = name;
34+
this.bucketNameUC = name.charAt(0).toUpperCase() + name.slice(1);
35+
this.navItems = [];
36+
this.navItemsIndex = [];
37+
this.patternItems = [];
38+
this.patternItemsIndex = [];
39+
};
40+
41+
var oNavItem = function(name){
42+
this.sectionNameLC = name;
43+
this.sectionNameUC = name.charAt(0).toUpperCase() + name.slice(1);
44+
this.navSubItems = [];
45+
this.navSubItemsIndex = [];
46+
};
47+
48+
var oNavSubItem = function(name){
49+
this.patternPath = '';
50+
this.patternPartial = '';
51+
this.patternName = name.charAt(0).toUpperCase() + name.slice(1);
52+
};
53+
54+
var oPatternItem = function(){
55+
this.patternPath = '';
56+
this.patternPartial = '';
57+
this.patternName = '';
58+
};
59+
60+
function getVersion() {
61+
grunt.log.ok(patternlab.package.version);
62+
}
63+
64+
function build(){
4865

49-
var oPatternItem = function(){
50-
this.patternPath = '';
51-
this.patternPartial = '';
52-
this.patternName = '';
53-
};
54-
55-
var mustache = require('mustache');
56-
57-
module.exports = function(grunt) {
58-
grunt.registerTask('patternlab', 'create design systems with atomic design', function(arg) {
59-
60-
61-
var patternlab = {};
62-
patternlab.package = grunt.file.readJSON('package.json');
6366
patternlab.data = grunt.file.readJSON('./source/_data/data.json');
6467
patternlab.listitems = grunt.file.readJSON('./source/_data/listitems.json');
6568
patternlab.header = grunt.file.read('./source/_patternlab-files/pattern-header-footer/header.html');
@@ -76,7 +79,7 @@ module.exports = function(grunt) {
7679
//check if the pattern already exists.
7780
var patternName = filename.substring(0, filename.indexOf('.'));
7881
var patternIndex = patternlab.patternIndex.indexOf(subdir + '-' + patternName);
79-
var currentPattern;
82+
var currentPattern;
8083
var flatPatternPath;
8184

8285
//ignore _underscored patterns
@@ -173,7 +176,7 @@ module.exports = function(grunt) {
173176

174177
//loop through all patterns. deciding to do this separate from the recursion, even at a performance hit, to attempt to separate the tasks of styleguide creation versus site menu creation
175178
for(var i = 0; i < patternlab.patterns.length; i++){
176-
var pattern = patternlab.patterns[i];
179+
var pattern = patternlab.patterns[i];
177180
var bucketName = pattern.name.replace(/\//g, '-').split('-')[1];
178181

179182
//check if the bucket already exists
@@ -225,7 +228,7 @@ module.exports = function(grunt) {
225228
//add to patternPaths
226229
patternlab.patternPaths[bucketName][pattern.patternName] = pattern.subdir + "/" + pattern.filename.substring(0, pattern.filename.indexOf('.'));
227230

228-
}
231+
}
229232

230233
//add the bucket.
231234
patternlab.buckets.push(bucket);
@@ -332,5 +335,33 @@ module.exports = function(grunt) {
332335
var outputFilename = './patternlab.json';
333336
grunt.file.write(outputFilename, JSON.stringify(patternlab, null, 3));
334337
}
338+
339+
}
340+
341+
return {
342+
version: function(){
343+
return getVersion();
344+
},
345+
build: function(){
346+
build();
347+
}
348+
};
349+
350+
};
351+
352+
module.exports = plnode;
353+
354+
module.exports = function(grunt) {
355+
grunt.registerTask('patternlab', 'create design systems with atomic design', function(v) {
356+
357+
var pl = plnode(grunt);
358+
359+
if(v && v === 'v'){
360+
pl.version();
361+
} else{
362+
pl.build();
363+
}
364+
335365
});
366+
336367
};

test/runner.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var runner = require('../node_modules/grunt-contrib-qunit/tasks/qunit.js');
2+
var plnode = require('plnode');
3+
4+
runner.run({
5+
code: "../builder/patternlab.js",
6+
tests: "tests.js"
7+
}, callback);

test/tests.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div id="qunit"></div>
1010
<div id="qunit-fixture"></div>
1111
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
12+
<script src="runner.js"></script>
1213
<script src="tests.js"></script>
1314
</body>
1415
</html>

test/tests.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
13
test( "hello test", function() {
2-
ok( 2 == "2", "Passed!" );
4+
ok( 2 == "2", "Passed!" );
5+
});
6+
7+
test( "hello patternlab", function() {
8+
console.log(plnode);
9+
ok( 2 == "2", "Passed!" );
310
});

0 commit comments

Comments
 (0)