/
build-resources.js
46 lines (42 loc) 路 1.29 KB
/
build-resources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import path from "path";
import { max, padEnd } from "lodash/fp";
import Observable from "zen-observable";
import build from "./build";
import getTargets from "./get-targets";
import getSourceSets from "./get-source-sets";
import writeEach from "./write-each";
import serverRequire from "./server-require";
export default buildResources;
function buildResources(resources, target, context) {
return new Observable(observer => {
const idPad = padEnd(max(resources.map(r => r.id.length)));
build(resources, {
async read(source, _, count) {
observer.next(
`${context.verbose ? "Resources: " : ""}${idPad(
source.id
)} ${count}/${resources.length}`
);
return await source.content;
},
async write(source, set) {
const baseName = path.basename(set.id);
const dirName = path.dirname(set.id);
const filePath = path.resolve(
target,
dirName,
`${baseName}.${set.type}`
);
return writeEach(source, [filePath]);
},
done() {
observer.next(
`${context.verbose
? "Resources: "
: ""}${resources.length}/${resources.length}`
);
observer.complete();
}
}).catch(err => observer.error(err));
});
}