UNPKG

4.09 kBJavaScriptView Raw
1"use strict";
2
3var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4
5var _curry = require("generic-jsx").curry;
6
7function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
8
9const { mkdirp, tstat, readdir, copy, read } = require("./fs-sync");
10const { find: findTransform, transform } = require("./transform");
11const toMatcher = require("./to-matcher");
12const { refine, deref, set, exists, stem } = require("@njudah/cursor");
13const { relative, join, basename, extname, resolve } = require("path");
14const { stringify } = JSON;
15const getChecksum = require("@njudah/get-checksum");
16const { fromJS, Map } = require("immutable");
17const uuid = require("uuid");
18
19module.exports.build = function build({ source, destination, cache, state, children = [], ignore }) {
20 const ignoreMatcher = toMatcher.mcall(refine(state, "ignore"), ignore, destination, "**/.*");
21 const metadata = refine(state, "metadata");
22 const unique = uuid.mcall(refine(state, "uuid"));
23 const root = resolve(source);
24
25 return _curry(
26 stem,
27 null,
28 _curry(mkdirp, { path: destination }),
29 _curry(mkdirp, { path: cache }),
30 _curry(item, { root: root,
31 source: root,
32 state: refine(state, "item"),
33 transforms: transform.optimize.await(refine(state, "optimize"), children),
34 ignore: ignoreMatcher,
35 cache: cache,
36 metadata: metadata,
37 destination: join(destination, unique) })
38 );
39};
40
41module.exports.transform = require("./transform");
42
43function item(_ref) {
44 let { source, state, ignore } = _ref,
45 rest = _objectWithoutProperties(_ref, ["source", "state", "ignore"]);
46
47 if (ignore.mcall(refine(state, "ignored"), source)) return "ignored";
48
49 const which = tstat.mcall(refine(state, "type"), { path: source });
50 const type = which === "file" ? file : directory;
51
52 return _curry(type, _extends({ source: source,
53 ignore: ignore
54 }, rest, {
55 state: refine(state, "implementation") }));
56}
57
58function file({ source, cache, transforms, state, metadata, destination, root }) {
59 const { transform, checksum: transformChecksum } = findTransform.mcall(refine(state, "find-checksum"), source, transforms) || {};
60
61 if (!transform) return _curry(copy, { source: source, destination: destination });
62
63 const contents = read({ path: source, encoding: "utf-8" });
64 const fileChecksum = getChecksum(contents);
65 const extension = extname(source);
66 const checksum = getChecksum(stringify({ transformChecksum, fileChecksum, extension }));
67
68 const artifactsPath = join(cache, checksum);
69 const rooted = join("~", relative(root, source));
70 const transformed = transform({ source: rooted, root, contents, destination: artifactsPath });
71
72 if (!exists(metadata) && transformed.metadata) set(metadata, fromJS(transformed.metadata));
73
74 return _curry(copy, { source: transformed.transformedPath,
75 destination: destination });
76}
77
78function directory({ source, cache, transforms, state, ignore, metadata, destination, root }) {
79 const children = readdir.mcall(refine(state, "children"), { path: source });
80
81 if (children.length <= 0 || !transforms) return _curry(stem, null);
82
83 if (!exists(metadata)) set(metadata, Map());
84
85 return _curry(
86 stem,
87 { path: source },
88 _curry(mkdirp, { path: destination }),
89 children.map(name => _curry(item, { root: root,
90 source: join(source, name),
91 ignore: ignore,
92 transforms: transforms,
93 state: refine(state, name),
94 cache: cache,
95 destination: join(destination, basename(name)),
96 metadata: refine(metadata, name) }))
97 );
98}
\No newline at end of file