123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
-
-
- 'use strict';
-
- var fs = require('fs');
- var path = require('path');
-
-
-
- module.exports = function detect(filepath, options) {
- if (!filepath || (typeof filepath !== 'string')) {
- return null;
- }
- if (fs.existsSync(filepath)) {
- return path.resolve(filepath);
- }
-
- options = options || {};
- if (options.nocase === true) {
- return nocase(filepath);
- }
- return null;
- };
-
-
-
- function nocase(filepath) {
- filepath = path.resolve(filepath);
- var res = tryReaddir(filepath);
- if (res === null) {
- return null;
- }
-
-
-
- if (res.path === filepath) {
- return res.path;
- }
-
-
-
-
- var upper = filepath.toUpperCase();
- var len = res.files.length;
- var idx = -1;
-
- while (++idx < len) {
- var fp = path.resolve(res.path, res.files[idx]);
- if (filepath === fp || upper === fp) {
- return fp;
- }
- var fpUpper = fp.toUpperCase();
- if (filepath === fpUpper || upper === fpUpper) {
- return fp;
- }
- }
-
- return null;
- }
-
-
-
- function tryReaddir(filepath) {
- var ctx = { path: filepath, files: [] };
- try {
- ctx.files = fs.readdirSync(filepath);
- return ctx;
- } catch (err) {}
- try {
- ctx.path = path.dirname(filepath);
- ctx.files = fs.readdirSync(ctx.path);
- return ctx;
- } catch (err) {}
- return null;
- }
|