Skip to content

Commit b96aba9

Browse files
committed
adjust legacy filesearch for new API
1 parent 4fe5cc3 commit b96aba9

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

packages/resolve-url-loader-filesearch/index.js.js

+62-55
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,84 @@
55
'use strict';
66

77
const path = require('path');
8-
const { createJoinFunction, defaultJoinGenerator, defaultJoinOperation } = require('resolve-url-loader');
8+
const { createJoinFunction, createJoinImplementation, defaultJoinGenerator } = require('resolve-url-loader');
99

10-
module.exports = (options = {}) => {
11-
const includeRoot = !!options.includeRoot;
12-
const attempts = Math.max(0, options.attempts) || 1E+9;
13-
const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json'];
10+
const generator = function* (item, options, loader) {
11+
if (item.isAbsolute) {
12+
for (let tuple of defaultJoinGenerator(item, options, loader)) {
13+
yield tuple;
14+
}
15+
} else {
16+
const includeRoot = !!options.includeRoot;
17+
const attempts = Math.max(0, options.attempts) || 1E+9;
18+
const breakOnFile = [].concat(options.breakOnFile) || ['package.json', 'bower.json'];
19+
const resolvedRoot = options.root || process.cwd();
1420

15-
const predicate = typeof options.predicate === 'function' ?
16-
options.testIsRunning :
17-
((fs, absolutePath) => breakOnFile
18-
.map((file) => path.resolve(absolutePath, file))
19-
.every((file) => !fs.existsSync(file) || !fs.statSync(file).isFile()));
21+
const isFile = (absolutePath) => {
22+
try {
23+
return loader.fs.statSync(absolutePath).isFile();
24+
} catch (error) {
25+
return false;
26+
}
27+
};
28+
29+
const isDirectory = (absolutePath) => {
30+
try {
31+
return loader.fs.statSync(absolutePath).isDirectory();
32+
} catch (error) {
33+
return false;
34+
}
35+
};
2036

21-
const baseGenerator = typeof options.generator === 'function' ?
22-
options.generator :
23-
defaultJoinGenerator;
37+
const predicate = typeof options.predicate === 'function' ?
38+
options.predicate :
39+
((fs, absolutePath) => breakOnFile
40+
.map((file) => path.resolve(absolutePath, file))
41+
.every((absolutePath) => !isFile(absolutePath)));
2442

25-
const searchingGeneartor = (filename, uri, bases, isAbsolute, {root, fs}) => {
26-
const resolvedRoot = !!root && path.resolve(root) || process.cwd();
2743
const testWithinLimit = (absolutePath) => {
28-
var relative = path.relative(resolvedRoot, absolutePath);
44+
const relative = path.relative(resolvedRoot, absolutePath);
2945
return !!relative && (relative.slice(0, 2) !== '..');
3046
};
3147

3248
const enqueue = (queue, excludes, basePath) =>
33-
fs.readdirSync(basePath)
49+
loader.fs.readdirSync(basePath)
3450
.filter((filename) => filename.charAt(0) !== '.')
3551
.map((filename) => path.join(basePath, filename))
36-
.filter((absolutePath) => fs.existsSync(absolutePath) && fs.statSync(absolutePath).isDirectory())
52+
.filter(isDirectory)
3753
.filter((absolutePath) => !excludes.contains(absolutePath))
38-
.filter((absolutePath) => predicate(fs, absolutePath))
54+
.filter((absolutePath) => predicate(loader.fs, absolutePath))
3955
.forEach((absolutePath) => queue.push(absolutePath));
4056

41-
return function* () {
42-
for (let base of baseGenerator(filename, uri, bases, isAbsolute, options)) {
43-
// #69 limit searching: make at least one attempt
44-
let remaining = attempts;
57+
for (let [absoluteStart, uri] of defaultJoinGenerator(item, options, loader)) {
58+
// #69 limit searching: make at least one attempt
59+
let remaining = attempts;
4560

46-
// find path to the root, stopping at cwd or at explicit boundary
47-
const pathToRoot = [];
48-
let isWorking;
49-
let absoluteStart = path.resolve(base);
50-
do {
51-
pathToRoot.push(absoluteStart);
52-
isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart);
53-
absoluteStart = path.resolve(absoluteStart, '..');
54-
} while (isWorking);
61+
// find path to the root, stopping at cwd or at explicit boundary
62+
const pathToRoot = [];
63+
let isWorking;
64+
do {
65+
pathToRoot.push(absoluteStart);
66+
isWorking = testWithinLimit(absoluteStart) && predicate(absoluteStart);
67+
absoluteStart = path.resolve(absoluteStart, '..');
68+
} while (isWorking);
5569

56-
// #62 support stylus nib: optionally force that path to include the root
57-
const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0);
58-
const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []);
70+
// #62 support stylus nib: optionally force that path to include the root
71+
const appendRoot = includeRoot && (pathToRoot.indexOf(resolvedRoot) < 0);
72+
const queue = pathToRoot.concat(appendRoot ? resolvedRoot : []);
5973

60-
// the queue pattern ensures that we favour paths closest the the start path
61-
// process the queue until empty or until we exhaust our attempts
62-
while (queue.length && (remaining-- > 0)) {
63-
var basePath = queue.shift();
64-
yield basePath;
65-
enqueue(queue, pathToRoot, basePath);
66-
}
74+
// the queue pattern ensures that we favour paths closest the the start path
75+
// process the queue until empty or until we exhaust our attempts
76+
while (queue.length && (remaining-- > 0)) {
77+
const base = queue.shift();
78+
yield [base, uri];
79+
enqueue(queue, pathToRoot, base);
6780
}
68-
};
69-
};
70-
71-
// only decorate relative URIs
72-
const generator = (filename, uri, bases, isAbsolute, options) =>
73-
(isAbsolute ? baseGenerator : searchingGeneartor)(filename, uri, bases, isAbsolute, options);
74-
75-
return createJoinFunction({
76-
name: 'resolve-url-loader-filesearch',
77-
scheme: 'alstroemeria',
78-
generator: generator,
79-
operation: defaultJoinOperation
80-
});
81+
}
82+
}
8183
};
84+
85+
module.exports = createJoinFunction(
86+
'resolve-url-loader-filesearch',
87+
createJoinImplementation(generator)
88+
);

0 commit comments

Comments
 (0)