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

Commit 6c18513

Browse files
Merge pull request #8 from pattern-lab/dev
3...2...1... Launch: alpha!
2 parents e11bde8 + aa6c55f commit 6c18513

File tree

10 files changed

+411
-1
lines changed

10 files changed

+411
-1
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
tab_width = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true

.eslintrc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"builtin": true,
5+
"es6": true
6+
},
7+
"globals": {},
8+
"rules": {
9+
"block-scoped-var": 0,
10+
"camelcase": 0,
11+
"comma-spacing": [1, {"before": false, "after": true}],
12+
"consistent-return": 2,
13+
"curly": [2, "all"],
14+
"dot-notation": [1, { "allowKeywords": true }],
15+
"eqeqeq": [2, "allow-null"],
16+
"global-strict": [0, "never"],
17+
"guard-for-in": 2,
18+
"indent": [1, 2, {"SwitchCase": 1, "VariableDeclarator": 1}],
19+
"lines-around-comment": [1, {
20+
"beforeBlockComment": true,
21+
"beforeLineComment": true,
22+
"allowBlockStart": true,
23+
"allowObjectStart": true,
24+
"allowArrayStart": true
25+
}],
26+
"key-spacing": 0,
27+
"keyword-spacing": 1,
28+
"new-cap": 0,
29+
"no-alert": 2,
30+
"no-bitwise": 2,
31+
"no-caller": 2,
32+
"no-cond-assign": [2, "except-parens"],
33+
"no-debugger": 2,
34+
"no-dupe-args": 2,
35+
"no-dupe-keys": 2,
36+
"no-empty": 2,
37+
"no-eval": 2,
38+
"no-extend-native": 2,
39+
"no-extra-bind": 2,
40+
"no-extra-parens": 0,
41+
"no-extra-semi": 2,
42+
"no-func-assign": 2,
43+
"no-implied-eval": 2,
44+
"no-invalid-regexp": 2,
45+
"no-irregular-whitespace": 1,
46+
"no-iterator": 2,
47+
"no-loop-func": 2,
48+
"no-mixed-requires": 0,
49+
"no-multi-str": 2,
50+
"no-multi-spaces": 1,
51+
"no-native-reassign": 2,
52+
"no-new": 2,
53+
"no-param-reassign": 1,
54+
"no-proto": 2,
55+
"no-redeclare": 0,
56+
"no-script-url": 2,
57+
"no-self-assign": 2,
58+
"no-self-compare": 2,
59+
"no-sequences": 2,
60+
"no-shadow": 2,
61+
"no-undef": 2,
62+
"no-underscore-dangle": 0,
63+
"no-unreachable": 1,
64+
"no-unused-vars": 0,
65+
"no-use-before-define": 1,
66+
"no-useless-call": 2,
67+
"no-useless-concat": 2,
68+
"no-with": 2,
69+
"quotes": [0, "single"],
70+
"radix": 2,
71+
"semi": [0, "never"],
72+
"strict": 0,
73+
"space-before-blocks": 1,
74+
"space-before-function-paren": [1, {
75+
"anonymous": "always",
76+
"named": "never"
77+
}],
78+
"space-in-parens": [1, "never"],
79+
"space-infix-ops": 1,
80+
"valid-typeof": 2,
81+
"vars-on-top": 0,
82+
"wrap-iife": [2, "inside"]
83+
}
84+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
.idea/

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# plugin-node-tab
1+
![license](https://img.shields.io/github/license/pattern-lab/plugin-node-tab.svg)
2+
[![npm](https://img.shields.io/npm/v/plugin-node-tab.svg)](https://www.npmjs.com/package/plugin-node-tab)
3+
[![Gitter](https://img.shields.io/gitter/room/pattern-lab/php.svg)](https://gitter.im/pattern-lab/php)
4+
5+
# Tab Plugin for Pattern Lab Node
6+
7+
The Tab Plugin allows Pattern Lab Node users to see sibling files next to a pattern in the filesystem, displaying them as additional tabs alongside the template and HTML tabs in both the Style Guide frontend and the single-pattern info modal.
8+
9+
## Installation
10+
11+
To add the Tab Plugin to your project using [npm](http://npmjs.com/) type:
12+
13+
npm install plugin-node-tab --save
14+
15+
Or add it directly to your project's `package.json` file and run `npm install`
16+
17+
Post installation, the plugin will prompt you for what filetypes you want to add tabs for.
18+
19+
```
20+
$ Specify filetype(s) to create a tab for. Separate multiple filetypes with a space, pipe or comma. Example: js css >>>
21+
```
22+
23+
## Expected Structure
24+
25+
With the Tab Plugin installed, you can now accompany pattern template files with the file types of your choice and expect Pattern Lab to show them as tabs. The file structure would be similar to that of `pattern.json` or `pattern.md` files, except that it will be `pattern.<<type>>`.
26+
27+
For example, if we added a` css` tab:
28+
29+
```
30+
./_patterns/foo/bar
31+
├── pattern.mustache (the pattern template)
32+
├── pattern.md (optional pattern-specific documentation and metadata)
33+
├── pattern.json (optional pattern-specific data)
34+
└── pattern.css (the tab you added.)
35+
```
36+
37+
## Enabling / Disabling the Plugin
38+
39+
After install, you may manually enable or disable the plugin by removing it from the `patternlab-config.json` file. Installation added a key called `plugin-node-tab` to it. In the future this will be possible via CLI.

dist/js/plugin-node-tab.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var PluginTab = {
2+
3+
/**
4+
* The function defined as the onready callback within the plugin configuration.
5+
*/
6+
init: function () {
7+
8+
//placeholder that will be replaced during configuation
9+
//most plugins could probably just implement logic here instead.
10+
/*SNIPPETS*/
11+
12+
}
13+
};

index.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
const pluginName = 'plugin-node-tab';
4+
5+
const fs = require('fs-extra'),
6+
glob = require('glob'),
7+
path = require('path'),
8+
EOL = require('os').EOL,
9+
tab_loader = require('./src/tab-loader');
10+
11+
function onPatternIterate(patternlab, pattern) {
12+
tab_loader(patternlab, pattern);
13+
}
14+
15+
/**
16+
* Define what events you wish to listen to here
17+
* For a full list of events - check out https://github.com/pattern-lab/patternlab-node/wiki/Creating-Plugins#events
18+
* @param patternlab - global data store which has the handle to the event emitter
19+
*/
20+
function registerEvents(patternlab) {
21+
//register our handler at the appropriate time of execution
22+
patternlab.events.on('patternlab-pattern-write-end', onPatternIterate);
23+
}
24+
25+
/**
26+
* A single place to define the frontend configuration
27+
* This configuration is outputted to the frontend explicitly as well as included in the plugins object.
28+
*
29+
*/
30+
function getPluginFrontendConfig() {
31+
return {
32+
'name':'pattern-lab\/' + pluginName,
33+
'templates':[],
34+
'stylesheets':[],
35+
'javascripts':['patternlab-components\/pattern-lab\/' + pluginName + '\/js\/' + pluginName + '.js'],
36+
'onready':'PluginTab.init()',
37+
'callback':''
38+
}
39+
}
40+
41+
/**
42+
* The entry point for the plugin. You should not have to alter this code much under many circumstances.
43+
* Instead, alter getPluginFrontendConfig() and registerEvents() methods
44+
*/
45+
function pluginInit(patternlab) {
46+
47+
if (!patternlab) {
48+
console.error('patternlab object not provided to plugin-init');
49+
process.exit(1);
50+
}
51+
52+
let fileTypes = require('./package.json').fileTypes;
53+
54+
//write the plugin json to public/patternlab-components
55+
var pluginConfig = getPluginFrontendConfig();
56+
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
57+
try {
58+
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
59+
} catch (ex) {
60+
console.trace('plugin-node-tab: Error occurred while writing pluginFile configuration');
61+
console.log(ex);
62+
}
63+
64+
//add the plugin config to the patternlab-object
65+
if (!patternlab.plugins) {
66+
patternlab.plugins = [];
67+
}
68+
patternlab.plugins.push(pluginConfig);
69+
70+
//write the plugin dist folder to public/pattern-lab
71+
var pluginFiles = glob.sync(__dirname + '/dist/**/*');
72+
73+
if (pluginFiles && pluginFiles.length > 0) {
74+
75+
let tab_frontend_snippet = fs.readFileSync(path.resolve(__dirname + '/src/snippet.js'), 'utf8');
76+
77+
for (let i = 0; i < pluginFiles.length; i++) {
78+
try {
79+
var fileStat = fs.statSync(pluginFiles[i]);
80+
if (fileStat.isFile()) {
81+
var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); //dist is dropped
82+
var writePath = path.join(patternlab.config.paths.public.root, 'patternlab-components', 'pattern-lab', pluginName, relativePath);
83+
84+
//a message to future plugin authors:
85+
//depending on your plugin's job - you might need to alter the dist file instead of copying.
86+
//if you are simply copying dist files, you can probably do the below:
87+
//fs.copySync(pluginFiles[i], writePath);
88+
89+
//in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json
90+
//we are also being a bit lazy here, since we only expect one file
91+
let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
92+
var snippetString = '';
93+
for (let j = 0; j < fileTypes.length; j++) {
94+
let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, fileTypes[j]).replace(/<<typeUC>>/g, fileTypes[j].toUpperCase());
95+
snippetString += tabSnippetLocal + EOL;
96+
}
97+
tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString);
98+
fs.outputFileSync(writePath, tabJSFileContents);
99+
}
100+
} catch (ex) {
101+
console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]);
102+
console.log(ex);
103+
}
104+
}
105+
}
106+
107+
//setup listeners if not already active
108+
if (patternlab.config[pluginName] !== undefined && !patternlab.config[pluginName]) {
109+
110+
//register events
111+
registerEvents(patternlab);
112+
113+
//set the plugin key to true to indicate it is installed and ready
114+
patternlab.config[pluginName] = true;
115+
}
116+
117+
}
118+
119+
module.exports = pluginInit;

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "plugin-node-tab",
3+
"version": "1.0.0-alpha",
4+
"description": "",
5+
"main": "index.js",
6+
"dependencies": {
7+
"fs-extra": "^0.30.0",
8+
"glob": "^7.0.0",
9+
"inquirer": "^1.1.3"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/pattern-lab/plugin-node-tab.git"
14+
},
15+
"scripts": {
16+
"test": "eslint dist/** src/** index.js postinstall.js",
17+
"postinstall": "node ./postinstall.js"
18+
},
19+
"author": "Brian Muenzenmeyer",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/pattern-lab/plugin-node-tab/issues"
23+
},
24+
"homepage": "https://github.com/pattern-lab/plugin-node-tab#readme",
25+
"devDependencies": {
26+
"eslint": "^3.5.0"
27+
}
28+
}

