pretest.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var fs = require("fs-extra");
  3. var cwd = process.cwd();
  4. var path = require("path");
  5. var buildDir = path.join(cwd, "build");
  6. if (fs.existsSync(buildDir)) {
  7. console.log("Removing old build folder...");
  8. fs.removeSync(buildDir);
  9. console.log("Done.");
  10. }
  11. var modernizr = require("modernizr");
  12. modernizr.metadata(function (metadata) {
  13. var props = metadata.map(function (data) {
  14. return {
  15. name: data.name,
  16. path: data.amdPath.replace("test/", ""),
  17. property: data.property,
  18. cssclass: data.cssclass
  19. };
  20. });
  21. var jsProps = props.filter(function (data) {
  22. return data.property;
  23. });
  24. var jsTests = {};
  25. jsProps.forEach(function (prop) {
  26. var sections = prop.path.split("/");
  27. var maxLength = sections.length - 1;
  28. var current = jsTests;
  29. if (sections.length === 1) {
  30. current.standalone = current.standalone || {};
  31. current.standalone[sections[0]] = prop.property;
  32. } else {
  33. sections.forEach(function (sect, idx) {
  34. if (idx === maxLength) {
  35. current[sect] = prop.property;
  36. } else {
  37. current[sect] = current[sect] || {};
  38. }
  39. current = current[sect];
  40. });
  41. }
  42. });
  43. var string = JSON.stringify(jsTests, null, "\t");
  44. // Convert "foo": "bar" -> "foo": Modernizr.bar
  45. string = string.replace(/(\:(?:\s)?)\"(.*)\"/g, "$1Modernizr.$2");
  46. // Convert [ "foo": "bar" ] -> [ "foo": Modernizr["bar"] ]
  47. string = string.replace(/(\t{3})\"(.*)\"/g, "$1Modernizr[\"$2\"]");
  48. // Convert Modernizr.foo-bar -> Modernizr["foo-bar"]
  49. string = string.replace(/\.((?:[\w]+)?\-(?:[\w]+))/g, "[\"$1\"]");
  50. // Save test/js/vanilla.js
  51. (function () {
  52. var vanilla = [
  53. "/* jshint sub: true */",
  54. "var Modernizr = window.Modernizr;",
  55. "var tests = " + string + ";"
  56. ].join("\n\n");
  57. fs.writeFileSync(path.join(cwd, "test", "js", "vanilla.js"), vanilla);
  58. }());
  59. // Save test/js/amd.js
  60. (function () {
  61. var amd = [
  62. "/* jshint sub: true */\n/* global define */",
  63. "define([\"Modernizr\"], function (M) {",
  64. " var tests = " + string.replace(/\n/g, "\n\t").replace(/Modernizr/g, "M") + ";",
  65. " return M;",
  66. "});"
  67. ].join("\n\n");
  68. fs.writeFileSync(path.join(cwd, "test", "js", "amd.js"), amd);
  69. }());
  70. // Save test/css/vanilla.css
  71. (function () {
  72. var cssProps = props.filter(function (data) {
  73. return data.cssclass;
  74. }).map(function (data, idx) {
  75. var cssclass = Array.isArray(data.cssclass) ? data.cssclass : [data.cssclass];
  76. var negateclass = (idx % 2 ? "no-" : "");
  77. return "." + negateclass + cssclass.join(", .");
  78. });
  79. var css = [
  80. cssProps.join(",\n") + " {",
  81. "\tbackground: red;",
  82. "}\n\n"
  83. ].join("\n");
  84. // Take a few classes and add a prefix for testing.
  85. var prefixed = [
  86. ".prefixed-cors",
  87. ".prefixed-input",
  88. ".prefixed-no-smil"
  89. ];
  90. css += [
  91. prefixed.join(",\n") + " {",
  92. "\tbackground: blue;",
  93. "}\n"
  94. ].join("\n");
  95. fs.writeFileSync(path.join(cwd, "test", "css", "vanilla.css"), css);
  96. }());
  97. var missingData = props.filter(function (data) {
  98. return !data.property && !data.cssclass;
  99. }).map(function (data) {
  100. return data.path;
  101. });
  102. if (missingData.length) {
  103. console.log("\n");
  104. console.log("The following tests have no metadata:");
  105. console.log();
  106. console.log(missingData.join("\n"));
  107. }
  108. });