123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 'use strict';
- const path = require('path');
- const globToRegExp = require('glob-to-regexp');
- module.exports = normalizeOptions;
- let isWindows = /^win/.test(process.platform);
- function normalizeOptions (options, internalOptions) {
- if (options === null || options === undefined) {
- options = {};
- }
- else if (typeof options !== 'object') {
- throw new TypeError('options must be an object');
- }
- let recurseDepth, recurseFn, recurseRegExp, recurseGlob, deep = options.deep;
- if (deep === null || deep === undefined) {
- recurseDepth = 0;
- }
- else if (typeof deep === 'boolean') {
- recurseDepth = deep ? Infinity : 0;
- }
- else if (typeof deep === 'number') {
- if (deep < 0 || isNaN(deep)) {
- throw new Error('options.deep must be a positive number');
- }
- else if (Math.floor(deep) !== deep) {
- throw new Error('options.deep must be an integer');
- }
- else {
- recurseDepth = deep;
- }
- }
- else if (typeof deep === 'function') {
- recurseDepth = Infinity;
- recurseFn = deep;
- }
- else if (deep instanceof RegExp) {
- recurseDepth = Infinity;
- recurseRegExp = deep;
- }
- else if (typeof deep === 'string' && deep.length > 0) {
- recurseDepth = Infinity;
- recurseGlob = globToRegExp(deep, { extended: true, globstar: true });
- }
- else {
- throw new TypeError('options.deep must be a boolean, number, function, regular expression, or glob pattern');
- }
- let filterFn, filterRegExp, filterGlob, filter = options.filter;
- if (filter !== null && filter !== undefined) {
- if (typeof filter === 'function') {
- filterFn = filter;
- }
- else if (filter instanceof RegExp) {
- filterRegExp = filter;
- }
- else if (typeof filter === 'string' && filter.length > 0) {
- filterGlob = globToRegExp(filter, { extended: true, globstar: true });
- }
- else {
- throw new TypeError('options.filter must be a function, regular expression, or glob pattern');
- }
- }
- let sep = options.sep;
- if (sep === null || sep === undefined) {
- sep = path.sep;
- }
- else if (typeof sep !== 'string') {
- throw new TypeError('options.sep must be a string');
- }
- let basePath = options.basePath;
- if (basePath === null || basePath === undefined) {
- basePath = '';
- }
- else if (typeof basePath === 'string') {
-
- if (basePath && basePath.substr(-1) !== sep) {
- basePath += sep;
- }
- }
- else {
- throw new TypeError('options.basePath must be a string');
- }
-
-
- let posixBasePath = basePath;
- if (posixBasePath && sep !== '/') {
- posixBasePath = posixBasePath.replace(new RegExp('\\' + sep, 'g'), '/');
-
- if (isWindows) {
-
- posixBasePath = posixBasePath.replace(/^([a-zA-Z]\:\/|\/\/)/, '/');
- }
- }
-
- let facade;
- if (options.fs === null || options.fs === undefined) {
-
- facade = internalOptions.facade;
- }
- else if (typeof options.fs === 'object') {
-
- facade = Object.assign({}, internalOptions.facade);
- facade.fs = Object.assign({}, internalOptions.facade.fs, options.fs);
- }
- else {
- throw new TypeError('options.fs must be an object');
- }
- return {
- recurseDepth,
- recurseFn,
- recurseRegExp,
- recurseGlob,
- filterFn,
- filterRegExp,
- filterGlob,
- sep,
- basePath,
- posixBasePath,
- facade,
- emit: !!internalOptions.emit,
- stats: !!internalOptions.stats,
- };
- }
|