build-pipeline.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. const typeDecorator = require("./decorators/typeDecorator");
  2. const outputDecorator = require("./decorators/outputDecorator");
  3. const articleDecorator = require("./decorators/articleDecorator");
  4. function toNameContentObject(items) {
  5. return items.reduce(
  6. (contentObject, item) =>
  7. Object.assign({}, contentObject, {
  8. [item.name]: item.content
  9. }),
  10. {}
  11. );
  12. }
  13. module.exports = {
  14. steps: [
  15. [
  16. {
  17. func: "rmrf",
  18. item: {
  19. path: "./output"
  20. }
  21. }
  22. ],
  23. [
  24. {
  25. name: "Read in markdown for index",
  26. func: "listDirectory",
  27. selector: state => [
  28. { path: "./content", tags: ["markdown"], outputExtension: ".html" },
  29. { path: "./images" },
  30. { path: "./static" },
  31. { path: "./styles", outputExtension: ".css" },
  32. { path: "./partials" },
  33. { path: "./fonts" },
  34. { path: "./js", outputExtension: ".js" },
  35. { path: "./pages" }
  36. ]
  37. }
  38. ],
  39. [
  40. {
  41. func: "decorateFileObject",
  42. selector: state => state.selectAll(),
  43. config: {
  44. decorators: [typeDecorator],
  45. defaultName: "Home"
  46. }
  47. }
  48. ],
  49. [
  50. {
  51. func: "decorateFileObject",
  52. selector: state =>
  53. state
  54. .matchingAnyTag(["fonts", "content", "js", "styles"])
  55. .and(state.selectByTag("images").not(state.selectByTag("favicon"))),
  56. config: {
  57. decorators: [outputDecorator],
  58. baseDir: "content"
  59. }
  60. },
  61. {
  62. func: "decorateFileObject",
  63. selector: state => state.selectByTag("favicon"),
  64. config: {
  65. decorators: [outputDecorator],
  66. baseDir: "images",
  67. outputExtension: ".ico"
  68. }
  69. },
  70. {
  71. func: "decorateFileObject",
  72. selector: state => state.selectByTag("static"),
  73. config: {
  74. decorators: [outputDecorator],
  75. baseDir: "static"
  76. }
  77. },
  78. {
  79. func: "copy",
  80. selector: state => state.matchingAnyTag(["partials", "svgs", "pages"])
  81. }
  82. ],
  83. [
  84. {
  85. func: "copyFileTo",
  86. selector: state => state.matchingAnyTag(["fonts", "static"]),
  87. config: { outputDir: "./output" }
  88. },
  89. {
  90. func: "imageMin",
  91. allowEmpty: true,
  92. selector: state =>
  93. state.selectByTag("images").not(state.selectByTag("svgs")),
  94. config: {
  95. outputDir: "./output"
  96. }
  97. },
  98. {
  99. func: "readInFile",
  100. selector: state => {
  101. return state.not(
  102. state
  103. .selectByTag("images")
  104. .not(state.selectByTag("svgs").and(state.selectByTag("fonts")))
  105. );
  106. }
  107. }
  108. ],
  109. [
  110. {
  111. func: "markdownToHtml",
  112. selector: state => state.selectByTag("markdown")
  113. },
  114. {
  115. func: "compileTemplates",
  116. selector: state => state.selectByTag("pages"),
  117. getConfig: state => ({
  118. partials: state.selectByTag("partials")
  119. })
  120. },
  121. {
  122. func: "copy",
  123. selector: state => state.matchingAnyTag(["styles", "svgs", "js"])
  124. }
  125. ],
  126. [
  127. {
  128. func: "decorateFileObject",
  129. selector: state =>
  130. state
  131. .selectByTag("content")
  132. .selectByTag("markdown")
  133. .not(state.selectByTag("index")),
  134. config: {
  135. decorators: [articleDecorator],
  136. cutoffLength: 180
  137. }
  138. },
  139. {
  140. func: "copy",
  141. selector: state => {
  142. return state.matchingAnyTag([
  143. "pages",
  144. "styles",
  145. "svgs",
  146. "index",
  147. "work",
  148. "js"
  149. ]);
  150. }
  151. }
  152. ],
  153. [
  154. {
  155. func: "renderTemplate",
  156. deferConfig: true,
  157. selector: state => state.selectByTag("content"),
  158. getConfig: (state, currentItem) => {
  159. const matchingTemplate = state
  160. .selectByTag("pages")
  161. .mostMatchingTags(currentItem.tags);
  162. console.log(
  163. `Rendering ${currentItem.dir}/${currentItem.name} with ${
  164. matchingTemplate.dir
  165. }/${matchingTemplate.name}`
  166. );
  167. const isIndex = currentItem.tags.includes("index");
  168. const dirPath = currentItem.dir.split("/");
  169. const dir = dirPath
  170. .slice(1, dirPath.length - (isIndex ? 1 : 0))
  171. .join("/");
  172. return {
  173. meta: {
  174. title: currentItem.niceName,
  175. dir: dir,
  176. now: new Date().toLocaleString(),
  177. context: dirPath[1],
  178. isIndex: isIndex,
  179. svgs: toNameContentObject(state.selectByTag("svgs")),
  180. partials: toNameContentObject(state.selectByTag("partials")),
  181. entries: state
  182. .selectByTag("content")
  183. .selectByTag("markdown")
  184. .not(state.selectByTag("index"))
  185. },
  186. template: matchingTemplate
  187. };
  188. }
  189. },
  190. {
  191. func: "copy",
  192. selector: state =>
  193. state.selectByTag("styles").and(state.selectByTag("js"))
  194. }
  195. ],
  196. [
  197. {
  198. func: "minifyHtml",
  199. selector: state => state.selectAll().not(state.selectByTag("js"))
  200. },
  201. {
  202. func: "minifyJS",
  203. selector: state => state.selectByTag("js")
  204. }
  205. ],
  206. [
  207. {
  208. func: "writeOutFile",
  209. selector: state => state.selectAll(),
  210. config: { outputDir: "./output" }
  211. }
  212. ]
  213. ]
  214. };