Skip to content

Commit 4807bce

Browse files
committed
Refactoring
1 parent fc211b8 commit 4807bce

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

lib/build-bundler.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let fs = require("fs");
2+
let path = require("path");
3+
let { createFile } = require("faucet-pipeline-util");
4+
// TODO: Not available on all supported Node versions
5+
let { promisify } = require("util");
6+
let readFile = promisify(fs.readFile);
7+
let readdir = promisify(fs.readdir);
8+
9+
module.exports = ({ source, target }) => {
10+
// TODO: Only copy changed files
11+
return _ => {
12+
readdir(source).then(sources => {
13+
return Promise.all(sources.map(filePath => {
14+
let sourcePath = path.join(source, filePath);
15+
let targetPath = path.join(target, filePath);
16+
17+
return readFile(sourcePath).then(content => {
18+
return createFile(targetPath, content);
19+
});
20+
}));
21+
});
22+
};
23+
};

lib/index.js

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
let buildConfig = require("./build-config");
2-
let fs = require("fs");
3-
let path = require("path");
4-
let { createFile } = require("faucet-pipeline-util");
2+
let buildBundler = require("./build-bundler");
53

64
module.exports = (rawConfig, configDir, { watcher, fingerprint }) => {
75
let config = buildConfig(rawConfig, configDir, fingerprint);
@@ -16,31 +14,11 @@ module.exports = (rawConfig, configDir, { watcher, fingerprint }) => {
1614
};
1715

1816
function buildBundleAll(config) {
19-
// TODO: Only copy changed files
20-
return _ => {
21-
config.bundles.forEach(bundle => {
22-
fs.readdir(bundle.source, (err, sources) => {
23-
// TODO: Error Handling
24-
if(err) {
25-
console.error(err);
26-
return;
27-
}
17+
let bundlers = config.bundles.map(bundleConfig => buildBundler(bundleConfig));
2818

29-
sources.forEach(filePath => {
30-
fs.readFile(path.join(bundle.source, filePath), (err, content) => {
31-
// TODO: Error Handling
32-
if(err) {
33-
console.error(err);
34-
return;
35-
}
36-
37-
// TODO: Fingerprint
38-
createFile(path.join(bundle.target, filePath), content);
39-
});
40-
});
41-
});
42-
// TODO: Manifest
43-
// TODO: Reporter
44-
});
19+
return files => {
20+
Promise.all(bundlers.map(bundler => bundler(files)));
4521
};
22+
// TODO: Manifest
23+
// TODO: Reporter
4624
}

0 commit comments

Comments
 (0)