Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit ae18e70

Browse files
committed
end to end hard-coded test of plugin - reading .scss files and displaying
part of pattern-lab/patternlab-node#432
1 parent f194aec commit ae18e70

File tree

4 files changed

+126
-38
lines changed

4 files changed

+126
-38
lines changed

dist/js/plugin-node-tab.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var PluginTab = {
2+
3+
init: function() {
4+
5+
Panels.add({
6+
'id': 'sg-panel-scss',
7+
'name': 'SCSS',
8+
'default': false,
9+
'templateID': 'pl-panel-template-code',
10+
'httpRequest': true,
11+
'httpRequestReplace': '.scss',
12+
'httpRequestCompleted': false,
13+
'prismHighlight': true,
14+
'language': 'markup',
15+
'keyCombo': 'ctrl+shift+z'
16+
});
17+
18+
}
19+
};

dist/js/plugin-tab.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

index.js

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,96 @@
1-
"use strict";
1+
'use strict';
2+
3+
const pluginName = 'plugin-node-tab';
24

35
var fs = require('fs-extra'),
46
glob = require('glob'),
5-
path = require('path');
7+
path = require('path'),
8+
tab_loader = require('./src/tab-loader');
9+
10+
function onPatternIterate(patternlab, pattern) {
11+
tab_loader(patternlab, pattern);
12+
}
13+
14+
/**
15+
* Define what events you wish to listen to here
16+
* @param patternlab - global data store which has the handle to the event emitter
17+
*/
18+
function registerEvents(patternlab){
19+
20+
//TODO: list all possible events
21+
patternlab.events.on('patternlab-pattern-write-end', onPatternIterate);
22+
}
23+
24+
/**
25+
* A single place to define the frontend configuration
26+
*/
27+
function getPluginFrontendConfig() {
28+
return {
29+
'name':'pattern-lab\/' + pluginName,
30+
'templates':[],
31+
'stylesheets':[],
32+
'javascripts':['patternlab-components\/pattern-lab\/' + pluginName + '\/js\/' + pluginName + '.js'],
33+
'onready':'PluginTab.init()',
34+
'callback':''
35+
}
36+
}
637

7-
function plugin_init(patternlab) {
38+
/**
39+
* The entry point for the plugin. You should not have to alter this code.
40+
* Instead, alter getPluginFrontendConfig() and registerEvents() methods
41+
*/
42+
function pluginInit(patternlab) {
843

944
if(!patternlab) {
1045
console.error('patternlab object not provided to plugin-init');
1146
process.exit(1);
1247
}
1348

1449
//write the plugin json to public/patternlab-components
15-
var pluginConfig = {
16-
"name":"pattern-lab\/plugin-node-tab",
17-
"templates":[],
18-
"stylesheets":[],
19-
"javascripts":["\/js\/plugin-tab.js"],
20-
"onready":"PluginTab.init()",
21-
"callback":""
22-
}
23-
50+
var pluginConfig = getPluginFrontendConfig();
2451
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
2552
try {
26-
fs.outputFileSync(pluginConfigPathName + '/plugin-tab.json', JSON.stringify(pluginConfig, null, 2));
53+
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
2754
} catch (ex) {
28-
console.trace('Error occurred while writing pluginFile configuration');
55+
console.trace('plugin-node-tab: Error occurred while writing pluginFile configuration');
2956
console.log(ex);
3057
}
3158

59+
//add the plugin config to the patternlab-object
60+
if (!patternlab.plugins) {
61+
patternlab.plugins = [];
62+
}
63+
patternlab.plugins.push(pluginConfig);
64+
3265
//write the plugin dist folder to public/pattern-lab
33-
var pluginFiles = glob.sync(__dirname +'/dist/**/*');
66+
var pluginFiles = glob.sync(__dirname + '/dist/**/*');
3467

3568
if (pluginFiles && pluginFiles.length > 0) {
3669
for (let i = 0; i < pluginFiles.length; i++) {
3770
try {
3871
var fileStat = fs.statSync(pluginFiles[i]);
3972
if (fileStat.isFile()) {
4073
var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); //dist is dropped
41-
var writePath = path.join(patternlab.config.paths.public.root,'patternlab-components', 'pattern-lab', 'plugin-node-tab', relativePath);
74+
var writePath = path.join(patternlab.config.paths.public.root,'patternlab-components', 'pattern-lab', pluginName, relativePath);
4275
fs.copySync(pluginFiles[i], writePath);
4376
}
4477
} catch (ex) {
45-
console.trace('Error occurred while copying pluginFile', pluginFiles[i]);
78+
console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]);
4679
console.log(ex);
4780
}
4881
}
4982
}
5083

84+
//setup listeners if not already active
85+
if (patternlab.config[pluginName] !== undefined && !patternlab.config[pluginName]) {
86+
87+
//register events
88+
registerEvents(patternlab);
89+
90+
//set the plugin key to true to indicate it is installed and ready
91+
patternlab.config[pluginName] = true;
92+
}
93+
5194
}
5295

53-
module.exports = plugin_init;
96+
module.exports = pluginInit;

src/tab-loader.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
var fs = require('fs-extra'),
4+
path = require('path');
5+
6+
function findTab(patternlab, pattern) {
7+
8+
if (!patternlab) {
9+
console.error('plugin-node-tab: patternlab object not provided to findTab');
10+
process.exit(1);
11+
}
12+
13+
if (!pattern) {
14+
console.error('plugin-node-tab: pattern object not provided to findTab');
15+
process.exit(1);
16+
}
17+
18+
//derive the custom filetype paths from the pattern relPath
19+
var customFileTypePath = path.join(patternlab.config.paths.source.patterns, pattern.relPath);
20+
customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + ".scss";
21+
var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.scss');
22+
23+
//look for a custom filetype for this template
24+
try {
25+
var tabFileName = path.resolve(customFileTypePath);
26+
try {
27+
var tabFileNameStats = fs.statSync(tabFileName);
28+
} catch (err) {
29+
//not a file
30+
}
31+
if (tabFileNameStats && tabFileNameStats.isFile()) {
32+
if (patternlab.config.debug) {
33+
console.log('plugin-node-tab: copied pattern-specific custom file for ' + pattern.patternPartial);
34+
}
35+
fs.copySync(tabFileName, customFileTypeOutputPath);
36+
} else {
37+
fs.outputFileSync(customFileTypeOutputPath, '');
38+
}
39+
}
40+
catch (err) {
41+
console.log('plugin-node-tab:There was an error parsing sibling JSON for ' + currentPattern.relPath);
42+
console.log(err);
43+
}
44+
}
45+
46+
module.exports = findTab;

0 commit comments

Comments
 (0)