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

Commit

Permalink
Merge pull request #17 from pattern-lab/dev
Browse files Browse the repository at this point in the history
plugin-node-tab 2.0
  • Loading branch information
bmuenzenmeyer authored Feb 21, 2017
2 parents c13eec8 + ed40aaf commit 2e0d5a9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 59 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,40 @@ To add the Tab Plugin to your project using [npm](http://npmjs.com/) type:

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

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

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

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.
Post-installation, you will see the following in your `patternlab-config.json`:

Example:

```
"plugins": {
"plugin-node-tab": {
"enabled": true,
"initialized": false
"initialized": false,
"options": {
"tabsToAdd": []
}
}
}
```

Add file extensions to this array as strings. Example: `"tabsToAdd": ['scss', 'js']`. You are all set now.

## Expected Structure

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

For example, if we added a` css` tab:
For example, if we added an `scss` tab:

```
./_patterns/foo/bar
├── pattern.mustache (the pattern template)
├── pattern.md (optional pattern-specific documentation and metadata)
├── pattern.json (optional pattern-specific data)
└── pattern.css (the tab you added.)
└── pattern.scss (a file matching the tab you added.)
```

## Enabling / Disabling the Plugin
Expand Down
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabsToAdd":[]
}
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

const pluginName = 'plugin-node-tab';

const fs = require('fs-extra'),
glob = require('glob'),
path = require('path'),
EOL = require('os').EOL,
tab_loader = require('./src/tab-loader');
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const EOL = require('os').EOL;
const tab_loader = require('./src/tab-loader');
const config = require('./config.json');

function writeConfigToOutput(patternlab, pluginConfig) {
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
try {
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
} catch (ex) {
console.trace(pluginName + ': Error occurred while writing pluginFile configuration');
console.log(ex);
}
}

function onPatternIterate(patternlab, pattern) {
tab_loader(patternlab, pattern);
Expand Down Expand Up @@ -49,10 +60,11 @@ function pluginInit(patternlab) {
process.exit(1);
}

let fileTypes = require('./package.json').fileTypes;

//write the plugin json to public/patternlab-components
var pluginConfig = getPluginFrontendConfig();
pluginConfig.tabsToAdd = patternlab.config.plugins[pluginName].options.tabsToAdd;
writeConfigToOutput(patternlab, pluginConfig);

var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
try {
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2));
Expand Down Expand Up @@ -90,8 +102,8 @@ function pluginInit(patternlab) {
//we are also being a bit lazy here, since we only expect one file
let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
var snippetString = '';
for (let j = 0; j < fileTypes.length; j++) {
let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, fileTypes[j]).replace(/<<typeUC>>/g, fileTypes[j].toUpperCase());
for (let j = 0; j < pluginConfig.tabsToAdd.length; j++) {
let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, pluginConfig.tabsToAdd[j]).replace(/<<typeUC>>/g, pluginConfig.tabsToAdd[j].toUpperCase());
snippetString += tabSnippetLocal + EOL;
}
tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString);
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "plugin-node-tab",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"fs-extra": "^0.30.0",
"glob": "^7.0.0",
"inquirer": "^1.1.3"
"glob": "^7.0.0"
},
"repository": {
"type": "git",
Expand Down
42 changes: 3 additions & 39 deletions postinstall.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
'use strict';

const fs = require('fs-extra'),
glob = require('glob'),
inquirer = require('inquirer');

let config = require('./package.json');
let fileTypes = [];

var questions = [
{
type: 'input',
name: 'types',
message: 'Specify filetype(s) to create a tab for. Separate multiple filetypes with a space, pipe or comma. Example: js css >>> '
}
];

inquirer
.prompt(questions)
.then(function (answers) {

fileTypes = answers.types.split(/,| /);

if (fileTypes.length === 1 && fileTypes[0] === '') {
console.log('No filetype(s) provided. Returning unconfigured!');
return;
}

for (let i = 0; i < fileTypes.length; i++) {
if (fileTypes[i].charAt(0) === '.') {
fileTypes[i] = fileTypes[i].slice(1);
}
}

console.log('Adding configuration for tabs', fileTypes, 'inside package.json');
config.fileTypes = fileTypes;
fs.outputFileSync('./package.json', JSON.stringify(config, null, 2), 'utf-8');
});

console.log('Pattern Lab Node Plugin - "plugin-node-tab" installed. ');
console.log('Configure or disable this plugin inside your patternlab-config.json file.');
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 comments on commit 2e0d5a9

Please sign in to comment.