Skip to content

Commit ba83de7

Browse files
committed
Merge branch 'post-install' into ui-rewrite
2 parents acaa876 + c7ec81e commit ba83de7

File tree

9 files changed

+109
-47
lines changed

9 files changed

+109
-47
lines changed

core/lib/markdown_parser.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
"use strict";
22

33
var md = require('markdown-it')();
4+
var yaml = require('js-yaml');
45

56
var markdown_parser = function () {
67

8+
/**
9+
* Converts a markdown block with frontmatter (each is optional, technically) to a well-formed object.
10+
* @param block - the ".md" file, which can contain frontmatter or not, or only frontmatter.
11+
* @returns an object with any frontmatter keys, plus a .markdown key
12+
*/
713
function parseMarkdownBlock(block) {
814
var returnObject = {};
915

1016
try {
1117
//for each block process the yaml frontmatter and markdown
1218
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
1319
var chunks = frontmatterRE.exec(block);
14-
if (chunks && chunks[1]) {
15-
16-
//convert each yaml frontmatter key / value into an object key
17-
var frontmatter = chunks[1];
18-
var frontmatterLines = frontmatter.split(/\n/gm);
19-
for (var j = 0; j < frontmatterLines.length; j++) {
20-
21-
var frontmatterLine = frontmatterLines[j];
22-
if (frontmatterLine.length > 0) {
23-
24-
var frontmatterLineChunks = frontmatterLine.split(':'); //test this
25-
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
26-
var frontmatterValueString = frontmatterLineChunks[1].trim();
27-
28-
returnObject[frontmatterKey] = frontmatterValueString;
29-
}
3020

21+
if (chunks) {
22+
//we got some frontmatter
23+
if (chunks && chunks[1]) {
24+
//parse the yaml if we got it
25+
var frontmatter = chunks[1];
26+
returnObject = yaml.safeLoad(frontmatter);
3127
}
32-
}
3328

34-
if (chunks && chunks[2]) {
35-
//parse the actual markdown
36-
returnObject.markdown = md.render(chunks[2]);
29+
if (chunks[2]) {
30+
//parse the actual markdown if it exists
31+
returnObject.markdown = md.render(chunks[2]);
32+
} else {
33+
returnObject.markdown = '';
34+
}
3735
} else {
38-
//assume the passed in block is raw markdown
36+
//assume the block was only markdown
3937
returnObject.markdown = md.render(block);
4038
}
4139
} catch (ex) {

core/lib/patternlab.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v2.3.0 - 2016
3-
*
1+
/*
2+
* patternlab-node - v2.4.0 - 2016
3+
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
5-
* Licensed under the MIT license.
6-
*
7-
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -21,11 +21,11 @@ var diveSync = require('diveSync'),
2121
function buildPatternData(dataFilesPath, fs) {
2222
var dataFilesPath = dataFilesPath;
2323
var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']});
24-
var mergeObject = {}
24+
var mergeObject = {};
2525
dataFiles.forEach(function (filePath) {
26-
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8')
27-
mergeObject = _.merge(mergeObject, jsonData)
28-
})
26+
var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8');
27+
mergeObject = _.merge(mergeObject, jsonData);
28+
});
2929
return mergeObject;
3030
}
3131

@@ -99,7 +99,6 @@ var patternlab_engine = function (config) {
9999
console.log(patternlab.package.version);
100100
}
101101

102-
103102
function help() {
104103

105104
console.log('');
@@ -185,7 +184,7 @@ var patternlab_engine = function (config) {
185184
}
186185

187186
function listStarterkits() {
188-
var starterkit_manager = new sm(patternlab);
187+
var starterkit_manager = new sm(patternlab.config);
189188
return starterkit_manager.list_starterkits();
190189
}
191190

@@ -273,7 +272,7 @@ var patternlab_engine = function (config) {
273272
//set pattern-specific header if necessary
274273
var head;
275274
if (patternlab.userHead) {
276-
head = patternlab.userHead.replace('{% pattern-lab-head %}', patternlab.header);
275+
head = patternlab.userHead;
277276
} else {
278277
head = patternlab.header;
279278
}

core/lib/starterkit_manager.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

3-
var starterkit_manager = function (pl) {
3+
var starterkit_manager = function (config) {
44
var path = require('path'),
55
fs = require('fs-extra'),
66
util = require('./utilities'),
7-
paths = pl.config.paths;
7+
paths = config.paths;
88

99
function loadStarterKit(starterkitName, clean) {
1010
try {
1111
var kitPath = path.resolve(
12-
path.join(process.cwd(), 'node_modules', starterkitName, pl.config.starterkitSubDir)
12+
path.join(process.cwd(), 'node_modules', starterkitName, config.starterkitSubDir)
1313
);
1414
console.log('Attempting to load starterkit from', kitPath);
1515
try {
@@ -49,6 +49,15 @@ var starterkit_manager = function (pl) {
4949

5050
}
5151

52+
function detectStarterKits() {
53+
var node_modules_path = path.join(process.cwd(), 'node_modules');
54+
var npm_modules = fs.readdirSync(node_modules_path).filter(function (dir) {
55+
var module_path = path.join(process.cwd(), 'node_modules', dir);
56+
return fs.statSync(module_path).isDirectory() && dir.indexOf('starterkit-') === 0;
57+
});
58+
return npm_modules;
59+
}
60+
5261
return {
5362
load_starterkit: function (starterkitName, clean) {
5463
loadStarterKit(starterkitName, clean);
@@ -58,6 +67,9 @@ var starterkit_manager = function (pl) {
5867
},
5968
pack_starterkit: function () {
6069
packStarterkit();
70+
},
71+
detect_starterkits: function () {
72+
return detectStarterKits();
6173
}
6274
};
6375

core/lib/utilities.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ var util = {
1515
console.log('\x1b[32m', message, '\x1b[0m');
1616
},
1717

18+
logOrange: function (message) {
19+
console.log('\x1b[33m', message, '\x1b[0m');
20+
},
21+
1822
logRed: function (message) {
1923
console.log('\x1b[41m', message, '\x1b[0m');
2024
},

core/scripts/postinstall.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
try{
3+
4+
console.log('Beginning Pattern Lab postinstall...');
5+
6+
var sm = require('../lib/starterkit_manager.js');
7+
var u = require('../lib/utilities.js');
8+
var path = require('path');
9+
var fs = require('fs-extra');
10+
11+
//get the config
12+
var configPath = path.resolve(process.cwd(), 'patternlab-config.json');
13+
var config = fs.readJSONSync(path.resolve(configPath), 'utf8');
14+
15+
//determine if any starterkits are already installed
16+
var starterkit_manager = new sm(config);
17+
var foundStarterkits = starterkit_manager.detect_starterkits();
18+
19+
//todo - enhance to support multiple kits with prompt for each or all
20+
if(foundStarterkits && foundStarterkits.length > 0) {
21+
starterkit_manager.load_starterkit(foundStarterkits[0], true);
22+
} else {
23+
console.log('No starterkits found to automatically load.')
24+
}
25+
u.logGreen('Pattern Lab postinstall complete.');
26+
27+
} catch (ex) {
28+
u.logOrange(ex);
29+
u.logOrange('An error occurred during Pattern Lab Node postinstall.');
30+
u.logOrange('Pattern Lab postinstall completed with errors.');
31+
}
32+
33+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "patternlab-node",
33
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
4-
"version": "2.3.0",
4+
"version": "2.4.0",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"diveSync": "^0.3.0",
88
"fs-extra": "^0.30.0",
99
"glob": "^7.0.0",
10+
"js-yaml": "^3.6.1",
1011
"json5": "^0.5.0",
1112
"lodash": "~4.13.1",
1213
"markdown-it": "^6.0.1",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
status: inprogress
3+
---

test/files/nav.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
el: #nav
2+
el: '#nav'
33
title : Navigation
44
---
55
Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions.

test/markdown_parser_tests.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,46 @@
22

33
var path = require('path');
44
var fs = require('fs-extra');
5-
var eol = require('os').EOL;
65
var mp = require('../core/lib/markdown_parser');
76
var markdown_parser = new mp();
87

98
exports['markdown_parser'] = {
10-
'parses pattern description block correctly when frontmatter not present' : function(test){
11-
//arrange
9+
'parses pattern description block correctly when frontmatter not present' : function(test) {
10+
//arrange
1211
var markdownFileName = path.resolve("./test/files/_patterns/00-test/00-foo.md");
1312
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
1413

15-
//act
16-
var returnObject = markdown_parser.parse(markdownFileContents)
14+
//act
15+
var returnObject = markdown_parser.parse(markdownFileContents);
1716

18-
//assert
17+
//assert
1918
test.equals(returnObject.markdown, '<h2>A Simple Include</h2>\n<p>This pattern contains an include of <code>test-bar</code>. It also has this markdown file, which does not have frontmatter.</p>\n');
2019
test.done();
2120
},
22-
'parses pattern description block correctly when frontmatter present' : function(test){
21+
'parses pattern description block correctly when frontmatter present' : function (test) {
2322
//arrange
2423
var markdownFileName = path.resolve("./test/files/_patterns/00-test/01-bar.md");
2524
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
2625

2726
//act
28-
var returnObject = markdown_parser.parse(markdownFileContents)
27+
var returnObject = markdown_parser.parse(markdownFileContents);
2928

3029
//assert
3130
test.equals(returnObject.markdown, '<h2>A Simple Bit of Markup</h2>\n<p>Foo cannot get simpler than bar, amiright?</p>\n');
3231
test.equals(returnObject.status, 'complete');
3332
test.done();
33+
},
34+
'parses frontmatter only when no markdown present': function (test) {
35+
//arrange
36+
var markdownFileName = path.resolve("./test/files/_patterns/00-test/03-styled-atom.md");
37+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
38+
39+
//act
40+
var returnObject = markdown_parser.parse(markdownFileContents);
41+
42+
//assert
43+
test.equals(returnObject.markdown, '');
44+
test.equals(returnObject.status, 'inprogress');
45+
test.done();
3446
}
3547
};

0 commit comments

Comments
 (0)