Skip to content

Commit 9cd2269

Browse files
committed
Preparatory refactoring: buildAll doesn't return a promise
1 parent e16cb10 commit 9cd2269

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

index.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ let readFile = promisify(require("fs").readFile);
66
let stat = promisify(require("fs").stat);
77

88
module.exports = (pluginConfig, assetManager, { watcher }) => {
9-
buildCopyAll(pluginConfig, assetManager).
10-
then(copyAll => {
11-
// Run once for all files
12-
copyAll();
9+
let copyAll = buildCopyAll(pluginConfig, assetManager);
1310

14-
if(watcher) {
15-
watcher.on("edit", copyAll);
16-
}
17-
});
11+
// Run once for all files
12+
copyAll();
13+
14+
if(watcher) {
15+
watcher.on("edit", copyAll);
16+
}
1817
};
1918

2019
function buildCopyAll(copyConfigs, assetManager) {
21-
let futureCopiers = copyConfigs.map(copyConfig =>
20+
let copiers = copyConfigs.map(copyConfig =>
2221
buildCopier(copyConfig, assetManager));
2322

24-
return Promise.all(futureCopiers).then(copiers => {
25-
return files => copiers.forEach(copier => copier(files));
26-
});
23+
return files => copiers.forEach(copier => copier(files));
2724
}
2825

2926
function buildCopier(copyConfig, assetManager) {
@@ -36,19 +33,25 @@ function buildCopier(copyConfig, assetManager) {
3633
filter: copyConfig.filter
3734
});
3835

39-
return stat(source).then(results => {
40-
// If `source` is a directory, `target` is used as target directory -
41-
// otherwise, `target`'s parent directory is used
42-
return results.isDirectory() ? target : path.dirname(target);
43-
}).then(targetDir => {
44-
let { fingerprint } = copyConfig;
45-
return files => {
46-
(files ? fileFinder.match(files) : fileFinder.all()).
47-
then(fileNames => processFiles(fileNames, {
48-
assetManager, source, target, targetDir, fingerprint
49-
}));
50-
};
51-
});
36+
let { fingerprint } = copyConfig;
37+
38+
return files => {
39+
Promise.all([
40+
(files ? fileFinder.match(files) : fileFinder.all()),
41+
determineTargetDir(source, target)
42+
]).then(([fileNames, targetDir]) => {
43+
return processFiles(fileNames, {
44+
assetManager, source, target, targetDir, fingerprint
45+
});
46+
});
47+
};
48+
}
49+
50+
// If `source` is a directory, `target` is used as target directory -
51+
// otherwise, `target`'s parent directory is used
52+
function determineTargetDir(source, target) {
53+
return stat(source).
54+
then(results => results.isDirectory() ? target : path.dirname(target));
5255
}
5356

5457
function processFiles(fileNames, config) {

0 commit comments

Comments
 (0)