diff --git a/.eslintrc b/.eslintrc index 0a23451..f2463e9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,6 +4,10 @@ "builtin": true, "es6": true }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, "globals": {}, "rules": { "block-scoped-var": 0, @@ -61,14 +65,14 @@ "no-undef": 2, "no-underscore-dangle": 0, "no-unreachable": 1, - "no-unused-vars": 0, + "no-unused-vars": 1, "no-use-before-define": 1, "no-useless-call": 2, "no-useless-concat": 2, "no-with": 2, "quotes": [0, "single"], "radix": 2, - "semi": [0, "never"], + "semi": [1, "always"], "strict": 0, "space-before-blocks": 1, "space-before-function-paren": [1, { diff --git a/README.md b/README.md index d9dc066..cd6897d 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ Or add it directly to your project's `package.json` file and run `npm install` During installation, the plugin is added as a key to the `plugins` object in your main Pattern Lab project's `patternlab-config.json` file +> If you don't see this object, try running `npm run postinstall` within the root of your project. + ## Configuration Post-installation, you will see the following in your `patternlab-config.json`: diff --git a/dist/js/plugin-node-tab.js b/dist/js/plugin-node-tab.js index a5b4902..9d582a7 100644 --- a/dist/js/plugin-node-tab.js +++ b/dist/js/plugin-node-tab.js @@ -1,7 +1,7 @@ var PluginTab = { /** - * The function defined as the onready callback within the plugin configuration. + * The function defined as the onready callback within the plugin configuration. */ init: function () { diff --git a/index.js b/index.js index 05c4f2b..e5c2be0 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ 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'); @@ -46,7 +45,7 @@ function getPluginFrontendConfig() { 'javascripts':['patternlab-components\/pattern-lab\/' + pluginName + '\/js\/' + pluginName + '.js'], 'onready':'PluginTab.init()', 'callback':'' - } + }; } /** @@ -102,12 +101,14 @@ 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 < pluginConfig.tabsToAdd.length; j++) { - let tabSnippetLocal = tab_frontend_snippet.replace(/<>/g, pluginConfig.tabsToAdd[j]).replace(/<>/g, pluginConfig.tabsToAdd[j].toUpperCase()); - snippetString += tabSnippetLocal + EOL; + if (pluginConfig.tabsToAdd && pluginConfig.tabsToAdd.length > 0) { + for (let j = 0; j < pluginConfig.tabsToAdd.length; j++) { + let tabSnippetLocal = tab_frontend_snippet.replace(/<>/g, pluginConfig.tabsToAdd[j]).replace(/<>/g, pluginConfig.tabsToAdd[j].toUpperCase()); + snippetString += tabSnippetLocal + EOL; + } + tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString); + fs.outputFileSync(writePath, tabJSFileContents); } - tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString); - fs.outputFileSync(writePath, tabJSFileContents); } } catch (ex) { console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]); diff --git a/package.json b/package.json index bac51f6..d84f95b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plugin-node-tab", - "version": "2.0.0", + "version": "2.0.2", "description": "", "main": "index.js", "dependencies": { @@ -12,7 +12,7 @@ "url": "git+https://github.com/pattern-lab/plugin-node-tab.git" }, "scripts": { - "test": "eslint dist/** src/** index.js postinstall.js", + "test": "eslint src/** index.js postinstall.js", "postinstall": "node ./postinstall.js" }, "author": "Brian Muenzenmeyer", diff --git a/postinstall.js b/postinstall.js index bd03ccc..b67a6f7 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,3 +1,4 @@ console.log('Pattern Lab Node Plugin - "plugin-node-tab" installed. '); +console.log('You may have to run `npm run postinstall` from your console as well.'); 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.'); diff --git a/src/tab-loader.js b/src/tab-loader.js index d5fd7c7..102295b 100644 --- a/src/tab-loader.js +++ b/src/tab-loader.js @@ -1,8 +1,7 @@ "use strict"; -const fs = require('fs-extra'), - path = require('path'), - fileTypes = require('./../package.json').fileTypes; +const fs = require('fs-extra'); +const path = require('path'); /** * The backend method that is called during the patternlab-pattern-write-end event. @@ -12,6 +11,9 @@ const fs = require('fs-extra'), */ function findTab(patternlab, pattern) { + //read the filetypes from the configuration + const fileTypes = patternlab.config.plugins['plugin-node-tab'].options.tabsToAdd; + //exit if either of these two parameters are missing if (!patternlab) { console.error('plugin-node-tab: patternlab object not provided to findTab'); @@ -28,9 +30,10 @@ function findTab(patternlab, pattern) { //loop through all configured types for (let i = 0; i < fileTypes.length; i++) { + const fileType = fileTypes[i].toLowerCase(); - customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + '.' + fileTypes[i]; - var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.' + fileTypes[i]); + customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + '.' + fileType; + var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.' + fileType); //look for a custom filetype for this template try {