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

Commit 2e0d5a9

Browse files
Merge pull request #17 from pattern-lab/dev
plugin-node-tab 2.0
2 parents c13eec8 + ed40aaf commit 2e0d5a9

File tree

5 files changed

+40
-59
lines changed

5 files changed

+40
-59
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,40 @@ To add the Tab Plugin to your project using [npm](http://npmjs.com/) type:
1616

1717
Or add it directly to your project's `package.json` file and run `npm install`
1818

19-
At time of installation, the plugin will prompt you for what filetypes you want to add tabs for.
19+
During installation, the plugin is added as a key to the `plugins` object in your main Pattern Lab project's `patternlab-config.json` file
2020

21-
```
22-
$ Specify filetype(s) to create a tab for. Separate multiple filetypes with a space, pipe or comma. Example: js css >>>
23-
```
21+
## Configuration
2422

25-
Post-installation, the plugin should be added as a key to the `plugins` object in your main Pattern Lab project's `patternlab-config.json` file.
23+
Post-installation, you will see the following in your `patternlab-config.json`:
2624

2725
Example:
2826

2927
```
3028
"plugins": {
3129
"plugin-node-tab": {
3230
"enabled": true,
33-
"initialized": false
31+
"initialized": false,
32+
"options": {
33+
"tabsToAdd": []
34+
}
3435
}
3536
}
3637
```
3738

39+
Add file extensions to this array as strings. Example: `"tabsToAdd": ['scss', 'js']`. You are all set now.
40+
3841
## Expected Structure
3942

4043
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>>`.
4144

42-
For example, if we added a` css` tab:
45+
For example, if we added an `scss` tab:
4346

4447
```
4548
./_patterns/foo/bar
4649
├── pattern.mustache (the pattern template)
4750
├── pattern.md (optional pattern-specific documentation and metadata)
4851
├── pattern.json (optional pattern-specific data)
49-
└── pattern.css (the tab you added.)
52+
└── pattern.scss (a file matching the tab you added.)
5053
```
5154

5255
## Enabling / Disabling the Plugin

config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tabsToAdd":[]
3+
}

index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
const pluginName = 'plugin-node-tab';
44

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');
5+
const fs = require('fs-extra');
6+
const glob = require('glob');
7+
const path = require('path');
8+
const EOL = require('os').EOL;
9+
const tab_loader = require('./src/tab-loader');
10+
const config = require('./config.json');
11+
12+
function writeConfigToOutput(patternlab, pluginConfig) {
13+
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
14+
try {
15+
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
16+
} catch (ex) {
17+
console.trace(pluginName + ': Error occurred while writing pluginFile configuration');
18+
console.log(ex);
19+
}
20+
}
1021

1122
function onPatternIterate(patternlab, pattern) {
1223
tab_loader(patternlab, pattern);
@@ -49,10 +60,11 @@ function pluginInit(patternlab) {
4960
process.exit(1);
5061
}
5162

52-
let fileTypes = require('./package.json').fileTypes;
53-
5463
//write the plugin json to public/patternlab-components
5564
var pluginConfig = getPluginFrontendConfig();
65+
pluginConfig.tabsToAdd = patternlab.config.plugins[pluginName].options.tabsToAdd;
66+
writeConfigToOutput(patternlab, pluginConfig);
67+
5668
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
5769
try {
5870
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
@@ -90,8 +102,8 @@ function pluginInit(patternlab) {
90102
//we are also being a bit lazy here, since we only expect one file
91103
let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
92104
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());
105+
for (let j = 0; j < pluginConfig.tabsToAdd.length; j++) {
106+
let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, pluginConfig.tabsToAdd[j]).replace(/<<typeUC>>/g, pluginConfig.tabsToAdd[j].toUpperCase());
95107
snippetString += tabSnippetLocal + EOL;
96108
}
97109
tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString);

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
"name": "plugin-node-tab",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "",
55
"main": "index.js",
66
"dependencies": {
77
"fs-extra": "^0.30.0",
8-
"glob": "^7.0.0",
9-
"inquirer": "^1.1.3"
8+
"glob": "^7.0.0"
109
},
1110
"repository": {
1211
"type": "git",

postinstall.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
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-
1+
console.log('Pattern Lab Node Plugin - "plugin-node-tab" installed. ');
2+
console.log('Configure or disable this plugin inside your patternlab-config.json file.');
3+
console.log('Add tabs to the Pattern Lab UI by adding file extensions to the "tabsToAdd" array on the plugins.plugin-node-tab.options object.');

0 commit comments

Comments
 (0)