postinstall.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra'),
4+
glob = require('glob'),
5+
inquirer = require('inquirer');
6+
7+
let config = require('./package.json');
8+
let fileTypes = [];
9+
10+
var questions = [
11+
{
12+
type: 'input',
13+
name: 'types',
14+
message: 'Specify filetype(s) to create a tab for. Separate multiple filetypes with a space, pipe or comma. Example: js css >>> '
15+
}
16+
];
17+
18+
inquirer
19+
.prompt(questions)
20+
.then(function (answers) {
21+
22+
fileTypes = answers.types.split(/,| /);
23+
24+
if (fileTypes.length === 1 && fileTypes[0] === '') {
25+
console.log('No filetype(s) provided. Returning unconfigured!');
26+
return;
27+
}
28+
29+
for (let i = 0; i < fileTypes.length; i++) {
30+
if (fileTypes[i].charAt(0) === '.') {
31+
fileTypes[i] = fileTypes[i].slice(1);
32+
}
33+
}
34+
35+
console.log('Adding configuration for tabs', fileTypes, 'inside package.json');
36+
config.fileTypes = fileTypes;
37+
fs.outputFileSync('./package.json', JSON.stringify(config, null, 2), 'utf-8');
38+
});
39+

src/snippet.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* global Panels */
2+
3+
Panels.add({
4+
'id': 'sg-panel-<<type>>',
5+
'name': '<<typeUC>>',
6+
'default': false,
7+
'templateID': 'pl-panel-template-code',
8+
'httpRequest': true,
9+
'httpRequestReplace': '.<<type>>',
10+
'httpRequestCompleted': false,
11+
'prismHighlight': true,
12+
'language': 'markup',
13+
'keyCombo': 'ctrl+shift+z'
14+
});

0 commit comments

Comments
 (0)