UNPKG

3.68 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 Call = Function.prototype.call.bind(Function.prototype.call);
10const Apply = Function.prototype.call.bind(Function.prototype.apply);
11
12const { base, getArguments } = require("generic-jsx");
13
14const getChecksum = require("@njudah/get-checksum");
15const toMatcher = require("./to-matcher");
16
17const isArray = Array.isArray;
18const isList = require("immutable").List.isList;
19const ArrayMap = Array.prototype.map;
20
21const { basename, dirname, join } = require("path");
22const { readFileSync, writeFileSync, existsSync } = require("fs");
23const { write, mkdirp, tstat, read } = require("./fs-sync");
24const { parse, stringify } = JSON;
25
26function 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
45function getMetadata(path) {
46 if (tstat({ path }) === "ENOENT") return undefined;
47
48 return parse(read({ path, encoding: "utf-8" }));
49}
50
51module.exports = transform;
52module.exports.transform = transform;
53
54function 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
69module.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
84module.exports.find = find;
85
86function 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
96module.exports.merge = merge;
\No newline at end of file