@fontoxml/fontoxml-development-tools
Version:
Development tools for FontoXML.
49 lines (42 loc) • 1.5 kB
JavaScript
;
const fs = require('fs');
const nodePathModule = require('path');
function getStatForPath (path, indiscriminateOfSymlinks) {
/* istanbul ignore next: No symlinks in this package, so we won't test this for now */
const statMethodName = indiscriminateOfSymlinks ? 'statSync' : 'lstatSync';
const stat = Object.assign(fs[statMethodName](path), { path: path });
return {
path: stat.path,
symlink: stat.isSymbolicLink() ?
nodePathModule.resolve(nodePathModule.dirname(stat.path), fs.readlinkSync(stat.path)) :
false,
file: stat.isFile(),
directory: stat.isDirectory(),
size: stat.size,
accessed: stat.atime,
modified: stat.mtime,
changed: stat.ctime,
created: stat.birthtime
};
}
function getAllStatsInPath (dir, indiscriminateOfSymlinks) {
return fs.readdirSync(dir)
.map(file => nodePathModule.resolve(dir, file))
.map(path => {
try {
return getStatForPath(path, indiscriminateOfSymlinks);
}
catch (_error) {
// Do nothing
}
})
.filter(result => !!result);
}
module.exports = function getParentDirectoryContainingFileSync (cwd, configFileName) {
const rootDir = nodePathModule.parse(cwd).root;
const pathHasConfigFile = getAllStatsInPath(cwd, false)
.some(stat => stat.file && nodePathModule.basename(stat.path) === configFileName);
// @TODO: This needs more testing
return pathHasConfigFile ? cwd : (cwd === rootDir ? null :
getParentDirectoryContainingFileSync(nodePathModule.dirname(cwd), configFileName));
};