File size: 2,672 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const React = require("react");
const fs = require("fs");
const babel = require("babel-core");
const ReactDOMServer = require("react-dom/server");

let configsObject;
if (fs.existsSync("./configs.json")) {
  configsObject = require("./configs.json");
} else {
  configsObject = {};
}

const fetchExampleString = exampleName => {
  const exampleString = fs.readFileSync(`examples/${exampleName}.js`, "utf-8");
  return exampleString;
};

const extractConfig = exampleString => {
  const pattern = /(var|const)\s+settings\s*=\s*(\{(.|\n)+?\n\s*\};)/;
  let extraction = exampleString.match(pattern);
  if (extraction) extraction = extraction[2];
  else return null;
  const propPattern = /(\w+)\:((?:.|\n)+?)(?=(,\n)|(\n\s*};))/g;
  let match;
  let matchObject = {};
  do {
    match = propPattern.exec(extraction);
    if (!match) break;
    if (!matchObject[match[1]]) {
      matchObject[match[1]] = match[2].trim();
    }
  } while (match);
  return matchObject;
};

const extractChildren = exampleString => {
  const pattern = /\<Slider(?:.|\n)*?\>((.|\n)*?)\<\/Slider\>/;
  return exampleString.match(pattern)[1];
};

const transpile = exampleString =>
  babel.transform(exampleString, {
    plugins: [
      "transform-react-jsx",
      "babel-plugin-transform-object-rest-spread",
      "babel-plugin-transform-class-properties",
      "babel-plugin-transform-es2015-arrow-functions"
    ]
  }).code;

const fetchExampleConfigs = (fileName, index) => {
  const exampleName = fileName.substring(0, fileName.length - 3);
  const exampleString = fetchExampleString(exampleName);
  const transformedString = transpile(exampleString);
  let childrenString = extractChildren(exampleString.replace(/\=\>/g, "$$$")); // jsx type string
  try {
    // react string without jsx
    childrenString = eval(
      transpile(
        `<div name="${exampleName}">` + childrenString + "</div>"
      ).replace(/baseUrl/g, "'./img/react-slick'")
    );
    console.log("success");
  } catch (error) {
    childrenString = "";
    console.error("children error:", fileName);
  }
  childrenString = ReactDOMServer.renderToString(childrenString); // pure html string
  let config = extractConfig(transformedString);
  if (config) {
    configsObject[exampleName] = {
      props: config,
      children: childrenString
    };
  } else {
    console.log("config error:", fileName);
  }
};

const exampleFiles = fs
  .readdirSync("examples")
  .filter(file => file.endsWith(".js") && file[0] === file[0].toUpperCase())
  .forEach((fileName, index) => fetchExampleConfigs(fileName, index));
fs.writeFileSync(
  "examples/scripts/configs.json",
  JSON.stringify(configsObject, null, 4)
);