Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions lib/newy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,48 @@ var PluginError = gutil.PluginError;
// check for missing directory
function checkMissingDir(destinationFile) {
var filePath = path.dirname(destinationFile);
var pathAbsolute = path.isAbsolute(filePath);
var directories = filePath.split(path.sep);
return transversePath(directories);
return transversePath(filePath);
}

function transversePath(directories) {
var pathBuild = '/';
var directoryMissing = directories.some(function(element) {
pathBuild = path.join(pathBuild, element);
try {
fs.statSync(pathBuild);
return false
} catch(e) {
return true;
function transversePath(filePath) {
var pathBuild = path.normalize(filePath);
var error = false;
if (!tryStatSync(pathBuild)) {
while (true) {
error = true;
var parentDirPath = path.dirname(pathBuild);
if (parentDirPath == pathBuild) {
// we are at the top level dir, dirname won't reduce either "C:\\" or "/"
break;
}
if (tryStatSync(parentDirPath)) {
break;
}
pathBuild = parentDirPath;
}
}
if (error) {
return pathBuild;
} else { return false; }

function tryStatSync(path) {
try {
fs.statSync(path);
return true;
}
catch (e) {
return false;
}
}
});

if (directoryMissing) {
return pathBuild;
} else { return false; }
}

var missingDirCache = {};

function consoleWarn(missingDir) {
if (missingDirCache.hasOwnProperty(missingDir)) {
return;
}
missingDirCache[missingDir] = 1;
console.log("-------------------- Newy Message ------------------" .magenta);
console.log("Warning - missing directory:" .bold .red);
console.log(missingDir .bold .red);
Expand Down