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

Commit 17bf7b5

Browse files
committed
Implement ability to specify more than one file extension
closes #2
1 parent d7b0f15 commit 17bf7b5

File tree

5 files changed

+75
-39
lines changed

5 files changed

+75
-39
lines changed

dist/js/plugin-node-tab.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
/* global Panels */
2-
31
var PluginTab = {
42

53
init: function () {
64

7-
Panels.add({
8-
'id': 'sg-panel-scss',
9-
'name': 'SCSS',
10-
'default': false,
11-
'templateID': 'pl-panel-template-code',
12-
'httpRequest': true,
13-
'httpRequestReplace': '.scss',
14-
'httpRequestCompleted': false,
15-
'prismHighlight': true,
16-
'language': 'markup',
17-
'keyCombo': 'ctrl+shift+z'
18-
});
5+
//placeholder that will be replaced during configuation
6+
/*SNIPPETS*/
197

208
}
219
};

index.js

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

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

5-
var fs = require('fs-extra'),
5+
const fs = require('fs-extra'),
66
glob = require('glob'),
77
path = require('path'),
8+
EOL = require('os').EOL,
89
tab_loader = require('./src/tab-loader');
910

1011
function onPatternIterate(patternlab, pattern) {
@@ -46,6 +47,8 @@ function pluginInit(patternlab) {
4647
process.exit(1);
4748
}
4849

50+
let fileTypes = require('./package.json').fileTypes;
51+
4952
//write the plugin json to public/patternlab-components
5053
var pluginConfig = getPluginFrontendConfig();
5154
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
@@ -66,13 +69,31 @@ function pluginInit(patternlab) {
6669
var pluginFiles = glob.sync(__dirname + '/dist/**/*');
6770

6871
if (pluginFiles && pluginFiles.length > 0) {
72+
73+
let tab_frontend_snippet = fs.readFileSync(path.resolve(__dirname + '/src/snippet.js'), 'utf8');
74+
6975
for (let i = 0; i < pluginFiles.length; i++) {
7076
try {
7177
var fileStat = fs.statSync(pluginFiles[i]);
7278
if (fileStat.isFile()) {
7379
var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); //dist is dropped
7480
var writePath = path.join(patternlab.config.paths.public.root, 'patternlab-components', 'pattern-lab', pluginName, relativePath);
75-
fs.copySync(pluginFiles[i], writePath);
81+
82+
//a message to future plugin authors:
83+
//depending on your plugin's job - you might need to alter the dist file instead of copying.
84+
//if you are simply copying dist files, you can probably do the below:
85+
//fs.copySync(pluginFiles[i], writePath);
86+
87+
//in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json
88+
//we are also being a bit lazy here, since we only expect one file
89+
let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
90+
var snippetString = '';
91+
for (let j = 0; j < fileTypes.length; j++) {
92+
let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, fileTypes[j]).replace(/<<typeUC>>/g, fileTypes[j].toUpperCase());
93+
snippetString += tabSnippetLocal + EOL;
94+
}
95+
tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString);
96+
fs.outputFileSync(writePath, tabJSFileContents);
7697
}
7798
} catch (ex) {
7899
console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]);

postinstall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ inquirer
2727
}
2828

2929
for (let i = 0; i < fileTypes.length; i++) {
30-
if (fileTypes[i].charAt(0) !== '.') {
31-
fileTypes[i] = '.' + fileTypes[i];
30+
if (fileTypes[i].charAt(0) === '.') {
31+
fileTypes[i] = fileTypes[i].slice(1);
3232
}
3333
}
3434

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+
});

src/tab-loader.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

3-
var fs = require('fs-extra'),
4-
path = require('path');
3+
const fs = require('fs-extra'),
4+
path = require('path'),
5+
fileTypes = require('./../package.json').fileTypes;
56

67
function findTab(patternlab, pattern) {
78

@@ -17,30 +18,42 @@ function findTab(patternlab, pattern) {
1718

1819
//derive the custom filetype paths from the pattern relPath
1920
var customFileTypePath = path.join(patternlab.config.paths.source.patterns, pattern.relPath);
20-
customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + ".scss";
21-
var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.scss');
2221

23-
//look for a custom filetype for this template
24-
try {
25-
var tabFileName = path.resolve(customFileTypePath);
22+
for (let i = 0; i < fileTypes.length; i++) {
23+
24+
customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + '.' + fileTypes[i];
25+
var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.' + fileTypes[i]);
26+
27+
//look for a custom filetype for this template
2628
try {
27-
var tabFileNameStats = fs.statSync(tabFileName);
28-
} catch (err) {
29-
//not a file
30-
}
31-
if (tabFileNameStats && tabFileNameStats.isFile()) {
32-
if (patternlab.config.debug) {
33-
console.log('plugin-node-tab: copied pattern-specific custom file for ' + pattern.patternPartial);
29+
var tabFileName = path.resolve(customFileTypePath);
30+
try {
31+
var tabFileNameStats = fs.statSync(tabFileName);
32+
} catch (err) {
33+
//not a file
34+
}
35+
if (tabFileNameStats && tabFileNameStats.isFile()) {
36+
if (patternlab.config.debug) {
37+
console.log('plugin-node-tab: copied pattern-specific custom file for ' + pattern.patternPartial);
38+
}
39+
fs.copySync(tabFileName, customFileTypeOutputPath);
40+
} else {
41+
fs.outputFileSync(customFileTypeOutputPath, '');
3442
}
35-
fs.copySync(tabFileName, customFileTypeOutputPath);
36-
} else {
37-
fs.outputFileSync(customFileTypeOutputPath, '');
3843
}
44+
catch (err) {
45+
console.log('plugin-node-tab:There was an error parsing sibling JSON for ' + pattern.relPath);
46+
console.log(err);
47+
}
48+
49+
50+
51+
3952
}
40-
catch (err) {
41-
console.log('plugin-node-tab:There was an error parsing sibling JSON for ' + pattern.relPath);
42-
console.log(err);
43-
}
53+
54+
55+
56+
4457
}
4558

4659
module.exports = findTab;

0 commit comments

Comments
 (0)