Guest User

Untitled

a guest
Nov 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. const fs = require('fs')
  2. const path = require('path')
  3.  
  4. // page{location}(content) => posthtml [inline-assets] w/ postcss
  5. const page = require('./page.js')
  6.  
  7. // watch system with deduping and some other fancy stuff
  8. const dispatch = require('./dispatch')
  9.  
  10. const globby = require('globby')
  11.  
  12. const cwd = process.cwd()
  13.  
  14. let readFile
  15. let writeFile
  16. let mkdir
  17.  
  18. let source
  19. let filename
  20.  
  21. var Promise = require('bluebird')
  22.  
  23. readFile = Promise.promisify(fs.readFile)
  24. writeFile = Promise.promisify(fs.writeFile)
  25. mkdir = Promise.promisify(fs.mkdir)
  26.  
  27. async function main () {
  28. let paths
  29.  
  30. let files
  31. let filename
  32.  
  33. files = await globby(['public/**/*.html', '!**/script/**'])
  34.  
  35. for (filename of files) {
  36. try {
  37. await build(filename)
  38. } catch (e) {}
  39. }
  40. }
  41.  
  42. async function write (filename, content) {
  43. let options
  44.  
  45. options = {encoding: 'utf8'}
  46.  
  47. try {
  48. await mkdir(path.dirname(filename))
  49. } catch (e) {}
  50.  
  51. await writeFile(filename, content, options)
  52. }
  53.  
  54. async function build (it) {
  55. let $it
  56. let input
  57. let output
  58.  
  59. let compile
  60.  
  61. let that
  62.  
  63. $it = it.replace(/^public/, 'page')
  64.  
  65. input = path.resolve(cwd, it)
  66. output = path.resolve(cwd, $it)
  67.  
  68. compile = page.bind({location: input})
  69.  
  70. process.stdout.write(`${it} `)
  71.  
  72. that = await read(input)
  73. process.stdout.write('=> ')
  74.  
  75. try {
  76. that = await compile(that)
  77. }
  78. catch (error) {
  79. console.log(`[${error.constructor.name}] ${error.message}`)
  80. return
  81. }
  82. finally {}
  83.  
  84. try {
  85. await mkdir(path.dirname(output))
  86. }
  87. catch (error) {}
  88. finally {}
  89.  
  90. try {
  91. await writeFile(output, that)
  92. process.stdout.write($it)
  93. }
  94. catch (error) {}
  95. finally {
  96. console.log('')
  97. }
  98. }
  99.  
  100. process.nextTick(main)
  101.  
  102. async function read (filename) {
  103. let options
  104.  
  105. options = {encoding: 'utf8'}
  106.  
  107. return await readFile(filename, options)
  108. }
Add Comment
Please, Sign In to add comment