Skip to content

Commit 2aa0f8f

Browse files
committed
feat(package): revamp plugin-tab
support for uikits support scoped package
1 parent a52b487 commit 2aa0f8f

File tree

6 files changed

+94
-1030
lines changed

6 files changed

+94
-1030
lines changed

packages/plugin-tab/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ For example, if we added an `scss` tab:
5656

5757
## Enabling / Disabling the Plugin
5858

59-
After install, you may manually enable or disable the plugin by finding the `plugin-node-tab` key within your main Pattern Lab project's `patternlab-config.json` file and setting the `enabled` flag. In the future this will be possible via CLI.
59+
After install, you may manually enable or disable the plugin by finding the `@pattern-lab/plugin-tab` key within your main Pattern Lab project's `patternlab-config.json` file and setting the `enabled` flag. In the future this will be possible via CLI.

packages/plugin-tab/index.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
'use strict';
22

3-
const pluginName = 'plugin-node-tab';
3+
const path = require('path');
4+
const EOL = require('os').EOL;
45

6+
const _ = require('lodash');
57
const fs = require('fs-extra');
68
const glob = require('glob');
7-
const path = require('path');
8-
const EOL = require('os').EOL;
9+
910
const tab_loader = require('./src/tab-loader');
1011

12+
const pluginName = 'pattern-lab-plugin-tab';
13+
1114
function writeConfigToOutput(patternlab, pluginConfig) {
12-
var pluginConfigPathName = path.resolve(
13-
patternlab.config.paths.public.root,
14-
'patternlab-components',
15-
'packages'
16-
);
1715
try {
18-
fs.outputFileSync(
19-
pluginConfigPathName + '/' + pluginName + '.json',
20-
JSON.stringify(pluginConfig, null, 2)
21-
);
16+
_.each(patternlab.uikits, uikit => {
17+
fs.outputFileSync(
18+
path.join(
19+
process.cwd(),
20+
uikit.outputDir,
21+
patternlab.config.paths.public.root,
22+
'patternlab-components',
23+
'packages',
24+
`/${pluginName}.json`
25+
),
26+
JSON.stringify(pluginConfig, null, 2)
27+
);
28+
});
2229
} catch (ex) {
2330
console.trace(
2431
pluginName + ': Error occurred while writing pluginFile configuration'
@@ -48,7 +55,7 @@ function registerEvents(patternlab) {
4855
*/
4956
function getPluginFrontendConfig() {
5057
return {
51-
name: 'pattern-lab/' + pluginName,
58+
name: pluginName,
5259
templates: [],
5360
stylesheets: [],
5461
javascripts: [
@@ -68,33 +75,38 @@ function getPluginFrontendConfig() {
6875
* Instead, alter getPluginFrontendConfig() and registerEvents() methods
6976
*/
7077
function pluginInit(patternlab) {
78+
console.log('PLUGIN INIT!');
79+
7180
if (!patternlab) {
72-
console.error('patternlab object not provided to plugin-init');
81+
console.error('patternlab object not provided to pluginInit');
7382
process.exit(1);
7483
}
7584

7685
//write the plugin json to public/patternlab-components
77-
var pluginConfig = getPluginFrontendConfig();
86+
const pluginConfig = getPluginFrontendConfig();
87+
88+
console.log(patternlab.config.plugins);
89+
7890
pluginConfig.tabsToAdd =
7991
patternlab.config.plugins[pluginName].options.tabsToAdd;
8092
writeConfigToOutput(patternlab, pluginConfig);
8193

82-
var pluginConfigPathName = path.resolve(
83-
patternlab.config.paths.public.root,
84-
'patternlab-components',
85-
'packages'
86-
);
87-
try {
88-
fs.outputFileSync(
89-
pluginConfigPathName + '/' + pluginName + '.json',
90-
JSON.stringify(pluginConfig, null, 2)
91-
);
92-
} catch (ex) {
93-
console.trace(
94-
'plugin-node-tab: Error occurred while writing pluginFile configuration'
95-
);
96-
console.log(ex);
97-
}
94+
// const pluginConfigPathName = path.resolve(
95+
// patternlab.config.paths.public.root,
96+
// 'patternlab-components',
97+
// 'packages'
98+
// );
99+
// try {
100+
// fs.outputFileSync(
101+
// pluginConfigPathName + '/' + pluginName + '.json',
102+
// JSON.stringify(pluginConfig, null, 2)
103+
// );
104+
// } catch (ex) {
105+
// console.trace(
106+
// 'plugin-tab: Error occurred while writing pluginFile configuration'
107+
// );
108+
// console.log(ex);
109+
// }
98110

99111
//add the plugin config to the patternlab-object
100112
if (!patternlab.plugins) {
@@ -103,22 +115,22 @@ function pluginInit(patternlab) {
103115
patternlab.plugins.push(pluginConfig);
104116

105117
//write the plugin dist folder to public/pattern-lab
106-
var pluginFiles = glob.sync(__dirname + '/dist/**/*');
118+
const pluginFiles = glob.sync(__dirname + '/dist/**/*');
107119

108120
if (pluginFiles && pluginFiles.length > 0) {
109-
let tab_frontend_snippet = fs.readFileSync(
121+
const tab_frontend_snippet = fs.readFileSync(
110122
path.resolve(__dirname + '/src/snippet.js'),
111123
'utf8'
112124
);
113125

114126
for (let i = 0; i < pluginFiles.length; i++) {
115127
try {
116-
var fileStat = fs.statSync(pluginFiles[i]);
128+
const fileStat = fs.statSync(pluginFiles[i]);
117129
if (fileStat.isFile()) {
118-
var relativePath = path
130+
const relativePath = path
119131
.relative(__dirname, pluginFiles[i])
120132
.replace('dist', ''); //dist is dropped
121-
var writePath = path.join(
133+
const writePath = path.join(
122134
patternlab.config.paths.public.root,
123135
'patternlab-components',
124136
'pattern-lab',
@@ -134,10 +146,10 @@ function pluginInit(patternlab) {
134146
//in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json
135147
//we are also being a bit lazy here, since we only expect one file
136148
let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
137-
var snippetString = '';
149+
let snippetString = '';
138150
if (pluginConfig.tabsToAdd && pluginConfig.tabsToAdd.length > 0) {
139151
for (let j = 0; j < pluginConfig.tabsToAdd.length; j++) {
140-
let tabSnippetLocal = tab_frontend_snippet
152+
const tabSnippetLocal = tab_frontend_snippet
141153
.replace(/<<type>>/g, pluginConfig.tabsToAdd[j])
142154
.replace(
143155
/<<typeUC>>/g,
@@ -149,12 +161,18 @@ function pluginInit(patternlab) {
149161
'/*SNIPPETS*/',
150162
snippetString
151163
);
152-
fs.outputFileSync(writePath, tabJSFileContents);
164+
console.log(161, tabJSFileContents);
165+
_.each(patternlab.uikits, uikit => {
166+
fs.outputFileSync(
167+
path.join(process.cwd(), uikit.outputDir, writePath),
168+
tabJSFileContents
169+
);
170+
});
153171
}
154172
}
155173
} catch (ex) {
156174
console.trace(
157-
'plugin-node-tab: Error occurred while copying pluginFile',
175+
'plugin-tab: Error occurred while copying pluginFile',
158176
pluginFiles[i]
159177
);
160178
console.log(ex);

0 commit comments

Comments
 (0)