|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const args = process.argv.slice(2) |
| 6 | + |
| 7 | +const fs = require('fs') |
| 8 | +const path = require('path') |
| 9 | +const mkdirp = require('mkdirp') |
| 10 | +const pug = require('pug') |
| 11 | +const src = './pug/' |
| 12 | +const dest = './src/' |
| 13 | +const pkg = require(path.resolve(__dirname, '../package.json')) |
| 14 | +const beautify = require('js-beautify').html |
| 15 | +const jsbOptions = { |
| 16 | + indent_size: 2, |
| 17 | + indent_inner_html: true, |
| 18 | + unformatted: [''], |
| 19 | + content_unformatted: ['textarea'], |
| 20 | + extra_liners: [''] |
| 21 | +} |
| 22 | +const version = args[0]; |
| 23 | + |
| 24 | +const basename = path.basename |
| 25 | +const dirname = path.dirname |
| 26 | +const resolve = path.resolve |
| 27 | +const normalize = path.normalize |
| 28 | +const join = path.join |
| 29 | +const relative = path.relative |
| 30 | +const extension = path.extname |
| 31 | + |
| 32 | +const walkSync = (dir, filelist = []) => { |
| 33 | + fs.readdirSync(dir).forEach(file => { |
| 34 | + filelist = fs.statSync(path.join(dir, file)).isDirectory() |
| 35 | + ? walkSync(path.join(dir, file), filelist) |
| 36 | + : filelist.concat(path.join(dir, file)) |
| 37 | + }) |
| 38 | + return filelist |
| 39 | +} |
| 40 | + |
| 41 | +const isPug = (filename) => { |
| 42 | + return extension(filename) === '.pug' ? true : false |
| 43 | +} |
| 44 | + |
| 45 | +const compile = (filename, basedir) => { |
| 46 | + const levels = filename.replace('pug/views/', '').replace('pug/pages/', '').split('/').length |
| 47 | + const base = (levels) => { |
| 48 | + let path = './' |
| 49 | + while (levels > 1) { |
| 50 | + levels = levels - 1; |
| 51 | + path = path + '../' |
| 52 | + } |
| 53 | + return path |
| 54 | + } |
| 55 | + |
| 56 | + const fn = pug.compileFile(filename, { |
| 57 | + basedir: basedir, |
| 58 | + pretty: true, |
| 59 | + }) |
| 60 | + const html = fn({ |
| 61 | + base: base(levels) |
| 62 | + }); |
| 63 | + return html |
| 64 | +} |
| 65 | + |
| 66 | +// Build html files |
| 67 | +const compileHtml = () => { |
| 68 | + // Build index |
| 69 | + if (version === 'ajax') { |
| 70 | + const html = compile('./pug/layout/index.pug', './pug/layout/') |
| 71 | + fs.writeFile(resolve(dest, 'index.html'), beautify(html, jsbOptions), function(err) { |
| 72 | + if(err) { |
| 73 | + return console.log(err); |
| 74 | + } |
| 75 | + console.log('index.html file was saved!'); |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + // Build views |
| 80 | + const views = walkSync('./pug/views/') |
| 81 | + views.forEach((view) => { |
| 82 | + if (isPug(view)) { |
| 83 | + const html = compile(view, './pug/layout/') |
| 84 | + let file |
| 85 | + if (version === 'ajax') { |
| 86 | + file = view.replace('pug/', '').replace('.pug', '.html') |
| 87 | + } else { |
| 88 | + file = view.replace('pug/views/', '').replace('.pug', '.html') |
| 89 | + } |
| 90 | + // Create tree |
| 91 | + mkdirp.sync(resolve(dest, dirname(file))) |
| 92 | + // Create HTML file |
| 93 | + fs.writeFile(resolve(dest, file), beautify(html, jsbOptions), function(err) { |
| 94 | + if(err) { |
| 95 | + return console.log(err) |
| 96 | + } |
| 97 | + console.log(file + ' file was saved!') |
| 98 | + }) |
| 99 | + } |
| 100 | + }) |
| 101 | + // Build pages |
| 102 | + const pages = walkSync('./pug/pages') |
| 103 | + pages.forEach((page) => { |
| 104 | + if (isPug(page)) { |
| 105 | + const html = compile(page, './pug/layout/') |
| 106 | + const file = page.replace('pug/pages/', '').replace('.pug', '.html') |
| 107 | + // Create tree |
| 108 | + mkdirp.sync(resolve(dest, dirname(file))) |
| 109 | + // Create HTML file |
| 110 | + fs.writeFile(resolve(dest, file), beautify(html, jsbOptions), function(err) { |
| 111 | + if(err) { |
| 112 | + return console.log(err) |
| 113 | + } |
| 114 | + console.log(file + ' file was saved!') |
| 115 | + }) |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +compileHtml() |
0 commit comments