Skip to content

Commit

Permalink
pattern-lab#15 - Refactor to use pathExists asynchronously, works eve…
Browse files Browse the repository at this point in the history
…ry time now!
  • Loading branch information
simonpioli committed Oct 16, 2017
1 parent 981afb6 commit 807c6e9
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/tab-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,34 @@ function findTab(patternlab, pattern) {
}

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

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

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

//look for a custom filetype for this template
try {
try {
var tabFileName = path.resolve(customFileTypePath);
var tabFileNameStats = fs.statSync(tabFileName);
} catch (err) {
//not a file - move on quietly
}
fs.pathExistsSync(tabFileName, (err, exists) => {
if (exists) {
let tabFileName = path.resolve(customFileTypePath);
let tabFileNameOutput = path.resolve(customFileTypeOutputPath);

//look for a custom filetype for this template
fs.pathExists(tabFileName, (err, exists) => {
if (exists === true) {
if (patternlab.config.debug) {
console.log('plugin-node-tab: copied pattern-specific ' + fileType + ' file for ' + pattern.patternPartial);
}

//copy the file to our output target if found
fs.copySync(tabFileName, customFileTypeOutputPath);
fs.copy(tabFileName, tabFileNameOutput, {overwrite: true});
} else {

if (patternlab.config.debug) {
console.log('plugin-node-tab: empty ' + fileType + ' file for ' + pattern.patternPartial + ' to prevent GET error');
}
//otherwise write nothing to the same location - this prevents GET errors on the tab.
fs.outputFileSync(customFileTypeOutputPath, '');
fs.outputFile(customFileTypeOutputPath, '');
}
});

Expand Down

0 comments on commit 807c6e9

Please sign in to comment.