1 | "use strict";
|
2 |
|
3 | var _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 |
|
5 | var _curry = require("generic-jsx").curry;
|
6 |
|
7 | function _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 |
|
9 | const Call = Function.prototype.call.bind(Function.prototype.call);
|
10 | const Apply = Function.prototype.call.bind(Function.prototype.apply);
|
11 |
|
12 | const { base, getArguments } = require("generic-jsx");
|
13 |
|
14 | const getChecksum = require("@njudah/get-checksum");
|
15 | const toMatcher = require("./to-matcher");
|
16 |
|
17 | const isArray = Array.isArray;
|
18 | const isList = require("immutable").List.isList;
|
19 | const ArrayMap = Array.prototype.map;
|
20 |
|
21 | const { basename, dirname, join } = require("path");
|
22 | const { readFileSync, writeFileSync, existsSync } = require("fs");
|
23 | const { write, mkdirp, tstat, read } = require("./fs-sync");
|
24 | const { parse, stringify } = JSON;
|
25 |
|
26 | function transform({ source, root, contents, destination, children: [aFunction] }) {
|
27 | const encoding = "utf-8";
|
28 | const transformedPath = join(destination + "-contents.js");
|
29 | const metadataPath = join(destination + "-metdata.json");
|
30 |
|
31 | if (tstat({ path: transformedPath }) !== "ENOENT") return { transformedPath, metadata: getMetadata(metadataPath) };
|
32 |
|
33 | console.log("TRANSFORMING " + source);
|
34 |
|
35 | const { contents: transformedContents, metadata } = aFunction({ contents, source, root });
|
36 | const metadataContents = !!metadata && stringify(metadata, null, 2);
|
37 |
|
38 | write({ path: transformedPath, contents: transformedContents, encoding });
|
39 |
|
40 | if (metadata) write({ path: metadataPath, contents: metadataContents, encoding });
|
41 |
|
42 | return { transformedPath, metadata };
|
43 | }
|
44 |
|
45 | function getMetadata(path) {
|
46 | if (tstat({ path }) === "ENOENT") return undefined;
|
47 |
|
48 | return parse(read({ path, encoding: "utf-8" }));
|
49 | }
|
50 |
|
51 | module.exports = transform;
|
52 | module.exports.transform = transform;
|
53 |
|
54 | function find(aPath, transforms) {
|
55 | for (const transform of transforms) {
|
56 | const props = getArguments(transform);
|
57 |
|
58 | if (!toMatcher(props.ignore)(aPath) && toMatcher(props.match)(aPath)) {
|
59 | const { children: [child] } = getArguments(transform);
|
60 | const _getArguments = getArguments(child),
|
61 | { children } = _getArguments,
|
62 | attributes = _objectWithoutProperties(_getArguments, ["children"]);
|
63 |
|
64 | return { transform, checksum: attributes.checksum || getChecksum(JSON.stringify(attributes)) };
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 | module.exports.optimize = async function optimize(aTransform) {
|
70 | if (isArray(aTransform) || isList(aTransform)) return Promise.all(aTransform.map(aTransform => optimize(aTransform)));
|
71 |
|
72 | const { children: [child] } = getArguments(aTransform);
|
73 | const { optimize: optimization } = base(child);
|
74 |
|
75 | if (!optimization) return aTransform;
|
76 |
|
77 | return _curry(
|
78 | aTransform,
|
79 | null,
|
80 | (await optimization(child))
|
81 | );
|
82 | };
|
83 |
|
84 | module.exports.find = find;
|
85 |
|
86 | function merge(transforms, attributes) {
|
87 | return Call(ArrayMap, transforms || [], function (transform) {
|
88 | const { transformMatch, transformIgnore } = getArguments(transforms);
|
89 | const mergedMatch = match.merge(attributes.match, transformMatch);
|
90 | const mergedIgnore = match.merge(attributes.ignore, transformIgnore);
|
91 |
|
92 | return _curry(transform, _extends({}, mergedMatch, mergedIgnore));
|
93 | });
|
94 | }
|
95 |
|
96 | module.exports.merge = merge; |
\ | No newline at end of file |