UNPKG

edp-core

Version:

[![NPM version](https://img.shields.io/npm/v/edp-core.svg?style=flat-square)](https://npmjs.org/package/edp-core) [![Build Status](https://img.shields.io/travis/ecomfe/edp-core/master.svg?style=flat-square)](https://travis-ci.org/ecomfe/edp-core) [![Depen

137 lines (119 loc) 3.77 kB
/** * @file 路径相关的一些函数 * @author leeight(liyubei@baidu.com) */ var fs = require('fs'); var path = require('path'); var util = require('util'); var minimatch = require('minimatch'); /** * 判断url是否本地路径 * * @param {string} url 路径 * @return {boolean} */ exports.isLocalPath = function (url) { // url('//www.baidu.com/img/logo.gif') // url('http://www.baidu.com/img/logo.gif') // url('https://www.baidu.com/img/logo.gif') return !(/^\/\//.test(url) || /^[a-z]{2,10}:/i.test(url)); }; /** * 判断url是否相对路径 * * @param {string} url 路径 * @return {boolean} */ exports.isRelativePath = function (url) { return !/^([a-z]{2,10}:\/)?\//i.test(url); }; (function () { function normalize(sourcePath) { return sourcePath.replace(/\\/g, '/'); } [ 'normalize', 'join', 'resolve', 'relative', 'dirname', 'basename', 'extname' ].forEach(function (method) { exports[method] = function () { return normalize(path[method].apply(path, arguments)); }; }); })(); /** * @param {string} str 需要转义的字符串. * @return {string} */ function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } /** * 判断路径是否满足规则 * * @param {string} path 源路径 * @param {string} pattern 路径规则 * @param {fs.Stats=} fileStat 路径所代表的文件状态对象 * @return {boolean} */ exports.satisfy = function (path, pattern, fileStat) { if (util.isRegExp(pattern)) { return pattern.test(path); } if (/^~/.test(pattern)) { // 自己扩展的模式 // ~esui return new RegExp('^' + escapeRegExp(pattern.substr(1)) + '\\b').test(path); } // If the pattern ends with a “/” // it would only find a match with a directory if (pattern.lastIndexOf('/') === pattern.length - 1) { pattern = pattern.slice(0, pattern.length - 1); if (fileStat && (!fileStat.isDirectory())) { return false; } } var matchBase = pattern.indexOf('/') === -1; return minimatch(path, pattern, { matchBase: matchBase }); }; /** * 以给定的目录为起点,获取项目或包的根目录 * * @param {string} [cwd] 指定开始查找的目录,默认为当前目录 * @return {string} */ exports.getRootDirectory = function (cwd) { // 有`.edpproj`文件夹的就是项目根目录,有`package.json`的是包根目录, // 但是由于存在`node_modules`这种文件夹,里面会有其它的`package.json`, // 因此包根目录的查找并不是非常精准的 // // 出于这个问题,项目根目录找的是最近的那个目录,而包的根目录找的是最远的那个 // // // 这里全部使用edp.path,防止require('path')和edp.path路径的不一致, // 主要是由于edp.path做了一次normalize var projectRoot = exports.resolve(cwd || process.cwd()); var packageRoot = null; var systemRoot = exports.resolve('/'); while (projectRoot !== systemRoot) { // 如果是项目根就直接返回,不用再找了 if (fs.existsSync(exports.join(projectRoot, '.edpproj'))) { return projectRoot; } // 包根目录记录后不直接返回,还要继教往上找 if (fs.existsSync(exports.join(projectRoot, 'package.json'))) { packageRoot = projectRoot; } projectRoot = exports.resolve(projectRoot, '..'); } if (packageRoot) { return packageRoot; } throw new Error('未找到项目/包文件夹,请确保当前目录在项目/包内'); };