Skip to content

Commit 807c6e9

Browse files
committed
pattern-lab#15 - Refactor to use pathExists asynchronously, works every time now!
1 parent 981afb6 commit 807c6e9

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

Diff for: src/tab-loader.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,34 @@ function findTab(patternlab, pattern) {
2626
}
2727

2828
//derive the custom filetype paths from the pattern relPath
29-
var customFileTypePath = path.join(patternlab.config.paths.source.patterns, pattern.relPath);
29+
let customFileTypePath = path.join(patternlab.config.paths.source.patterns, pattern.relPath);
3030

3131
//loop through all configured types
3232
for (let i = 0; i < fileTypes.length; i++) {
3333
const fileType = fileTypes[i].toLowerCase();
3434

3535
customFileTypePath = customFileTypePath.substr(0, customFileTypePath.lastIndexOf(".")) + '.' + fileType;
36-
var customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.' + fileType);
36+
let customFileTypeOutputPath = patternlab.config.paths.public.patterns + pattern.getPatternLink(patternlab, 'custom', '.' + fileType);
3737

38-
//look for a custom filetype for this template
3938
try {
40-
try {
41-
var tabFileName = path.resolve(customFileTypePath);
42-
var tabFileNameStats = fs.statSync(tabFileName);
43-
} catch (err) {
44-
//not a file - move on quietly
45-
}
46-
fs.pathExistsSync(tabFileName, (err, exists) => {
47-
if (exists) {
39+
let tabFileName = path.resolve(customFileTypePath);
40+
let tabFileNameOutput = path.resolve(customFileTypeOutputPath);
41+
42+
//look for a custom filetype for this template
43+
fs.pathExists(tabFileName, (err, exists) => {
44+
if (exists === true) {
4845
if (patternlab.config.debug) {
4946
console.log('plugin-node-tab: copied pattern-specific ' + fileType + ' file for ' + pattern.patternPartial);
5047
}
5148

5249
//copy the file to our output target if found
53-
fs.copySync(tabFileName, customFileTypeOutputPath);
50+
fs.copy(tabFileName, tabFileNameOutput, {overwrite: true});
5451
} else {
55-
52+
if (patternlab.config.debug) {
53+
console.log('plugin-node-tab: empty ' + fileType + ' file for ' + pattern.patternPartial + ' to prevent GET error');
54+
}
5655
//otherwise write nothing to the same location - this prevents GET errors on the tab.
57-
fs.outputFileSync(customFileTypeOutputPath, '');
56+
fs.outputFile(customFileTypeOutputPath, '');
5857
}
5958
});
6059

0 commit comments

Comments
 (0)